| 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 #library("utf16"); | |
| 6 #import("unicode_core.dart"); | |
| 7 #import("unicode.dart"); | |
| 8 | 5 |
| 9 /** | 6 /** |
| 10 * 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 |
| 11 * 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, |
| 12 * 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. |
| 13 * Set the [replacementCodepoint] to null to throw an IllegalArgumentException | 10 * Set the [replacementCodepoint] to null to throw an IllegalArgumentException |
| 14 * rather than replace the bad value. The default value for | 11 * rather than replace the bad value. The default value for |
| 15 * [replacementCodepoint] is U+FFFD. | 12 * [replacementCodepoint] is U+FFFD. |
| 16 */ | 13 */ |
| 17 IterableUtf16Decoder decodeUtf16AsIterable(List<int> bytes, [int offset = 0, | 14 IterableUtf16Decoder decodeUtf16AsIterable(List<int> bytes, [int offset = 0, |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 210 |
| 214 /** | 211 /** |
| 215 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type | 212 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type |
| 216 * 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 |
| 217 * 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.) |
| 218 */ | 215 */ |
| 219 class IterableUtf16Decoder implements Iterable<int> { | 216 class IterableUtf16Decoder implements Iterable<int> { |
| 220 final Function codeunitsProvider; | 217 final Function codeunitsProvider; |
| 221 final int replacementCodepoint; | 218 final int replacementCodepoint; |
| 222 | 219 |
| 223 IterableUtf16Decoder._(ListRangeIterator<int> this.codeunitsProvider(), | 220 IterableUtf16Decoder._(_ListRangeIterator<int> this.codeunitsProvider(), |
| 224 int this.replacementCodepoint); | 221 int this.replacementCodepoint); |
| 225 | 222 |
| 226 Utf16CodeUnitDecoder iterator() => | 223 Utf16CodeUnitDecoder iterator() => |
| 227 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), | 224 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), |
| 228 replacementCodepoint); | 225 replacementCodepoint); |
| 229 } | 226 } |
| 230 | 227 |
| 231 /** | 228 /** |
| 232 * 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 |
| 233 * 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 |
| 234 * endian-ness, and defaults to BE. | 231 * endian-ness, and defaults to BE. |
| 235 */ | 232 */ |
| 236 class Utf16BytesToCodeUnitsDecoder implements ListRangeIterator<int> { | 233 class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator<int> { |
| 237 final ListRangeIterator<int> utf16EncodedBytesIterator; | 234 final _ListRangeIterator<int> utf16EncodedBytesIterator; |
| 238 final int replacementCodepoint; | 235 final int replacementCodepoint; |
| 239 | 236 |
| 240 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( | 237 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( |
| 241 ListRangeIterator<int> this.utf16EncodedBytesIterator, | 238 _ListRangeIterator<int> this.utf16EncodedBytesIterator, |
| 242 int this.replacementCodepoint); | 239 int this.replacementCodepoint); |
| 243 | 240 |
| 244 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | 241 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 245 int offset = 0, int length, | 242 int offset = 0, int length, |
| 246 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 243 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 247 if (length == null) { | 244 if (length == null) { |
| 248 length = utf16EncodedBytes.length - offset; | 245 length = utf16EncodedBytes.length - offset; |
| 249 } | 246 } |
| 250 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { | 247 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { |
| 251 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | 248 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 } | 308 } |
| 312 | 309 |
| 313 /** | 310 /** |
| 314 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes | 311 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes |
| 315 * to produce the code unit (0-(2^16)-1). | 312 * to produce the code unit (0-(2^16)-1). |
| 316 */ | 313 */ |
| 317 class Utf16beBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { | 314 class Utf16beBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { |
| 318 Utf16beBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | 315 Utf16beBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 319 int offset = 0, int length, bool stripBom = true, | 316 int offset = 0, int length, bool stripBom = true, |
| 320 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 317 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 321 super._fromListRangeIterator((new ListRange(utf16EncodedBytes, offset, | 318 super._fromListRangeIterator((new _ListRange(utf16EncodedBytes, offset, |
| 322 length)).iterator(), replacementCodepoint) { | 319 length)).iterator(), replacementCodepoint) { |
| 323 if (stripBom && hasUtf16beBom(utf16EncodedBytes, offset, length)) { | 320 if (stripBom && hasUtf16beBom(utf16EncodedBytes, offset, length)) { |
| 324 skip(); | 321 skip(); |
| 325 } | 322 } |
| 326 } | 323 } |
| 327 | 324 |
| 328 int decode() { | 325 int decode() { |
| 329 int hi = utf16EncodedBytesIterator.next(); | 326 int hi = utf16EncodedBytesIterator.next(); |
| 330 int lo = utf16EncodedBytesIterator.next(); | 327 int lo = utf16EncodedBytesIterator.next(); |
| 331 return (hi << 8) + lo; | 328 return (hi << 8) + lo; |
| 332 } | 329 } |
| 333 } | 330 } |
| 334 | 331 |
| 335 /** | 332 /** |
| 336 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes | 333 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes |
| 337 * to produce the code unit (0-(2^16)-1). | 334 * to produce the code unit (0-(2^16)-1). |
| 338 */ | 335 */ |
| 339 class Utf16leBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { | 336 class Utf16leBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { |
| 340 Utf16leBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | 337 Utf16leBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 341 int offset = 0, int length, bool stripBom = true, | 338 int offset = 0, int length, bool stripBom = true, |
| 342 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 339 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 343 super._fromListRangeIterator((new ListRange(utf16EncodedBytes, offset, | 340 super._fromListRangeIterator((new _ListRange(utf16EncodedBytes, offset, |
| 344 length)).iterator(), replacementCodepoint) { | 341 length)).iterator(), replacementCodepoint) { |
| 345 if (stripBom && hasUtf16leBom(utf16EncodedBytes, offset, length)) { | 342 if (stripBom && hasUtf16leBom(utf16EncodedBytes, offset, length)) { |
| 346 skip(); | 343 skip(); |
| 347 } | 344 } |
| 348 } | 345 } |
| 349 | 346 |
| 350 int decode() { | 347 int decode() { |
| 351 int lo = utf16EncodedBytesIterator.next(); | 348 int lo = utf16EncodedBytesIterator.next(); |
| 352 int hi = utf16EncodedBytesIterator.next(); | 349 int hi = utf16EncodedBytesIterator.next(); |
| 353 return (hi << 8) + lo; | 350 return (hi << 8) + lo; |
| 354 } | 351 } |
| 355 } | 352 } |
| OLD | NEW |