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 utf.utf_16_code_unit_decoder; | 5 library utf.utf_16_code_unit_decoder; |
6 | 6 |
7 import 'constants.dart'; | 7 import 'constants.dart'; |
8 import 'list_range.dart'; | 8 import 'list_range.dart'; |
9 | 9 |
10 /** | 10 /** |
11 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. | 11 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. |
12 * The parameters can override the default Unicode replacement character. Set | 12 * The parameters can override the default Unicode replacement character. Set |
13 * the replacementCharacter to null to throw an ArgumentError | 13 * the replacementCharacter to null to throw an ArgumentError |
14 * rather than replace the bad value. | 14 * rather than replace the bad value. |
15 */ | 15 */ |
16 class Utf16CodeUnitDecoder implements Iterator<int> { | 16 class Utf16CodeUnitDecoder implements Iterator<int> { |
17 // TODO(kevmoo): should this field be private? | 17 // TODO(kevmoo): should this field be private? |
18 final ListRangeIterator utf16CodeUnitIterator; | 18 final ListRangeIterator utf16CodeUnitIterator; |
19 final int replacementCodepoint; | 19 final int replacementCodepoint; |
20 int _current = null; | 20 int _current = null; |
21 | 21 |
22 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, | 22 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, |
23 int this.replacementCodepoint = | 23 [int offset = 0, |
24 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 24 int length, |
25 utf16CodeUnitIterator = | 25 int this.replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) |
26 (new ListRange(utf16CodeUnits, offset, length)).iterator; | 26 : utf16CodeUnitIterator = |
| 27 (new ListRange(utf16CodeUnits, offset, length)).iterator; |
27 | 28 |
28 Utf16CodeUnitDecoder.fromListRangeIterator( | 29 Utf16CodeUnitDecoder.fromListRangeIterator( |
29 ListRangeIterator this.utf16CodeUnitIterator, | 30 ListRangeIterator this.utf16CodeUnitIterator, |
30 int this.replacementCodepoint); | 31 int this.replacementCodepoint); |
31 | 32 |
32 Iterator<int> get iterator => this; | 33 Iterator<int> get iterator => this; |
33 | 34 |
34 int get current => _current; | 35 int get current => _current; |
35 | 36 |
36 bool moveNext() { | 37 bool moveNext() { |
(...skipping 17 matching lines...) Expand all Loading... |
54 // merge surrogate pair | 55 // merge surrogate pair |
55 int nextValue = utf16CodeUnitIterator.current; | 56 int nextValue = utf16CodeUnitIterator.current; |
56 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | 57 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && |
57 nextValue <= UNICODE_UTF16_RESERVED_HI) { | 58 nextValue <= UNICODE_UTF16_RESERVED_HI) { |
58 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; | 59 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; |
59 value += UNICODE_UTF16_OFFSET + | 60 value += UNICODE_UTF16_OFFSET + |
60 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); | 61 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); |
61 _current = value; | 62 _current = value; |
62 } else { | 63 } else { |
63 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && | 64 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && |
64 nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) { | 65 nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) { |
65 utf16CodeUnitIterator.backup(); | 66 utf16CodeUnitIterator.backup(); |
66 } | 67 } |
67 if (replacementCodepoint != null) { | 68 if (replacementCodepoint != null) { |
68 _current = replacementCodepoint; | 69 _current = replacementCodepoint; |
69 } else { | 70 } else { |
70 throw new ArgumentError( | 71 throw new ArgumentError( |
71 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | 72 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); |
72 } | 73 } |
73 } | 74 } |
74 } else if (replacementCodepoint != null) { | 75 } else if (replacementCodepoint != null) { |
75 _current = replacementCodepoint; | 76 _current = replacementCodepoint; |
76 } else { | 77 } else { |
77 throw new ArgumentError( | 78 throw new ArgumentError( |
78 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | 79 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); |
79 } | 80 } |
80 return true; | 81 return true; |
81 } | 82 } |
82 } | 83 } |
83 | |
OLD | NEW |