| 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 utf; | 5 part of utf; |
| 6 | 6 |
| 7 // TODO(floitsch): make this transformer reusable. | 7 // TODO(floitsch): make this transformer reusable. |
| 8 abstract class _StringDecoder | 8 abstract class _StringDecoder |
| 9 implements StreamTransformer<List<int>, String>, EventSink<List<int>> { | 9 implements StreamTransformer<List<int>, String>, EventSink<List<int>> { |
| 10 List<int> _carry; | 10 List<int> _carry; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 if (_buffer.length > 0) { | 71 if (_buffer.length > 0) { |
| 72 // Limit to 'goodChars', if lower than actual charCodes in the buffer. | 72 // Limit to 'goodChars', if lower than actual charCodes in the buffer. |
| 73 _outSink.add(new String.fromCharCodes(_buffer)); | 73 _outSink.add(new String.fromCharCodes(_buffer)); |
| 74 } | 74 } |
| 75 _buffer = null; | 75 _buffer = null; |
| 76 } catch (e, stackTrace) { | 76 } catch (e, stackTrace) { |
| 77 _outSink.addError(e, stackTrace); | 77 _outSink.addError(e, stackTrace); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 void addError(Object error, [StackTrace stackTrace]) { | 81 void addError(error, [StackTrace stackTrace]) { |
| 82 _outSink.addError(error, stackTrace); | 82 _outSink.addError(error, stackTrace); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void close() { | 85 void close() { |
| 86 if (_carry != null) { | 86 if (_carry != null) { |
| 87 if (_replacementChar != null) { | 87 if (_replacementChar != null) { |
| 88 _outSink.add(new String.fromCharCodes( | 88 _outSink.add(new String.fromCharCodes( |
| 89 new List.filled(_carry.length, _replacementChar))); | 89 new List.filled(_carry.length, _replacementChar))); |
| 90 } else { | 90 } else { |
| 91 throw new ArgumentError('Invalid codepoint'); | 91 throw new ArgumentError('Invalid codepoint'); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 } | 182 } |
| 183 _outSink = sink; | 183 _outSink = sink; |
| 184 return this; | 184 return this; |
| 185 }); | 185 }); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void add(String data) { | 188 void add(String data) { |
| 189 _outSink.add(_processString(data)); | 189 _outSink.add(_processString(data)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void addError(Object error, [StackTrace stackTrace]) { | 192 void addError(error, [StackTrace stackTrace]) { |
| 193 _outSink.addError(error, stackTrace); | 193 _outSink.addError(error, stackTrace); |
| 194 } | 194 } |
| 195 | 195 |
| 196 void close() { _outSink.close(); } | 196 void close() { _outSink.close(); } |
| 197 | 197 |
| 198 List<int> _processString(String string); | 198 List<int> _processString(String string); |
| 199 } | 199 } |
| 200 | 200 |
| 201 /** | 201 /** |
| 202 * StringTransformer that UTF-8 encodes a stream of strings. | 202 * StringTransformer that UTF-8 encodes a stream of strings. |
| 203 */ | 203 */ |
| 204 class Utf8EncoderTransformer extends _StringEncoder { | 204 class Utf8EncoderTransformer extends _StringEncoder { |
| 205 List<int> _processString(String string) { | 205 List<int> _processString(String string) { |
| 206 var bytes = []; | 206 var bytes = <int>[]; |
| 207 int pos = 0; | 207 int pos = 0; |
| 208 List<int> codepoints = utf16CodeUnitsToCodepoints(string.codeUnits); | 208 List<int> codepoints = utf16CodeUnitsToCodepoints(string.codeUnits); |
| 209 int length = codepoints.length; | 209 int length = codepoints.length; |
| 210 for (int i = 0; i < length; i++) { | 210 for (int i = 0; i < length; i++) { |
| 211 int additionalBytes; | 211 int additionalBytes; |
| 212 int charCode = codepoints[i]; | 212 int charCode = codepoints[i]; |
| 213 if (charCode <= 0x007F) { | 213 if (charCode <= 0x007F) { |
| 214 additionalBytes = 0; | 214 additionalBytes = 0; |
| 215 bytes.add(charCode); | 215 bytes.add(charCode); |
| 216 } else if (charCode <= 0x07FF) { | 216 } else if (charCode <= 0x07FF) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 228 } | 228 } |
| 229 for (int i = additionalBytes; i > 0; i--) { | 229 for (int i = additionalBytes; i > 0; i--) { |
| 230 // 10xxxxxx (xxxxxx is next 6 bits from the top). | 230 // 10xxxxxx (xxxxxx is next 6 bits from the top). |
| 231 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); | 231 bytes.add(((charCode >> (6 * (i - 1))) & 0x3F) | 0x80); |
| 232 } | 232 } |
| 233 pos += additionalBytes + 1; | 233 pos += additionalBytes + 1; |
| 234 } | 234 } |
| 235 return bytes; | 235 return bytes; |
| 236 } | 236 } |
| 237 } | 237 } |
| OLD | NEW |