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

Side by Side Diff: sdk/lib/io/string_stream.dart

Issue 11364115: Don't crash when decoding astral plane UTF-8. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Respond to review. Created 8 years, 1 month 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 | « no previous file | tests/standalone/io/string_stream_test.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 abstract class _StringDecoder { 7 abstract class _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);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 break; 78 break;
79 } 79 }
80 } 80 }
81 return buffer.length; 81 return buffer.length;
82 } 82 }
83 83
84 bool get isEmpty => _result.isEmpty; 84 bool get isEmpty => _result.isEmpty;
85 85
86 int get lineBreaks => _lineBreaks; 86 int get lineBreaks => _lineBreaks;
87 87
88 String decoded([int len]) { 88 String decoded([int length]) {
89 if (isEmpty) return null; 89 if (isEmpty) return null;
90 90
91 String result; 91 List<int> result;
92 if (len !== null && len < available()) { 92 if (length != null && length < available()) {
93 result = new String.fromCharCodes(_result.getRange(_resultOffset, len)); 93 result = _result.getRange(_resultOffset, length);
94 } else if (_resultOffset == 0) {
95 result = _result;
94 } else { 96 } else {
95 if (_resultOffset == 0) { 97 result = _result.getRange(_resultOffset, _result.length - _resultOffset);
96 result = new String.fromCharCodes(_result);
97 } else {
98 result =
99 new String.fromCharCodes(
100 _result.getRange(_resultOffset,
101 _result.length - _resultOffset));
102 }
103 } 98 }
99
104 _resultOffset += result.length; 100 _resultOffset += result.length;
105 while (!_lineBreakEnds.isEmpty && 101 while (!_lineBreakEnds.isEmpty &&
106 _lineBreakEnds.first < _charOffset + _resultOffset) { 102 _lineBreakEnds.first < _charOffset + _resultOffset) {
107 _lineBreakEnds.removeFirst(); 103 _lineBreakEnds.removeFirst();
108 _lineBreaks--; 104 _lineBreaks--;
109 } 105 }
110 if (_result.length == _resultOffset) _resetResult(); 106 if (_result.length == _resultOffset) _resetResult();
111 return result; 107 return new String.fromCharCodes(result);
112 } 108 }
113 109
114 String get decodedLine { 110 String get decodedLine {
115 if (_lineBreakEnds.isEmpty) return null; 111 if (_lineBreakEnds.isEmpty) return null;
116 int lineEnd = _lineBreakEnds.removeFirst(); 112 int lineEnd = _lineBreakEnds.removeFirst();
117 int terminationSequenceLength = 1; 113 int terminationSequenceLength = 1;
118 if (_result[lineEnd - _charOffset] == LF && 114 if (_result[lineEnd - _charOffset] == LF &&
119 lineEnd > _charOffset && 115 lineEnd > _charOffset &&
120 _resultOffset < lineEnd && 116 _resultOffset < lineEnd &&
121 _result[lineEnd - _charOffset - 1] == CR) { 117 _result[lineEnd - _charOffset - 1] == CR) {
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 _checkInstallDataHandler(); 408 _checkInstallDataHandler();
413 _checkScheduleCallback(); 409 _checkScheduleCallback();
414 } 410 }
415 411
416 void set onClosed(void callback()) { 412 void set onClosed(void callback()) {
417 _clientCloseHandler = callback; 413 _clientCloseHandler = callback;
418 } 414 }
419 415
420 void set onError(void callback(e)) { 416 void set onError(void callback(e)) {
421 _input.onError = callback; 417 _input.onError = callback;
422 _decoder.onError = (e) { 418 _decoder.onError = (e) {
423 _clientCloseHandler = null; 419 _clientCloseHandler = null;
424 _input.close(); 420 _input.close();
425 callback(e); 421 callback(e);
426 }; 422 };
427 } 423 }
428 424
429 void _onData() { 425 void _onData() {
430 _readData(); 426 _readData();
431 if (!_decoder.isEmpty && _clientDataHandler !== null) { 427 if (!_decoder.isEmpty && _clientDataHandler !== null) {
432 _clientDataHandler(); 428 _clientDataHandler();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 bool _inputClosed = false; // Is the underlying input stream closed? 533 bool _inputClosed = false; // Is the underlying input stream closed?
538 bool _closed = false; // Is this stream closed. 534 bool _closed = false; // Is this stream closed.
539 bool _eof = false; // Has all data been read from the decoder? 535 bool _eof = false; // Has all data been read from the decoder?
540 Timer _scheduledDataCallback; 536 Timer _scheduledDataCallback;
541 Timer _scheduledLineCallback; 537 Timer _scheduledLineCallback;
542 Timer _scheduledCloseCallback; 538 Timer _scheduledCloseCallback;
543 Function _clientDataHandler; 539 Function _clientDataHandler;
544 Function _clientLineHandler; 540 Function _clientLineHandler;
545 Function _clientCloseHandler; 541 Function _clientCloseHandler;
546 } 542 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/string_stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698