Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: runtime/bin/string_stream.dart

Issue 10945004: Make it possible to read a specific number of characters from a string input stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/input_stream.dart ('k') | runtime/bin/websocket_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Interface for decoders decoding binary data into string data. The 5 // Interface for decoders decoding binary data into string data. The
6 // decoder keeps track of line breaks during decoding. 6 // decoder keeps track of line breaks during decoding.
7 interface _StringDecoder { 7 interface _StringDecoder {
8 // Add more binary data to be decoded. The ownership of the buffer 8 // Add more binary data to be decoded. The ownership of the buffer
9 // is transfered to the decoder and the caller most not modify it any more. 9 // is transfered to the decoder and the caller most not modify it any more.
10 int write(List<int> buffer); 10 int write(List<int> buffer);
11 11
12 // Returns whether any decoded data is available. 12 // Returns whether any decoded data is available.
13 bool isEmpty(); 13 bool isEmpty();
14 14
15 // Returns the number of available decoded characters. 15 // Returns the number of available decoded characters.
16 int available(); 16 int available();
17 17
18 // Get the number of line breaks present in the current decoded 18 // Get the number of line breaks present in the current decoded
19 // data. 19 // data.
20 int get lineBreaks; 20 int get lineBreaks;
21 21
22 // Get the string data decoded since the last call to [decode] or 22 // Get up to [len] characters of string data decoded since the last
23 // [decodeLine]. Returns null if no decoded data is available. 23 // call to [decode] or [decodeLine]. Returns null if no decoded data
24 String get decoded; 24 // is available. If [len] is not specified all decoded characters
25 // are returned.
26 String decoded([int len]);
25 27
26 // Get the string data decoded since the last call to [decode] or 28 // Get the string data decoded since the last call to [decode] or
27 // [decodeLine] up to the next line break present. Returns null if 29 // [decodeLine] up to the next line break present. Returns null if
28 // no line break is present. The line break character sequence is 30 // no line break is present. The line break character sequence is
29 // discarded. 31 // discarded.
30 String get decodedLine; 32 String get decodedLine;
31 } 33 }
32 34
33 35
34 class _StringDecoders { 36 class _StringDecoders {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 break; 75 break;
74 } 76 }
75 } 77 }
76 return buffer.length; 78 return buffer.length;
77 } 79 }
78 80
79 bool isEmpty() => _result.isEmpty(); 81 bool isEmpty() => _result.isEmpty();
80 82
81 int get lineBreaks => _lineBreaks; 83 int get lineBreaks => _lineBreaks;
82 84
83 String get decoded { 85 String decoded([int len]) {
84 if (isEmpty()) return null; 86 if (isEmpty()) return null;
85 87
86 String result; 88 String result;
87 if (_resultOffset == 0) { 89 if (len !== null && len < available()) {
88 result = new String.fromCharCodes(_result); 90 result = new String.fromCharCodes(_result.getRange(_resultOffset, len));
89 } else { 91 } else {
90 result = 92 if (_resultOffset == 0) {
91 new String.fromCharCodes( 93 result = new String.fromCharCodes(_result);
92 _result.getRange(_resultOffset, _result.length - _resultOffset)); 94 } else {
95 result =
96 new String.fromCharCodes(
97 _result.getRange(_resultOffset,
98 _result.length - _resultOffset));
99 }
93 } 100 }
94 while (!_lineBreakEnds.isEmpty() && _lineBreakEnds.first() < _charOffset) { 101 _resultOffset += result.length;
102 while (!_lineBreakEnds.isEmpty() &&
103 _lineBreakEnds.first() < _charOffset + _resultOffset) {
95 _lineBreakEnds.removeFirst(); 104 _lineBreakEnds.removeFirst();
96 _lineBreaks--; 105 _lineBreaks--;
97 } 106 }
98 _resetResult(); 107 if (_result.length == _resultOffset) _resetResult();
99 return result; 108 return result;
100 } 109 }
101 110
102 String get decodedLine { 111 String get decodedLine {
103 if (_lineBreakEnds.isEmpty()) return null; 112 if (_lineBreakEnds.isEmpty()) return null;
104 int lineEnd = _lineBreakEnds.removeFirst(); 113 int lineEnd = _lineBreakEnds.removeFirst();
105 int terminationSequenceLength = 1; 114 int terminationSequenceLength = 1;
106 if (_result[lineEnd - _charOffset] == LF && 115 if (_result[lineEnd - _charOffset] == LF &&
107 lineEnd > _charOffset && 116 lineEnd > _charOffset &&
117 _resultOffset < lineEnd &&
108 _result[lineEnd - _charOffset - 1] == CR) { 118 _result[lineEnd - _charOffset - 1] == CR) {
109 terminationSequenceLength = 2; 119 terminationSequenceLength = 2;
110 } 120 }
111 var lineLength = 121 var lineLength =
112 lineEnd - _charOffset - _resultOffset - terminationSequenceLength + 1; 122 lineEnd - _charOffset - _resultOffset - terminationSequenceLength + 1;
113 String result = 123 String result =
114 new String.fromCharCodes(_result.getRange(_resultOffset, lineLength)); 124 new String.fromCharCodes(_result.getRange(_resultOffset, lineLength));
115 _lineBreaks--; 125 _lineBreaks--;
116 _resultOffset += (lineLength + terminationSequenceLength); 126 _resultOffset += (lineLength + terminationSequenceLength);
117 if (_result.length == _resultOffset) _resetResult(); 127 if (_result.length == _resultOffset) _resetResult();
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 356
347 class _StringInputStream implements StringInputStream { 357 class _StringInputStream implements StringInputStream {
348 _StringInputStream(InputStream this._input, 358 _StringInputStream(InputStream this._input,
349 [Encoding encoding = Encoding.UTF_8]) 359 [Encoding encoding = Encoding.UTF_8])
350 : _encoding = encoding { 360 : _encoding = encoding {
351 _decoder = _StringDecoders.decoder(encoding); 361 _decoder = _StringDecoders.decoder(encoding);
352 _input.onData = _onData; 362 _input.onData = _onData;
353 _input.onClosed = _onClosed; 363 _input.onClosed = _onClosed;
354 } 364 }
355 365
356 String read() { 366 String read([int len]) {
357 String result = _decoder.decoded; 367 String result = _decoder.decoded(len);
358 _checkInstallDataHandler(); 368 _checkInstallDataHandler();
359 return result; 369 return result;
360 } 370 }
361 371
362 String readLine() { 372 String readLine() {
363 String decodedLine = _decoder.decodedLine; 373 String decodedLine = _decoder.decodedLine;
364 if (decodedLine == null) { 374 if (decodedLine == null) {
365 if (_inputClosed) { 375 if (_inputClosed) {
366 // Last line might not have a line separator. 376 // Last line might not have a line separator.
367 decodedLine = _decoder.decoded; 377 decodedLine = _decoder.decoded();
368 if (decodedLine != null && 378 if (decodedLine != null &&
369 decodedLine[decodedLine.length - 1] == '\r') { 379 decodedLine[decodedLine.length - 1] == '\r') {
370 decodedLine = decodedLine.substring(0, decodedLine.length - 1); 380 decodedLine = decodedLine.substring(0, decodedLine.length - 1);
371 } 381 }
372 } 382 }
373 } 383 }
374 _checkInstallDataHandler(); 384 _checkInstallDataHandler();
375 return decodedLine; 385 return decodedLine;
376 } 386 }
377 387
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 bool _inputClosed = false; // Is the underlying input stream closed? 525 bool _inputClosed = false; // Is the underlying input stream closed?
516 bool _closed = false; // Is this stream closed. 526 bool _closed = false; // Is this stream closed.
517 bool _eof = false; // Has all data been read from the decoder? 527 bool _eof = false; // Has all data been read from the decoder?
518 Timer _scheduledDataCallback; 528 Timer _scheduledDataCallback;
519 Timer _scheduledLineCallback; 529 Timer _scheduledLineCallback;
520 Timer _scheduledCloseCallback; 530 Timer _scheduledCloseCallback;
521 Function _clientDataHandler; 531 Function _clientDataHandler;
522 Function _clientLineHandler; 532 Function _clientLineHandler;
523 Function _clientCloseHandler; 533 Function _clientCloseHandler;
524 } 534 }
OLDNEW
« no previous file with comments | « runtime/bin/input_stream.dart ('k') | runtime/bin/websocket_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698