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

Side by Side Diff: pkg/utf/lib/utf_stream.dart

Issue 25354003: Redo StreamTransformers so they work with Stack traces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix two more tests. Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of dart.utf; 5 part of dart.utf;
6 6
7 abstract class _StringDecoder 7 abstract class _StringDecoder
8 extends StreamEventTransformer<List<int>, String> { 8 implements StreamTransformer<List<int>, String>, EventSink<List<int>> {
9 List<int> _carry; 9 List<int> _carry;
10 List<int> _buffer; 10 List<int> _buffer;
11 int _replacementChar; 11 int _replacementChar;
12 12
13 EventSink<String> _outSink;
14
13 _StringDecoder(int this._replacementChar); 15 _StringDecoder(int this._replacementChar);
14 16
15 void handleData(List<int> bytes, EventSink<String> sink) { 17 Stream<String> bind(Stream<List<int>> stream) {
18 return new Stream.eventTransformed(
19 stream,
20 (EventSink<String> sink) {
21 if (_outSink != null) {
22 throw new StateError("String decoder already used");
23 }
24 _outSink = sink;
25 return this;
26 });
27 }
28
29 void add(List<int> bytes) {
16 try { 30 try {
17 _buffer = <int>[]; 31 _buffer = <int>[];
18 List<int> carry = _carry; 32 List<int> carry = _carry;
19 _carry = null; 33 _carry = null;
20 int pos = 0; 34 int pos = 0;
21 int available = bytes.length; 35 int available = bytes.length;
22 // If we have carry-over data, start from negative index, indicating carry 36 // If we have carry-over data, start from negative index, indicating carry
23 // index. 37 // index.
24 int goodChars = 0; 38 int goodChars = 0;
25 if (carry != null) pos = -carry.length; 39 if (carry != null) pos = -carry.length;
(...skipping 22 matching lines...) Expand all
48 break; 62 break;
49 } else { 63 } else {
50 // Invalid byte at position pos - 1 64 // Invalid byte at position pos - 1
51 _buffer.length = goodChars; 65 _buffer.length = goodChars;
52 _addChar(-1); 66 _addChar(-1);
53 goodChars = _buffer.length; 67 goodChars = _buffer.length;
54 } 68 }
55 } 69 }
56 if (_buffer.length > 0) { 70 if (_buffer.length > 0) {
57 // Limit to 'goodChars', if lower than actual charCodes in the buffer. 71 // Limit to 'goodChars', if lower than actual charCodes in the buffer.
58 sink.add(new String.fromCharCodes(_buffer)); 72 _outSink.add(new String.fromCharCodes(_buffer));
59 } 73 }
60 _buffer = null; 74 _buffer = null;
61 } catch (e) { 75 } catch (e, stackTrace) {
62 sink.addError(e); 76 _outSink.addError(e, stackTrace);
63 } 77 }
64 } 78 }
65 79
66 void handleDone(EventSink<String> sink) { 80 void addError(Object error, [StackTrace stackTrace]) {
81 _outSink.addError(error, stackTrace);
82 }
83
84 void close() {
67 if (_carry != null) { 85 if (_carry != null) {
68 if (_replacementChar != null) { 86 if (_replacementChar != null) {
69 sink.add(new String.fromCharCodes( 87 _outSink.add(new String.fromCharCodes(
70 new List.filled(_carry.length, _replacementChar))); 88 new List.filled(_carry.length, _replacementChar)));
71 } else { 89 } else {
72 throw new ArgumentError('Invalid codepoint'); 90 throw new ArgumentError('Invalid codepoint');
73 } 91 }
74 } 92 }
75 sink.close(); 93 _outSink.close();
76 } 94 }
77 95
78 int _processBytes(int getNext()); 96 int _processBytes(int getNext());
79 97
80 void _addChar(int char) { 98 void _addChar(int char) {
81 void error() { 99 void error() {
82 if (_replacementChar != null) { 100 if (_replacementChar != null) {
83 char = _replacementChar; 101 char = _replacementChar;
84 } else { 102 } else {
85 throw new ArgumentError('Invalid codepoint'); 103 throw new ArgumentError('Invalid codepoint');
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 _addChar(value); 161 _addChar(value);
144 return 1 + additionalBytes; 162 return 1 + additionalBytes;
145 } 163 }
146 _addChar(value); 164 _addChar(value);
147 return 1; 165 return 1;
148 } 166 }
149 } 167 }
150 168
151 169
152 abstract class _StringEncoder 170 abstract class _StringEncoder
153 extends StreamEventTransformer<String, List<int>> { 171 implements StreamTransformer<String, List<int>>, EventSink<String> {
154 172
155 void handleData(String data, EventSink<List<int>> sink) { 173 EventSink<List<int>> _outSink;
156 sink.add(_processString(data)); 174
175 Stream<List<int>> bind(Stream<String> stream) {
176 return new Stream.eventTransformed(
177 stream,
178 (EventSink<List<int>> sink) {
179 if (_outSink != null) {
180 throw new StateError("String encoder already used");
181 }
182 _outSink = sink;
183 return this;
184 });
157 } 185 }
158 186
187 void add(String data) {
188 _outSink.add(_processString(data));
189 }
190
191 void addError(Object error, [StackTrace stackTrace]) {
192 _outSink.addError(error, stackTrace);
193 }
194
195 void close() { _outSink.close(); }
196
159 List<int> _processString(String string); 197 List<int> _processString(String string);
160 } 198 }
161 199
162 /** 200 /**
163 * StringTransformer that UTF-8 encodes a stream of strings. 201 * StringTransformer that UTF-8 encodes a stream of strings.
164 */ 202 */
165 class Utf8EncoderTransformer extends _StringEncoder { 203 class Utf8EncoderTransformer extends _StringEncoder {
166 List<int> _processString(String string) { 204 List<int> _processString(String string) {
167 var bytes = []; 205 var bytes = [];
168 int pos = 0; 206 int pos = 0;
(...skipping 20 matching lines...) Expand all
189 } 227 }
190 for (int i = additionalBytes; i > 0; i--) { 228 for (int i = additionalBytes; i > 0; i--) {
191 // 10xxxxxx (xxxxxx is next 6 bits from the top). 229 // 10xxxxxx (xxxxxx is next 6 bits from the top).
192 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); 230 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80);
193 } 231 }
194 pos += additionalBytes + 1; 232 pos += additionalBytes + 1;
195 } 233 }
196 return bytes; 234 return bytes;
197 } 235 }
198 } 236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698