| OLD | NEW |
| 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 | 5 |
| 6 /** | 6 /** |
| 7 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert | 7 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert |
| 8 * as much of the input as needed. Determines the byte order from the BOM, | 8 * as much of the input as needed. Determines the byte order from the BOM, |
| 9 * or uses big-endian as a default. This method always strips a leading BOM. | 9 * or uses big-endian as a default. This method always strips a leading BOM. |
| 10 * Set the [replacementCodepoint] to null to throw an ArgumentError | 10 * Set the [replacementCodepoint] to null to throw an ArgumentError |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 // TODO is16BitCodeUnit() is used to work around a bug with dart2js | 201 // TODO is16BitCodeUnit() is used to work around a bug with dart2js |
| 202 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 202 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 203 // removing after this issue is resolved. | 203 // removing after this issue is resolved. |
| 204 if (_is16BitCodeUnit()) { | 204 if (_is16BitCodeUnit()) { |
| 205 return str.charCodes(); | 205 return str.charCodes(); |
| 206 } else { | 206 } else { |
| 207 return _codepointsToUtf16CodeUnits(str.charCodes()); | 207 return _codepointsToUtf16CodeUnits(str.charCodes()); |
| 208 } | 208 } |
| 209 } | 209 } |
| 210 | 210 |
| 211 typedef _ListRangeIterator _CodeUnitsProvider(); |
| 212 |
| 211 /** | 213 /** |
| 212 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type | 214 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type |
| 213 * provides an iterator on demand and the iterator will only translate bytes | 215 * provides an iterator on demand and the iterator will only translate bytes |
| 214 * as requested by the user of the iterator. (Note: results are not cached.) | 216 * as requested by the user of the iterator. (Note: results are not cached.) |
| 215 */ | 217 */ |
| 216 class IterableUtf16Decoder implements Iterable<int> { | 218 class IterableUtf16Decoder implements Iterable<int> { |
| 217 final Function codeunitsProvider; | 219 final _CodeUnitsProvider codeunitsProvider; |
| 218 final int replacementCodepoint; | 220 final int replacementCodepoint; |
| 219 | 221 |
| 220 IterableUtf16Decoder._(_ListRangeIterator this.codeunitsProvider(), | 222 IterableUtf16Decoder._(this.codeunitsProvider, this.replacementCodepoint); |
| 221 int this.replacementCodepoint); | |
| 222 | 223 |
| 223 Utf16CodeUnitDecoder iterator() => | 224 Utf16CodeUnitDecoder iterator() => |
| 224 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), | 225 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), |
| 225 replacementCodepoint); | 226 replacementCodepoint); |
| 226 } | 227 } |
| 227 | 228 |
| 228 /** | 229 /** |
| 229 * Convert UTF-16 encoded bytes to UTF-16 code units by grouping 1-2 bytes | 230 * Convert UTF-16 encoded bytes to UTF-16 code units by grouping 1-2 bytes |
| 230 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine | 231 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine |
| 231 * endian-ness, and defaults to BE. | 232 * endian-ness, and defaults to BE. |
| 232 */ | 233 */ |
| 233 class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator { | 234 class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator { |
| 234 final _ListRangeIterator utf16EncodedBytesIterator; | 235 final _ListRangeIterator utf16EncodedBytesIterator; |
| 235 final int replacementCodepoint; | 236 final int replacementCodepoint; |
| 236 | 237 |
| 237 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( | 238 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( |
| 238 _ListRangeIterator this.utf16EncodedBytesIterator, | 239 this.utf16EncodedBytesIterator, this.replacementCodepoint); |
| 239 int this.replacementCodepoint); | |
| 240 | 240 |
| 241 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | 241 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 242 int offset = 0, int length, | 242 int offset = 0, int length, |
| 243 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 243 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 244 if (length == null) { | 244 if (length == null) { |
| 245 length = utf16EncodedBytes.length - offset; | 245 length = utf16EncodedBytes.length - offset; |
| 246 } | 246 } |
| 247 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { | 247 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { |
| 248 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | 248 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, |
| 249 length - 2, false, replacementCodepoint); | 249 length - 2, false, replacementCodepoint); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 skip(); | 343 skip(); |
| 344 } | 344 } |
| 345 } | 345 } |
| 346 | 346 |
| 347 int decode() { | 347 int decode() { |
| 348 int lo = utf16EncodedBytesIterator.next(); | 348 int lo = utf16EncodedBytesIterator.next(); |
| 349 int hi = utf16EncodedBytesIterator.next(); | 349 int hi = utf16EncodedBytesIterator.next(); |
| 350 return (hi << 8) + lo; | 350 return (hi << 8) + lo; |
| 351 } | 351 } |
| 352 } | 352 } |
| OLD | NEW |