| 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 frog/dartc | 10 // TODO _is16BitCodeUnit() is used to work around a bug with frog/dartc |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 final int UNICODE_UTF16_HI_MASK = 0xffc00; | 69 final int UNICODE_UTF16_HI_MASK = 0xffc00; |
| 70 final int UNICODE_UTF16_LO_MASK = 0x3ff; | 70 final int UNICODE_UTF16_LO_MASK = 0x3ff; |
| 71 | 71 |
| 72 /** | 72 /** |
| 73 * Encode code points as UTF16 code units. | 73 * Encode code points as UTF16 code units. |
| 74 */ | 74 */ |
| 75 List<int> _codepointsToUtf16CodeUnits( | 75 List<int> _codepointsToUtf16CodeUnits( |
| 76 List<int> codepoints, [int offset = 0, int length, | 76 List<int> codepoints, [int offset = 0, int length, |
| 77 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 77 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 78 | 78 |
| 79 _ListRange<int> listRange = new _ListRange<int>(codepoints, offset, length); | 79 _ListRange listRange = new _ListRange(codepoints, offset, length); |
| 80 int encodedLength = 0; | 80 int encodedLength = 0; |
| 81 for (int value in listRange) { | 81 for (int value in listRange) { |
| 82 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | 82 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || |
| 83 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 83 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
| 84 encodedLength++; | 84 encodedLength++; |
| 85 } else if (value > UNICODE_PLANE_ONE_MAX && | 85 } else if (value > UNICODE_PLANE_ONE_MAX && |
| 86 value <= UNICODE_VALID_RANGE_MAX) { | 86 value <= UNICODE_VALID_RANGE_MAX) { |
| 87 encodedLength += 2; | 87 encodedLength += 2; |
| 88 } else { | 88 } else { |
| 89 encodedLength++; | 89 encodedLength++; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 111 } | 111 } |
| 112 return codeUnitsBuffer; | 112 return codeUnitsBuffer; |
| 113 } | 113 } |
| 114 | 114 |
| 115 /** | 115 /** |
| 116 * Decodes the utf16 codeunits to codepoints. | 116 * Decodes the utf16 codeunits to codepoints. |
| 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<int> source = | 121 _ListRangeIterator source = |
| 122 (new _ListRange<int>(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 } |
| 138 | 138 |
| 139 /** | 139 /** |
| 140 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. | 140 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. |
| 141 * The parameters can override the default Unicode replacement character. Set | 141 * The parameters can override the default Unicode replacement character. Set |
| 142 * the replacementCharacter to null to throw an IllegalArgumentException | 142 * the replacementCharacter to null to throw an IllegalArgumentException |
| 143 * rather than replace the bad value. | 143 * rather than replace the bad value. |
| 144 */ | 144 */ |
| 145 class Utf16CodeUnitDecoder implements Iterator<int> { | 145 class Utf16CodeUnitDecoder implements Iterator<int> { |
| 146 final _ListRangeIterator<int> utf16CodeUnitIterator; | 146 final _ListRangeIterator utf16CodeUnitIterator; |
| 147 final int replacementCodepoint; | 147 final int replacementCodepoint; |
| 148 | 148 |
| 149 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, | 149 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, |
| 150 int this.replacementCodepoint = | 150 int this.replacementCodepoint = |
| 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<int> 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 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) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | 202 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 /** | 207 /** |
| 208 * _ListRange in an internal type used to create a lightweight Interable on a | 208 * _ListRange in an internal type used to create a lightweight Interable on a |
| 209 * range within a source list. DO NOT MODIFY the underlying list while | 209 * range within a source list. DO NOT MODIFY the underlying list while |
| 210 * iterating over it. The results of doing so are undefined. | 210 * iterating over it. The results of doing so are undefined. |
| 211 */ | 211 */ |
| 212 class _ListRange<T> implements Iterable<T> { | 212 class _ListRange implements Iterable { |
| 213 final List<T> _source; | 213 final List _source; |
| 214 final int _offset; | 214 final int _offset; |
| 215 final int _length; | 215 final int _length; |
| 216 | 216 |
| 217 _ListRange(List<T> source, [int offset = 0, int length]) : | 217 _ListRange(source, [offset = 0, length]) : |
| 218 this._source = source, this._offset = offset, | 218 this._source = source, |
| 219 this._offset = offset, |
| 219 this._length = (length == null ? source.length - offset : length) { | 220 this._length = (length == null ? source.length - offset : length) { |
| 220 if (_offset < 0 || _offset > _source.length) { | 221 if (_offset < 0 || _offset > _source.length) { |
| 221 throw new IndexOutOfRangeException("offset out of range (< 0)"); | 222 throw new IndexOutOfRangeException("offset out of range (< 0)"); |
| 222 } | 223 } |
| 223 if (_length != null && (_length < 0)) { | 224 if (_length != null && (_length < 0)) { |
| 224 throw new IndexOutOfRangeException("length out of range (< 0)"); | 225 throw new IndexOutOfRangeException("length out of range (< 0)"); |
| 225 } | 226 } |
| 226 if (_length + _offset > _source.length) { | 227 if (_length + _offset > _source.length) { |
| 227 throw new IndexOutOfRangeException("offset + length > source.length"); | 228 throw new IndexOutOfRangeException("offset + length > source.length"); |
| 228 } | 229 } |
| 229 } | 230 } |
| 230 | 231 |
| 231 _ListRangeIterator<T> iterator() => | 232 _ListRangeIterator iterator() => |
| 232 new _ListRangeIteratorImpl(_source, _offset, _offset + _length); | 233 new _ListRangeIteratorImpl(_source, _offset, _offset + _length); |
| 233 | 234 |
| 234 int get length() => _length; | 235 int get length() => _length; |
| 235 } | 236 } |
| 236 | 237 |
| 237 /** | 238 /** |
| 238 * The _ListRangeIterator provides more capabilities than a standard iterator, | 239 * The _ListRangeIterator provides more capabilities than a standard iterator, |
| 239 * including the ability to get the current position, count remaining items, | 240 * including the ability to get the current position, count remaining items, |
| 240 * and move forward/backward within the iterator. | 241 * and move forward/backward within the iterator. |
| 241 */ | 242 */ |
| 242 interface _ListRangeIterator<T> extends Iterator<T> { | 243 interface _ListRangeIterator extends Iterator<int> { |
| 243 bool hasNext(); | 244 bool hasNext(); |
| 244 T next(); | 245 int next(); |
| 245 int get position(); | 246 int get position(); |
| 246 void backup([int by]); | 247 void backup([by]); |
| 247 int get remaining(); | 248 int get remaining(); |
| 248 void skip([int count]); | 249 void skip([count]); |
| 249 } | 250 } |
| 250 | 251 |
| 251 class _ListRangeIteratorImpl<T> implements _ListRangeIterator<T> { | 252 class _ListRangeIteratorImpl implements _ListRangeIterator { |
| 252 final List<T> _source; | 253 final List<int> _source; |
| 253 int _offset; | 254 int _offset; |
| 254 final int _end; | 255 final int _end; |
| 255 | 256 |
| 256 _ListRangeIteratorImpl(List<T> source, int offset, int end) : | 257 _ListRangeIteratorImpl(this._source, this._offset, this._end); |
| 257 _source = source, _offset = offset, _end = end; | |
| 258 | 258 |
| 259 bool hasNext() => _offset < _end; | 259 bool hasNext() => _offset < _end; |
| 260 T next() => _source[_offset++]; | 260 |
| 261 int next() => _source[_offset++]; |
| 262 |
| 261 int get position() => _offset; | 263 int get position() => _offset; |
| 264 |
| 262 void backup([int by = 1]) { | 265 void backup([int by = 1]) { |
| 263 _offset -= by; | 266 _offset -= by; |
| 264 } | 267 } |
| 268 |
| 265 int get remaining() => _end - _offset; | 269 int get remaining() => _end - _offset; |
| 270 |
| 266 void skip([int count = 1]) { | 271 void skip([int count = 1]) { |
| 267 _offset += count; | 272 _offset += count; |
| 268 } | 273 } |
| 269 } | 274 } |
| 270 | 275 |
| OLD | NEW |