| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library("unicode_core"); | |
| 6 | |
| 7 /* | |
| 8 * Test for presence of bug related to the use of UTF-16 code units for | |
| 9 * Dart compiled to JS. | |
| 10 */ | |
| 11 bool _test16BitCodeUnit = null; | |
| 12 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | |
| 13 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | |
| 14 // removing after this issue is resolved. | |
| 15 bool is16BitCodeUnit() { | |
| 16 if (_test16BitCodeUnit == null) { | |
| 17 _test16BitCodeUnit = (new String.fromCharCodes([0x1D11E])) == | |
| 18 (new String.fromCharCodes([0xD11E])); | |
| 19 } | |
| 20 return _test16BitCodeUnit; | |
| 21 } | |
| 22 | |
| 23 /** | |
| 24 * Invalid codepoints or encodings may be substituted with the value U+fffd. | |
| 25 */ | |
| 26 final int UNICODE_REPLACEMENT_CHARACTER_CODEPOINT = 0xfffd; | |
| 27 final int UNICODE_BOM = 0xfeff; | |
| 28 final int UNICODE_UTF_BOM_LO = 0xff; | |
| 29 final int UNICODE_UTF_BOM_HI = 0xfe; | |
| 30 | |
| 31 final int UNICODE_BYTE_ZERO_MASK = 0xff; | |
| 32 final int UNICODE_BYTE_ONE_MASK = 0xff00; | |
| 33 final int UNICODE_VALID_RANGE_MAX = 0x10ffff; | |
| 34 final int UNICODE_PLANE_ONE_MAX = 0xffff; | |
| 35 final int UNICODE_UTF16_RESERVED_LO = 0xd800; | |
| 36 final int UNICODE_UTF16_RESERVED_HI = 0xdfff; | |
| 37 final int UNICODE_UTF16_OFFSET = 0x10000; | |
| 38 final int UNICODE_UTF16_SURROGATE_UNIT_0_BASE = 0xd800; | |
| 39 final int UNICODE_UTF16_SURROGATE_UNIT_1_BASE = 0xdc00; | |
| 40 final int UNICODE_UTF16_HI_MASK = 0xffc00; | |
| 41 final int UNICODE_UTF16_LO_MASK = 0x3ff; | |
| 42 | |
| 43 /** | |
| 44 * Encode code points as UTF16 code units. | |
| 45 */ | |
| 46 List<int> codepointsToUtf16CodeUnits( | |
| 47 List<int> codepoints, [int offset = 0, int length, | |
| 48 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 49 | |
| 50 ListRange<int> listRange = new ListRange<int>(codepoints, offset, length); | |
| 51 int encodedLength = 0; | |
| 52 for (int value in listRange) { | |
| 53 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | |
| 54 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | |
| 55 encodedLength++; | |
| 56 } else if (value > UNICODE_PLANE_ONE_MAX && | |
| 57 value <= UNICODE_VALID_RANGE_MAX) { | |
| 58 encodedLength += 2; | |
| 59 } else { | |
| 60 encodedLength++; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 List<int> codeUnitsBuffer = new List<int>(encodedLength); | |
| 65 int j = 0; | |
| 66 for (int value in listRange) { | |
| 67 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | |
| 68 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | |
| 69 codeUnitsBuffer[j++] = value; | |
| 70 } else if (value > UNICODE_PLANE_ONE_MAX && | |
| 71 value <= UNICODE_VALID_RANGE_MAX) { | |
| 72 int base = value - UNICODE_UTF16_OFFSET; | |
| 73 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_0_BASE + | |
| 74 ((base & UNICODE_UTF16_HI_MASK) >> 10); | |
| 75 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_1_BASE + | |
| 76 (base & UNICODE_UTF16_LO_MASK); | |
| 77 } else if (replacementCodepoint != null) { | |
| 78 codeUnitsBuffer[j++] = replacementCodepoint; | |
| 79 } else { | |
| 80 throw new IllegalArgumentException("Invalid encoding"); | |
| 81 } | |
| 82 } | |
| 83 return codeUnitsBuffer; | |
| 84 } | |
| 85 | |
| 86 /** | |
| 87 * Decodes the utf16 codeunits to codepoints. | |
| 88 */ | |
| 89 List<int> utf16CodeUnitsToCodepoints( | |
| 90 List<int> utf16CodeUnits, [int offset = 0, int length, | |
| 91 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 92 ListRangeIterator<int> source = | |
| 93 (new ListRange<int>(utf16CodeUnits, offset, length)).iterator(); | |
| 94 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder | |
| 95 .fromListRangeIterator(source, replacementCodepoint); | |
| 96 List<int> codepoints = new List<int>(source.remaining); | |
| 97 int i = 0; | |
| 98 while (decoder.hasNext()) { | |
| 99 codepoints[i++] = decoder.next(); | |
| 100 } | |
| 101 if (i == codepoints.length) { | |
| 102 return codepoints; | |
| 103 } else { | |
| 104 List<int> codepointTrunc = new List<int>(i); | |
| 105 codepointTrunc.setRange(0, i, codepoints); | |
| 106 return codepointTrunc; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. | |
| 112 * The parameters can override the default Unicode replacement character. Set | |
| 113 * the replacementCharacter to null to throw an IllegalArgumentException | |
| 114 * rather than replace the bad value. | |
| 115 */ | |
| 116 class Utf16CodeUnitDecoder implements Iterator<int> { | |
| 117 final ListRangeIterator<int> utf16CodeUnitIterator; | |
| 118 final int replacementCodepoint; | |
| 119 | |
| 120 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, | |
| 121 int this.replacementCodepoint = | |
| 122 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | |
| 123 utf16CodeUnitIterator = (new ListRange(utf16CodeUnits, offset, length)) | |
| 124 .iterator(); | |
| 125 | |
| 126 Utf16CodeUnitDecoder.fromListRangeIterator( | |
| 127 ListRangeIterator<int> this.utf16CodeUnitIterator, | |
| 128 int this.replacementCodepoint); | |
| 129 | |
| 130 Iterator<int> iterator() => this; | |
| 131 | |
| 132 bool hasNext() => utf16CodeUnitIterator.hasNext(); | |
| 133 | |
| 134 int next() { | |
| 135 int value = utf16CodeUnitIterator.next(); | |
| 136 if (value < 0) { | |
| 137 if (replacementCodepoint != null) { | |
| 138 return replacementCodepoint; | |
| 139 } else { | |
| 140 throw new IllegalArgumentException( | |
| 141 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | |
| 142 } | |
| 143 } else if (value < UNICODE_UTF16_RESERVED_LO || | |
| 144 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | |
| 145 // transfer directly | |
| 146 return value; | |
| 147 } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | |
| 148 utf16CodeUnitIterator.hasNext()) { | |
| 149 // merge surrogate pair | |
| 150 int nextValue = utf16CodeUnitIterator.next(); | |
| 151 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | |
| 152 nextValue <= UNICODE_UTF16_RESERVED_HI) { | |
| 153 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; | |
| 154 value += UNICODE_UTF16_OFFSET + | |
| 155 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); | |
| 156 return value; | |
| 157 } else { | |
| 158 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && | |
| 159 nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) { | |
| 160 utf16CodeUnitIterator.backup(); | |
| 161 } | |
| 162 if (replacementCodepoint != null) { | |
| 163 return replacementCodepoint; | |
| 164 } else { | |
| 165 throw new IllegalArgumentException( | |
| 166 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | |
| 167 } | |
| 168 } | |
| 169 } else if (replacementCodepoint != null) { | |
| 170 return replacementCodepoint; | |
| 171 } else { | |
| 172 throw new IllegalArgumentException( | |
| 173 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 /** | |
| 179 * ListRange in an internal type used to create a lightweight Interable on a | |
| 180 * range within a source list. DO NOT MODIFY the underlying list while | |
| 181 * iterating over it. The results of doing so are undefined. | |
| 182 */ | |
| 183 class ListRange<T> implements Iterable<T> { | |
| 184 final List<T> _source; | |
| 185 final int _offset; | |
| 186 final int _length; | |
| 187 | |
| 188 ListRange(List<T> source, [int offset = 0, int length]) : | |
| 189 this._source = source, this._offset = offset, | |
| 190 this._length = (length == null ? source.length - offset : length) { | |
| 191 if (_offset < 0 || _offset > _source.length) { | |
| 192 throw new IndexOutOfRangeException("offset out of range (< 0)"); | |
| 193 } | |
| 194 if (_length != null && (_length < 0)) { | |
| 195 throw new IndexOutOfRangeException("length out of range (< 0)"); | |
| 196 } | |
| 197 if (_length + _offset > _source.length) { | |
| 198 throw new IndexOutOfRangeException("offset + length > source.length"); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 ListRangeIterator<T> iterator() => | |
| 203 new ListRangeIteratorImpl(_source, _offset, _offset + _length); | |
| 204 | |
| 205 int get length() => _length; | |
| 206 } | |
| 207 | |
| 208 /** | |
| 209 * The ListRangeIterator provides more capabilities than a standard iterator, | |
| 210 * including the ability to get the current position, count remaining items, | |
| 211 * and move forward/backward within the iterator. | |
| 212 */ | |
| 213 interface ListRangeIterator<T> extends Iterator<T> { | |
| 214 bool hasNext(); | |
| 215 T next(); | |
| 216 int get position(); | |
| 217 void backup([int by]); | |
| 218 int get remaining(); | |
| 219 void skip([int count]); | |
| 220 } | |
| 221 | |
| 222 class ListRangeIteratorImpl<T> implements ListRangeIterator<T> { | |
| 223 final List<T> _source; | |
| 224 int _offset; | |
| 225 final int _end; | |
| 226 | |
| 227 ListRangeIteratorImpl(List<T> source, int offset, int end) : | |
| 228 _source = source, _offset = offset, _end = end; | |
| 229 | |
| 230 bool hasNext() => _offset < _end; | |
| 231 T next() => _source[_offset++]; | |
| 232 int get position() => _offset; | |
| 233 void backup([int by = 1]) { | |
| 234 _offset -= by; | |
| 235 } | |
| 236 int get remaining() => _end - _offset; | |
| 237 void skip([int count = 1]) { | |
| 238 _offset += count; | |
| 239 } | |
| 240 } | |
| OLD | NEW |