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 * 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 |
7 * 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, |
8 * 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. |
9 * Set the replacementCharacter to null to throw an IllegalArgumentException | 9 * Set the replacementCharacter to null to throw an ArgumentError |
10 * rather than replace the bad value. | 10 * rather than replace the bad value. |
11 */ | 11 */ |
12 IterableUtf32Decoder decodeUtf32AsIterable(List<int> bytes, [ | 12 IterableUtf32Decoder decodeUtf32AsIterable(List<int> bytes, [ |
13 int offset = 0, int length, | 13 int offset = 0, int length, |
14 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 14 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
15 return new IterableUtf32Decoder._( | 15 return new IterableUtf32Decoder._( |
16 () => new Utf32BytesDecoder(bytes, offset, length, replacementCodepoint)); | 16 () => new Utf32BytesDecoder(bytes, offset, length, replacementCodepoint)); |
17 } | 17 } |
18 | 18 |
19 /** | 19 /** |
20 * Decodes the UTF-32BE bytes as an iterable. Thus, the consumer can only conver
t | 20 * Decodes the UTF-32BE bytes as an iterable. Thus, the consumer can only conver
t |
21 * as much of the input as needed. This method strips a leading BOM by default, | 21 * as much of the input as needed. This method strips a leading BOM by default, |
22 * but can be overridden by setting the optional parameter [stripBom] to false. | 22 * but can be overridden by setting the optional parameter [stripBom] to false. |
23 * Set the replacementCharacter to null to throw an IllegalArgumentException | 23 * Set the replacementCharacter to null to throw an ArgumentError |
24 * rather than replace the bad value. | 24 * rather than replace the bad value. |
25 */ | 25 */ |
26 IterableUtf32Decoder decodeUtf32beAsIterable(List<int> bytes, [ | 26 IterableUtf32Decoder decodeUtf32beAsIterable(List<int> bytes, [ |
27 int offset = 0, int length, bool stripBom = true, | 27 int offset = 0, int length, bool stripBom = true, |
28 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 28 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
29 return new IterableUtf32Decoder._( | 29 return new IterableUtf32Decoder._( |
30 () => new Utf32beBytesDecoder(bytes, offset, length, stripBom, | 30 () => new Utf32beBytesDecoder(bytes, offset, length, stripBom, |
31 replacementCodepoint)); | 31 replacementCodepoint)); |
32 } | 32 } |
33 | 33 |
34 /** | 34 /** |
35 * Decodes the UTF-32LE bytes as an iterable. Thus, the consumer can only conver
t | 35 * Decodes the UTF-32LE bytes as an iterable. Thus, the consumer can only conver
t |
36 * as much of the input as needed. This method strips a leading BOM by default, | 36 * as much of the input as needed. This method strips a leading BOM by default, |
37 * but can be overridden by setting the optional parameter [stripBom] to false. | 37 * but can be overridden by setting the optional parameter [stripBom] to false. |
38 * Set the replacementCharacter to null to throw an IllegalArgumentException | 38 * Set the replacementCharacter to null to throw an ArgumentError |
39 * rather than replace the bad value. | 39 * rather than replace the bad value. |
40 */ | 40 */ |
41 IterableUtf32Decoder decodeUtf32leAsIterable(List<int> bytes, [ | 41 IterableUtf32Decoder decodeUtf32leAsIterable(List<int> bytes, [ |
42 int offset = 0, int length, bool stripBom = true, | 42 int offset = 0, int length, bool stripBom = true, |
43 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 43 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
44 return new IterableUtf32Decoder._( | 44 return new IterableUtf32Decoder._( |
45 () => new Utf32leBytesDecoder(bytes, offset, length, stripBom, | 45 () => new Utf32leBytesDecoder(bytes, offset, length, stripBom, |
46 replacementCodepoint)); | 46 replacementCodepoint)); |
47 } | 47 } |
48 | 48 |
49 /** | 49 /** |
50 * Produce a String from a sequence of UTF-32 encoded bytes. The parameters | 50 * Produce a String from a sequence of UTF-32 encoded bytes. The parameters |
51 * allow an offset into a list of bytes (as int), limiting the length of the | 51 * allow an offset into a list of bytes (as int), limiting the length of the |
52 * values be decoded and the ability of override the default Unicode | 52 * values be decoded and the ability of override the default Unicode |
53 * replacement character. Set the replacementCharacter to null to throw an | 53 * replacement character. Set the replacementCharacter to null to throw an |
54 * IllegalArgumentException rather than replace the bad value. | 54 * ArgumentError rather than replace the bad value. |
55 */ | 55 */ |
56 String decodeUtf32(List<int> bytes, [int offset = 0, int length, | 56 String decodeUtf32(List<int> bytes, [int offset = 0, int length, |
57 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 57 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
58 return codepointsToString((new Utf32BytesDecoder(bytes, offset, length, | 58 return codepointsToString((new Utf32BytesDecoder(bytes, offset, length, |
59 replacementCodepoint)).decodeRest()); | 59 replacementCodepoint)).decodeRest()); |
60 } | 60 } |
61 /** | 61 /** |
62 * Produce a String from a sequence of UTF-32BE encoded bytes. The parameters | 62 * Produce a String from a sequence of UTF-32BE encoded bytes. The parameters |
63 * allow an offset into a list of bytes (as int), limiting the length of the | 63 * allow an offset into a list of bytes (as int), limiting the length of the |
64 * values be decoded and the ability of override the default Unicode | 64 * values be decoded and the ability of override the default Unicode |
65 * replacement character. Set the replacementCharacter to null to throw an | 65 * replacement character. Set the replacementCharacter to null to throw an |
66 * IllegalArgumentException rather than replace the bad value. | 66 * ArgumentError rather than replace the bad value. |
67 */ | 67 */ |
68 String decodeUtf32be( | 68 String decodeUtf32be( |
69 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | 69 List<int> bytes, [int offset = 0, int length, bool stripBom = true, |
70 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | 70 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => |
71 codepointsToString((new Utf32beBytesDecoder(bytes, offset, length, stripBom, | 71 codepointsToString((new Utf32beBytesDecoder(bytes, offset, length, stripBom, |
72 replacementCodepoint)).decodeRest()); | 72 replacementCodepoint)).decodeRest()); |
73 | 73 |
74 /** | 74 /** |
75 * Produce a String from a sequence of UTF-32LE encoded bytes. The parameters | 75 * Produce a String from a sequence of UTF-32LE encoded bytes. The parameters |
76 * allow an offset into a list of bytes (as int), limiting the length of the | 76 * allow an offset into a list of bytes (as int), limiting the length of the |
77 * values be decoded and the ability of override the default Unicode | 77 * values be decoded and the ability of override the default Unicode |
78 * replacement character. Set the replacementCharacter to null to throw an | 78 * replacement character. Set the replacementCharacter to null to throw an |
79 * IllegalArgumentException rather than replace the bad value. | 79 * ArgumentError rather than replace the bad value. |
80 */ | 80 */ |
81 String decodeUtf32le( | 81 String decodeUtf32le( |
82 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | 82 List<int> bytes, [int offset = 0, int length, bool stripBom = true, |
83 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | 83 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => |
84 codepointsToString((new Utf32leBytesDecoder(bytes, offset, length, stripBom, | 84 codepointsToString((new Utf32leBytesDecoder(bytes, offset, length, stripBom, |
85 replacementCodepoint)).decodeRest()); | 85 replacementCodepoint)).decodeRest()); |
86 | 86 |
87 /** | 87 /** |
88 * Produce a list of UTF-32 encoded bytes. This method prefixes the resulting | 88 * Produce a list of UTF-32 encoded bytes. This method prefixes the resulting |
89 * bytes with a big-endian byte-order-marker. | 89 * bytes with a big-endian byte-order-marker. |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 } | 225 } |
226 | 226 |
227 bool hasNext() => utf32EncodedBytesIterator.hasNext(); | 227 bool hasNext() => utf32EncodedBytesIterator.hasNext(); |
228 | 228 |
229 int next() { | 229 int next() { |
230 if (utf32EncodedBytesIterator.remaining < 4) { | 230 if (utf32EncodedBytesIterator.remaining < 4) { |
231 utf32EncodedBytesIterator.skip(utf32EncodedBytesIterator.remaining); | 231 utf32EncodedBytesIterator.skip(utf32EncodedBytesIterator.remaining); |
232 if (replacementCodepoint != null) { | 232 if (replacementCodepoint != null) { |
233 return replacementCodepoint; | 233 return replacementCodepoint; |
234 } else { | 234 } else { |
235 throw new IllegalArgumentException( | 235 throw new ArgumentError( |
236 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); | 236 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); |
237 } | 237 } |
238 } else { | 238 } else { |
239 int codepoint = decode(); | 239 int codepoint = decode(); |
240 if (_validCodepoint(codepoint)) { | 240 if (_validCodepoint(codepoint)) { |
241 return codepoint; | 241 return codepoint; |
242 } else if (replacementCodepoint != null) { | 242 } else if (replacementCodepoint != null) { |
243 return replacementCodepoint; | 243 return replacementCodepoint; |
244 } else { | 244 } else { |
245 throw new IllegalArgumentException( | 245 throw new ArgumentError( |
246 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); | 246 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); |
247 } | 247 } |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 int get position => utf32EncodedBytesIterator.position ~/ 4; | 251 int get position => utf32EncodedBytesIterator.position ~/ 4; |
252 | 252 |
253 void backup([int by = 1]) { | 253 void backup([int by = 1]) { |
254 utf32EncodedBytesIterator.backup(4 * by); | 254 utf32EncodedBytesIterator.backup(4 * by); |
255 } | 255 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 value += (utf32EncodedBytesIterator.next() << 24); | 309 value += (utf32EncodedBytesIterator.next() << 24); |
310 return value; | 310 return value; |
311 } | 311 } |
312 } | 312 } |
313 | 313 |
314 bool _validCodepoint(int codepoint) { | 314 bool _validCodepoint(int codepoint) { |
315 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || | 315 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || |
316 (codepoint > UNICODE_UTF16_RESERVED_HI && | 316 (codepoint > UNICODE_UTF16_RESERVED_HI && |
317 codepoint < UNICODE_VALID_RANGE_MAX); | 317 codepoint < UNICODE_VALID_RANGE_MAX); |
318 } | 318 } |
OLD | NEW |