OLD | NEW |
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 Loading... |
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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 _addChar(value); | 163 _addChar(value); |
146 return 1 + additionalBytes; | 164 return 1 + additionalBytes; |
147 } | 165 } |
148 _addChar(value); | 166 _addChar(value); |
149 return 1; | 167 return 1; |
150 } | 168 } |
151 } | 169 } |
152 | 170 |
153 | 171 |
154 abstract class _StringEncoder | 172 abstract class _StringEncoder |
155 extends StreamEventTransformer<String, List<int>> { | 173 implements StreamTransformer<String, List<int>>, EventSink<String> { |
156 | 174 |
157 void handleData(String data, EventSink<List<int>> sink) { | 175 EventSink<List<int>> _outSink; |
158 sink.add(_processString(data)); | 176 |
| 177 Stream<List<int>> bind(Stream<String> stream) { |
| 178 return new Stream.eventTransformed( |
| 179 stream, |
| 180 (EventSink<List<int>> sink) { |
| 181 if (_outSink != null) { |
| 182 throw new StateError("String encoder already used"); |
| 183 } |
| 184 _outSink = sink; |
| 185 return this; |
| 186 }); |
159 } | 187 } |
160 | 188 |
| 189 void add(String data) { |
| 190 _outSink.add(_processString(data)); |
| 191 } |
| 192 |
| 193 void addError(Object error, [StackTrace stackTrace]) { |
| 194 _outSink.addError(error, stackTrace); |
| 195 } |
| 196 |
| 197 void close() { _outSink.close(); } |
| 198 |
161 List<int> _processString(String string); | 199 List<int> _processString(String string); |
162 } | 200 } |
163 | 201 |
164 /** | 202 /** |
165 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | 203 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` |
166 * instead. | 204 * instead. |
167 */ | 205 */ |
168 @deprecated | 206 @deprecated |
169 class Utf8EncoderTransformer extends _StringEncoder { | 207 class Utf8EncoderTransformer extends _StringEncoder { |
170 List<int> _processString(String string) { | 208 List<int> _processString(String string) { |
(...skipping 22 matching lines...) Expand all Loading... |
193 } | 231 } |
194 for (int i = additionalBytes; i > 0; i--) { | 232 for (int i = additionalBytes; i > 0; i--) { |
195 // 10xxxxxx (xxxxxx is next 6 bits from the top). | 233 // 10xxxxxx (xxxxxx is next 6 bits from the top). |
196 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); | 234 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); |
197 } | 235 } |
198 pos += additionalBytes + 1; | 236 pos += additionalBytes + 1; |
199 } | 237 } |
200 return bytes; | 238 return bytes; |
201 } | 239 } |
202 } | 240 } |
OLD | NEW |