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 * Provide a list of Unicode codepoints for a given string. | 6 * Provide a list of Unicode codepoints for a given string. |
7 */ | 7 */ |
8 List<int> stringToCodepoints(String str) { | 8 List<int> stringToCodepoints(String str) { |
9 List<int> codepoints; | 9 List<int> codepoints; |
10 // TODO _is16BitCodeUnit() is used to work around a bug with dart2js | 10 // TODO _is16BitCodeUnit() is used to work around a bug with dart2js |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 */ | 117 */ |
118 List<int> _utf16CodeUnitsToCodepoints( | 118 List<int> _utf16CodeUnitsToCodepoints( |
119 List<int> utf16CodeUnits, [int offset = 0, int length, | 119 List<int> utf16CodeUnits, [int offset = 0, int length, |
120 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 120 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
121 _ListRangeIterator source = | 121 _ListRangeIterator source = |
122 (new _ListRange(utf16CodeUnits, offset, length)).iterator(); | 122 (new _ListRange(utf16CodeUnits, offset, length)).iterator(); |
123 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder | 123 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder |
124 .fromListRangeIterator(source, replacementCodepoint); | 124 .fromListRangeIterator(source, replacementCodepoint); |
125 List<int> codepoints = new List<int>(source.remaining); | 125 List<int> codepoints = new List<int>(source.remaining); |
126 int i = 0; | 126 int i = 0; |
127 while (decoder.hasNext()) { | 127 while (decoder.hasNext) { |
128 codepoints[i++] = decoder.next(); | 128 codepoints[i++] = decoder.next(); |
129 } | 129 } |
130 if (i == codepoints.length) { | 130 if (i == codepoints.length) { |
131 return codepoints; | 131 return codepoints; |
132 } else { | 132 } else { |
133 List<int> codepointTrunc = new List<int>(i); | 133 List<int> codepointTrunc = new List<int>(i); |
134 codepointTrunc.setRange(0, i, codepoints); | 134 codepointTrunc.setRange(0, i, codepoints); |
135 return codepointTrunc; | 135 return codepointTrunc; |
136 } | 136 } |
137 } | 137 } |
(...skipping 13 matching lines...) Expand all Loading... |
151 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 151 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
152 utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length)) | 152 utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length)) |
153 .iterator(); | 153 .iterator(); |
154 | 154 |
155 Utf16CodeUnitDecoder.fromListRangeIterator( | 155 Utf16CodeUnitDecoder.fromListRangeIterator( |
156 _ListRangeIterator this.utf16CodeUnitIterator, | 156 _ListRangeIterator this.utf16CodeUnitIterator, |
157 int this.replacementCodepoint); | 157 int this.replacementCodepoint); |
158 | 158 |
159 Iterator<int> iterator() => this; | 159 Iterator<int> iterator() => this; |
160 | 160 |
161 bool hasNext() => utf16CodeUnitIterator.hasNext(); | 161 bool get hasNext => utf16CodeUnitIterator.hasNext; |
162 | 162 |
163 int next() { | 163 int next() { |
164 int value = utf16CodeUnitIterator.next(); | 164 int value = utf16CodeUnitIterator.next(); |
165 if (value < 0) { | 165 if (value < 0) { |
166 if (replacementCodepoint != null) { | 166 if (replacementCodepoint != null) { |
167 return replacementCodepoint; | 167 return replacementCodepoint; |
168 } else { | 168 } else { |
169 throw new ArgumentError( | 169 throw new ArgumentError( |
170 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | 170 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); |
171 } | 171 } |
172 } else if (value < UNICODE_UTF16_RESERVED_LO || | 172 } else if (value < UNICODE_UTF16_RESERVED_LO || |
173 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 173 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
174 // transfer directly | 174 // transfer directly |
175 return value; | 175 return value; |
176 } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | 176 } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE && |
177 utf16CodeUnitIterator.hasNext()) { | 177 utf16CodeUnitIterator.hasNext) { |
178 // merge surrogate pair | 178 // merge surrogate pair |
179 int nextValue = utf16CodeUnitIterator.next(); | 179 int nextValue = utf16CodeUnitIterator.next(); |
180 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | 180 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && |
181 nextValue <= UNICODE_UTF16_RESERVED_HI) { | 181 nextValue <= UNICODE_UTF16_RESERVED_HI) { |
182 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; | 182 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; |
183 value += UNICODE_UTF16_OFFSET + | 183 value += UNICODE_UTF16_OFFSET + |
184 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); | 184 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); |
185 return value; | 185 return value; |
186 } else { | 186 } else { |
187 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && | 187 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 234 |
235 int get length => _length; | 235 int get length => _length; |
236 } | 236 } |
237 | 237 |
238 /** | 238 /** |
239 * The _ListRangeIterator provides more capabilities than a standard iterator, | 239 * The _ListRangeIterator provides more capabilities than a standard iterator, |
240 * including the ability to get the current position, count remaining items, | 240 * including the ability to get the current position, count remaining items, |
241 * and move forward/backward within the iterator. | 241 * and move forward/backward within the iterator. |
242 */ | 242 */ |
243 abstract class _ListRangeIterator implements Iterator<int> { | 243 abstract class _ListRangeIterator implements Iterator<int> { |
244 bool hasNext(); | 244 bool hasNext; |
245 int next(); | 245 int next(); |
246 int get position; | 246 int get position; |
247 void backup([by]); | 247 void backup([by]); |
248 int get remaining; | 248 int get remaining; |
249 void skip([count]); | 249 void skip([count]); |
250 } | 250 } |
251 | 251 |
252 class _ListRangeIteratorImpl implements _ListRangeIterator { | 252 class _ListRangeIteratorImpl implements _ListRangeIterator { |
253 final List<int> _source; | 253 final List<int> _source; |
254 int _offset; | 254 int _offset; |
255 final int _end; | 255 final int _end; |
256 | 256 |
257 _ListRangeIteratorImpl(this._source, this._offset, this._end); | 257 _ListRangeIteratorImpl(this._source, this._offset, this._end); |
258 | 258 |
259 bool hasNext() => _offset < _end; | 259 bool get hasNext => _offset < _end; |
260 | 260 |
261 int next() => _source[_offset++]; | 261 int next() => _source[_offset++]; |
262 | 262 |
263 int get position => _offset; | 263 int get position => _offset; |
264 | 264 |
265 void backup([int by = 1]) { | 265 void backup([int by = 1]) { |
266 _offset -= by; | 266 _offset -= by; |
267 } | 267 } |
268 | 268 |
269 int get remaining => _end - _offset; | 269 int get remaining => _end - _offset; |
270 | 270 |
271 void skip([int count = 1]) { | 271 void skip([int count = 1]) { |
272 _offset += count; | 272 _offset += count; |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
OLD | NEW |