| 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 IllegalArgumentException | 10 * Set the [replacementCodepoint] to null to throw an IllegalArgumentException |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 210 |
| 211 /** | 211 /** |
| 212 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type | 212 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type |
| 213 * provides an iterator on demand and the iterator will only translate bytes | 213 * 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.) | 214 * as requested by the user of the iterator. (Note: results are not cached.) |
| 215 */ | 215 */ |
| 216 class IterableUtf16Decoder implements Iterable<int> { | 216 class IterableUtf16Decoder implements Iterable<int> { |
| 217 final Function codeunitsProvider; | 217 final Function codeunitsProvider; |
| 218 final int replacementCodepoint; | 218 final int replacementCodepoint; |
| 219 | 219 |
| 220 IterableUtf16Decoder._(_ListRangeIterator<int> this.codeunitsProvider(), | 220 IterableUtf16Decoder._(_ListRangeIterator this.codeunitsProvider(), |
| 221 int this.replacementCodepoint); | 221 int this.replacementCodepoint); |
| 222 | 222 |
| 223 Utf16CodeUnitDecoder iterator() => | 223 Utf16CodeUnitDecoder iterator() => |
| 224 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), | 224 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), |
| 225 replacementCodepoint); | 225 replacementCodepoint); |
| 226 } | 226 } |
| 227 | 227 |
| 228 /** | 228 /** |
| 229 * Convert UTF-16 encoded bytes to UTF-16 code units by grouping 1-2 bytes | 229 * 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 | 230 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine |
| 231 * endian-ness, and defaults to BE. | 231 * endian-ness, and defaults to BE. |
| 232 */ | 232 */ |
| 233 class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator<int> { | 233 class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator { |
| 234 final _ListRangeIterator<int> utf16EncodedBytesIterator; | 234 final _ListRangeIterator utf16EncodedBytesIterator; |
| 235 final int replacementCodepoint; | 235 final int replacementCodepoint; |
| 236 | 236 |
| 237 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( | 237 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( |
| 238 _ListRangeIterator<int> this.utf16EncodedBytesIterator, | 238 _ListRangeIterator this.utf16EncodedBytesIterator, |
| 239 int 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, |
| (...skipping 94 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 |