| 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("utf32"); | |
| 6 #import("unicode_core.dart"); | |
| 7 #import("unicode.dart"); | |
| 8 | |
| 9 /** | 5 /** |
| 10 * Decodes the UTF-32 bytes as an iterable. Thus, the consumer can only convert | 6 * Decodes the UTF-32 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, | 7 * 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. | 8 * or uses big-endian as a default. This method always strips a leading BOM. |
| 13 * Set the replacementCharacter to null to throw an IllegalArgumentException | 9 * Set the replacementCharacter to null to throw an IllegalArgumentException |
| 14 * rather than replace the bad value. | 10 * rather than replace the bad value. |
| 15 */ | 11 */ |
| 16 IterableUtf32Decoder decodeUtf32AsIterable(List<int> bytes, [ | 12 IterableUtf32Decoder decodeUtf32AsIterable(List<int> bytes, [ |
| 17 int offset = 0, int length, | 13 int offset = 0, int length, |
| 18 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 14 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 final Function codeunitsProvider; | 182 final Function codeunitsProvider; |
| 187 | 183 |
| 188 IterableUtf32Decoder._(Function this.codeunitsProvider); | 184 IterableUtf32Decoder._(Function this.codeunitsProvider); |
| 189 | 185 |
| 190 Utf32BytesDecoder iterator() => codeunitsProvider(); | 186 Utf32BytesDecoder iterator() => codeunitsProvider(); |
| 191 } | 187 } |
| 192 | 188 |
| 193 /** | 189 /** |
| 194 * Abstrace parent class converts encoded bytes to codepoints. | 190 * Abstrace parent class converts encoded bytes to codepoints. |
| 195 */ | 191 */ |
| 196 class Utf32BytesDecoder implements ListRangeIterator<int> { | 192 class Utf32BytesDecoder implements _ListRangeIterator<int> { |
| 197 final ListRangeIterator<int> utf32EncodedBytesIterator; | 193 final _ListRangeIterator<int> utf32EncodedBytesIterator; |
| 198 final int replacementCodepoint; | 194 final int replacementCodepoint; |
| 199 | 195 |
| 200 Utf32BytesDecoder._fromListRangeIterator( | 196 Utf32BytesDecoder._fromListRangeIterator( |
| 201 ListRangeIterator<int> this.utf32EncodedBytesIterator, | 197 _ListRangeIterator<int> this.utf32EncodedBytesIterator, |
| 202 int this.replacementCodepoint); | 198 int this.replacementCodepoint); |
| 203 | 199 |
| 204 factory Utf32BytesDecoder(List<int> utf32EncodedBytes, [ | 200 factory Utf32BytesDecoder(List<int> utf32EncodedBytes, [ |
| 205 int offset = 0, int length, | 201 int offset = 0, int length, |
| 206 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 202 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 207 if (length == null) { | 203 if (length == null) { |
| 208 length = utf32EncodedBytes.length - offset; | 204 length = utf32EncodedBytes.length - offset; |
| 209 } | 205 } |
| 210 if (hasUtf32beBom(utf32EncodedBytes, offset, length)) { | 206 if (hasUtf32beBom(utf32EncodedBytes, offset, length)) { |
| 211 return new Utf32beBytesDecoder(utf32EncodedBytes, offset + 4, length - 4, | 207 return new Utf32beBytesDecoder(utf32EncodedBytes, offset + 4, length - 4, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 264 } |
| 269 | 265 |
| 270 /** | 266 /** |
| 271 * Convert UTF-32BE encoded bytes to codepoints by grouping 4 bytes | 267 * Convert UTF-32BE encoded bytes to codepoints by grouping 4 bytes |
| 272 * to produce the unicode codepoint. | 268 * to produce the unicode codepoint. |
| 273 */ | 269 */ |
| 274 class Utf32beBytesDecoder extends Utf32BytesDecoder { | 270 class Utf32beBytesDecoder extends Utf32BytesDecoder { |
| 275 Utf32beBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, | 271 Utf32beBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, |
| 276 int length, bool stripBom = true, | 272 int length, bool stripBom = true, |
| 277 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 273 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 278 super._fromListRangeIterator((new ListRange(utf32EncodedBytes, offset, | 274 super._fromListRangeIterator((new _ListRange(utf32EncodedBytes, offset, |
| 279 length)).iterator(), replacementCodepoint) { | 275 length)).iterator(), replacementCodepoint) { |
| 280 if (stripBom && hasUtf32beBom(utf32EncodedBytes, offset, length)) { | 276 if (stripBom && hasUtf32beBom(utf32EncodedBytes, offset, length)) { |
| 281 skip(); | 277 skip(); |
| 282 } | 278 } |
| 283 } | 279 } |
| 284 | 280 |
| 285 int decode() { | 281 int decode() { |
| 286 int value = utf32EncodedBytesIterator.next(); | 282 int value = utf32EncodedBytesIterator.next(); |
| 287 value = (value << 8) + utf32EncodedBytesIterator.next(); | 283 value = (value << 8) + utf32EncodedBytesIterator.next(); |
| 288 value = (value << 8) + utf32EncodedBytesIterator.next(); | 284 value = (value << 8) + utf32EncodedBytesIterator.next(); |
| 289 value = (value << 8) + utf32EncodedBytesIterator.next(); | 285 value = (value << 8) + utf32EncodedBytesIterator.next(); |
| 290 return value; | 286 return value; |
| 291 } | 287 } |
| 292 } | 288 } |
| 293 | 289 |
| 294 /** | 290 /** |
| 295 * Convert UTF-32BE encoded bytes to codepoints by grouping 4 bytes | 291 * Convert UTF-32BE encoded bytes to codepoints by grouping 4 bytes |
| 296 * to produce the unicode codepoint. | 292 * to produce the unicode codepoint. |
| 297 */ | 293 */ |
| 298 class Utf32leBytesDecoder extends Utf32BytesDecoder { | 294 class Utf32leBytesDecoder extends Utf32BytesDecoder { |
| 299 Utf32leBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, | 295 Utf32leBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, |
| 300 int length, bool stripBom = true, | 296 int length, bool stripBom = true, |
| 301 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 297 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 302 super._fromListRangeIterator((new ListRange(utf32EncodedBytes, offset, | 298 super._fromListRangeIterator((new _ListRange(utf32EncodedBytes, offset, |
| 303 length)).iterator(), replacementCodepoint) { | 299 length)).iterator(), replacementCodepoint) { |
| 304 if (stripBom && hasUtf32leBom(utf32EncodedBytes, offset, length)) { | 300 if (stripBom && hasUtf32leBom(utf32EncodedBytes, offset, length)) { |
| 305 skip(); | 301 skip(); |
| 306 } | 302 } |
| 307 } | 303 } |
| 308 | 304 |
| 309 int decode() { | 305 int decode() { |
| 310 int value = (utf32EncodedBytesIterator.next()); | 306 int value = (utf32EncodedBytesIterator.next()); |
| 311 value += (utf32EncodedBytesIterator.next() << 8); | 307 value += (utf32EncodedBytesIterator.next() << 8); |
| 312 value += (utf32EncodedBytesIterator.next() << 16); | 308 value += (utf32EncodedBytesIterator.next() << 16); |
| 313 value += (utf32EncodedBytesIterator.next() << 24); | 309 value += (utf32EncodedBytesIterator.next() << 24); |
| 314 return value; | 310 return value; |
| 315 } | 311 } |
| 316 } | 312 } |
| 317 | 313 |
| 318 bool _validCodepoint(int codepoint) { | 314 bool _validCodepoint(int codepoint) { |
| 319 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || | 315 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || |
| 320 (codepoint > UNICODE_UTF16_RESERVED_HI && | 316 (codepoint > UNICODE_UTF16_RESERVED_HI && |
| 321 codepoint < UNICODE_VALID_RANGE_MAX); | 317 codepoint < UNICODE_VALID_RANGE_MAX); |
| 322 } | 318 } |
| OLD | NEW |