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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/input_stream.dart ('k') | runtime/bin/websocket_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/string_stream.dart
diff --git a/runtime/bin/string_stream.dart b/runtime/bin/string_stream.dart
index 12f09df2a4ed43139fde6825e0be54220832b18a..ce2be4b9a9c418d0a43f4fb4b642f2d88eddab8f 100644
--- a/runtime/bin/string_stream.dart
+++ b/runtime/bin/string_stream.dart
@@ -19,9 +19,11 @@ interface _StringDecoder {
// data.
int get lineBreaks;
- // Get the string data decoded since the last call to [decode] or
- // [decodeLine]. Returns null if no decoded data is available.
- String get decoded;
+ // Get up to [len] characters of string data decoded since the last
+ // call to [decode] or [decodeLine]. Returns null if no decoded data
+ // is available. If [len] is not specified all decoded characters
+ // are returned.
+ String decoded([int len]);
// Get the string data decoded since the last call to [decode] or
// [decodeLine] up to the next line break present. Returns null if
@@ -80,22 +82,29 @@ class _StringDecoderBase implements _StringDecoder {
int get lineBreaks => _lineBreaks;
- String get decoded {
+ String decoded([int len]) {
if (isEmpty()) return null;
String result;
- if (_resultOffset == 0) {
- result = new String.fromCharCodes(_result);
+ if (len !== null && len < available()) {
+ result = new String.fromCharCodes(_result.getRange(_resultOffset, len));
} else {
- result =
- new String.fromCharCodes(
- _result.getRange(_resultOffset, _result.length - _resultOffset));
+ if (_resultOffset == 0) {
+ result = new String.fromCharCodes(_result);
+ } else {
+ result =
+ new String.fromCharCodes(
+ _result.getRange(_resultOffset,
+ _result.length - _resultOffset));
+ }
}
- while (!_lineBreakEnds.isEmpty() && _lineBreakEnds.first() < _charOffset) {
+ _resultOffset += result.length;
+ while (!_lineBreakEnds.isEmpty() &&
+ _lineBreakEnds.first() < _charOffset + _resultOffset) {
_lineBreakEnds.removeFirst();
_lineBreaks--;
}
- _resetResult();
+ if (_result.length == _resultOffset) _resetResult();
return result;
}
@@ -105,6 +114,7 @@ class _StringDecoderBase implements _StringDecoder {
int terminationSequenceLength = 1;
if (_result[lineEnd - _charOffset] == LF &&
lineEnd > _charOffset &&
+ _resultOffset < lineEnd &&
_result[lineEnd - _charOffset - 1] == CR) {
terminationSequenceLength = 2;
}
@@ -353,8 +363,8 @@ class _StringInputStream implements StringInputStream {
_input.onClosed = _onClosed;
}
- String read() {
- String result = _decoder.decoded;
+ String read([int len]) {
+ String result = _decoder.decoded(len);
_checkInstallDataHandler();
return result;
}
@@ -364,7 +374,7 @@ class _StringInputStream implements StringInputStream {
if (decodedLine == null) {
if (_inputClosed) {
// Last line might not have a line separator.
- decodedLine = _decoder.decoded;
+ decodedLine = _decoder.decoded();
if (decodedLine != null &&
decodedLine[decodedLine.length - 1] == '\r') {
decodedLine = decodedLine.substring(0, decodedLine.length - 1);
« 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