Chromium Code Reviews| 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("unicode_core"); | 5 #library("unicode_core"); |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * Test for presence of bug related to the use of UTF-16 code units for | 8 * Test for presence of bug related to the use of UTF-16 code units for |
| 9 * Dart compiled to JS. | 9 * Dart compiled to JS. |
| 10 */ | 10 */ |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 final int UNICODE_UTF16_SURROGATE_UNIT_1_BASE = 0xdc00; | 39 final int UNICODE_UTF16_SURROGATE_UNIT_1_BASE = 0xdc00; |
| 40 final int UNICODE_UTF16_HI_MASK = 0xffc00; | 40 final int UNICODE_UTF16_HI_MASK = 0xffc00; |
| 41 final int UNICODE_UTF16_LO_MASK = 0x3ff; | 41 final int UNICODE_UTF16_LO_MASK = 0x3ff; |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Encode code points as UTF16 code units. | 44 * Encode code points as UTF16 code units. |
| 45 */ | 45 */ |
| 46 List<int> codepointsToUtf16CodeUnits( | 46 List<int> codepointsToUtf16CodeUnits( |
| 47 List<int> codepoints, [int offset = 0, int length, | 47 List<int> codepoints, [int offset = 0, int length, |
| 48 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 48 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 49 if (!(offset >= 0)) { | |
| 50 throw new IllegalArgumentException("offset"); | |
| 51 } | |
| 52 | 49 |
| 53 if (!(length == null || length >= 0)) { | 50 ListRange<int> listRange = new ListRange<int>(codepoints, offset, length); |
| 54 throw new IllegalArgumentException("length"); | |
| 55 } | |
| 56 | |
| 57 int end = length != null ? | |
| 58 Math.min(codepoints.length, offset + length) : | |
| 59 codepoints.length; | |
| 60 | |
| 61 int encodedLength = 0; | 51 int encodedLength = 0; |
| 62 for (int i = offset; i < end; i++) { | 52 for (int value in listRange) { |
| 63 int value = codepoints[i]; | |
| 64 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | 53 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || |
| 65 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 54 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
| 66 encodedLength++; | 55 encodedLength++; |
| 67 } else if (value > UNICODE_PLANE_ONE_MAX && | 56 } else if (value > UNICODE_PLANE_ONE_MAX && |
| 68 value <= UNICODE_VALID_RANGE_MAX) { | 57 value <= UNICODE_VALID_RANGE_MAX) { |
| 69 encodedLength += 2; | 58 encodedLength += 2; |
| 70 } else { | 59 } else { |
| 71 encodedLength++; | 60 encodedLength++; |
| 72 } | 61 } |
| 73 } | 62 } |
| 74 | 63 |
| 75 void addReplacementCodepoint(List<int> codepointBuffer, int offset, | |
| 76 int replacementCodepoint) { | |
| 77 if (replacementCodepoint != null) { | |
| 78 codepointBuffer[offset] = replacementCodepoint; | |
| 79 } else { | |
| 80 throw new IllegalArgumentException("Invalid encoding"); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 List<int> codeUnitsBuffer = new List<int>(encodedLength); | 64 List<int> codeUnitsBuffer = new List<int>(encodedLength); |
| 85 int j = 0; | 65 int j = 0; |
| 86 for (int i = offset; i < end; i++) { | 66 for (int value in listRange) { |
| 87 int value = codepoints[i]; | |
| 88 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | 67 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || |
| 89 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 68 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
| 90 codeUnitsBuffer[j++] = value; | 69 codeUnitsBuffer[j++] = value; |
| 91 } else if (value > UNICODE_PLANE_ONE_MAX && | 70 } else if (value > UNICODE_PLANE_ONE_MAX && |
| 92 value <= UNICODE_VALID_RANGE_MAX) { | 71 value <= UNICODE_VALID_RANGE_MAX) { |
| 93 int base = value - UNICODE_UTF16_OFFSET; | 72 int base = value - UNICODE_UTF16_OFFSET; |
| 94 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_0_BASE + | 73 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_0_BASE + |
| 95 ((base & UNICODE_UTF16_HI_MASK) >> 10); | 74 ((base & UNICODE_UTF16_HI_MASK) >> 10); |
| 96 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_1_BASE + | 75 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_1_BASE + |
| 97 (base & UNICODE_UTF16_LO_MASK); | 76 (base & UNICODE_UTF16_LO_MASK); |
| 77 } else if (replacementCodepoint != null) { | |
| 78 codeUnitsBuffer[j++] = replacementCodepoint; | |
| 98 } else { | 79 } else { |
| 99 addReplacementCodepoint(codeUnitsBuffer, j++, replacementCodepoint); | 80 throw new IllegalArgumentException("Invalid encoding"); |
| 100 } | 81 } |
| 101 } | 82 } |
| 102 return codeUnitsBuffer; | 83 return codeUnitsBuffer; |
| 103 } | 84 } |
| 104 | 85 |
| 105 /** | 86 /** |
| 106 * Decodes the utf16 codeunits to codepoints. | 87 * Decodes the utf16 codeunits to codepoints. |
| 107 */ | 88 */ |
| 108 List<int> utf16CodeUnitsToCodepoints( | 89 List<int> utf16CodeUnitsToCodepoints( |
| 109 List<int> utf16CodeUnits, [int offset = 0, int length, | 90 List<int> utf16CodeUnits, [int offset = 0, int length, |
| 110 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 91 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 111 if (!(offset >= 0)) { | 92 ListRangeIterator<int> source = |
| 112 throw new IllegalArgumentException("offset"); | 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(); | |
| 113 } | 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 } | |
| 114 | 109 |
| 115 if (!(length == null || length >= 0)) { | 110 /** |
| 116 throw new IllegalArgumentException("length"); | 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 } | |
| 117 } | 175 } |
| 176 } | |
| 118 | 177 |
| 119 int end = length != null ? | 178 /** |
| 120 Math.min(utf16CodeUnits.length, offset + length) : | 179 * ListRange in an internal type used to create a lightweight Interable on a |
| 121 utf16CodeUnits.length; | 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; | |
| 122 | 187 |
| 123 void decode(void f(int v)) { | 188 ListRange(List<T> source, [int offset = 0, int length]) : |
| 124 int i = offset; | 189 this._source = source, this._offset = offset, |
| 125 // skip the first entry if it is a BOM. | 190 this._length = (length == null ? source.length - offset : length) { |
| 126 if (end > 0 && utf16CodeUnits[0] == UNICODE_BOM) { | 191 if (_offset < 0 || _offset > _source.length) { |
| 127 i++; | 192 throw new IndexOutOfRangeException("offset out of range (< 0)"); |
| 128 } | 193 } |
| 129 while (i < end) { | 194 if (_length != null && (_length < 0)) { |
| 130 int value = utf16CodeUnits[i++]; | 195 throw new IndexOutOfRangeException("length out of range (< 0)"); |
| 131 if (value < 0) { | 196 } |
| 132 f(null); | 197 if (_length + _offset > _source.length) { |
| 133 continue; | 198 throw new IndexOutOfRangeException("offset + length > source.length"); |
| 134 } | |
| 135 if (value < UNICODE_UTF16_RESERVED_LO || | |
| 136 (value > UNICODE_UTF16_RESERVED_HI && | |
| 137 value <= UNICODE_PLANE_ONE_MAX)) { | |
| 138 // transfer directly | |
| 139 f(value); | |
| 140 } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE && i < end) { | |
| 141 // merge surrogate pair | |
| 142 int nextValue = utf16CodeUnits[i++]; | |
| 143 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE && | |
| 144 nextValue <= UNICODE_UTF16_RESERVED_HI) { | |
| 145 value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10; | |
| 146 value += UNICODE_UTF16_OFFSET + | |
| 147 (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE); | |
| 148 f(value); | |
| 149 } else { | |
| 150 if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE && | |
| 151 nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) { | |
| 152 i--; | |
| 153 } | |
| 154 f(null); | |
| 155 continue; | |
| 156 } | |
| 157 } else { | |
| 158 f(null); | |
| 159 continue; | |
| 160 } | |
| 161 } | 199 } |
| 162 } | 200 } |
| 163 | 201 |
| 164 // First pass through data to 1) size the output buffer and 2) check for | 202 ListRangeIterator<T> iterator() => |
| 165 // special case optimization where A) the length stays the same and B) | 203 new ListRangeIteratorImpl(_source, _offset, _offset + _length); |
| 166 // no special replacement characters are used. If these criteria are met | |
| 167 // we can just copy input to the output. | |
| 168 int codepointBufferLength = 0; | |
| 169 bool hasReplacements = false; | |
| 170 decode(void _(int value) { | |
| 171 codepointBufferLength++; | |
| 172 if (value == null) { | |
| 173 hasReplacements = true; | |
| 174 } | |
| 175 }); | |
| 176 | 204 |
| 177 // If the string calls for replacements, but when the method is called | 205 int get length() => _length; |
| 178 // with replacementCodepoint explicitly set to null, then throw an exception. | 206 } |
| 179 if (hasReplacements && replacementCodepoint == null) { | 207 |
| 180 throw new IllegalArgumentException("Invalid encoding"); | 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> { | |
|
jat
2012/02/15 17:26:51
Why not _ListRange if it is internal?
dcarlson
2012/02/15 17:31:17
I want to use this in libraries (like utf16.dart).
| |
| 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; | |
| 181 } | 235 } |
| 182 | 236 int get remaining() => _end - _offset; |
| 183 int _length = end - offset; | 237 void skip([int count = 1]) { |
| 184 List<int> codepointBuffer = new List<int>(codepointBufferLength); | 238 _offset += count; |
| 185 if (_length == codepointBufferLength && !hasReplacements) { | |
| 186 codepointBuffer.setRange(0, _length, utf16CodeUnits, offset); | |
| 187 } else { | |
| 188 int i = 0; | |
| 189 decode( | |
| 190 void _(int value) { | |
| 191 if (value != null) { | |
| 192 codepointBuffer[i++] = value; | |
| 193 } else { | |
| 194 codepointBuffer[i++] = replacementCodepoint; | |
| 195 } | |
| 196 } | |
| 197 ); | |
| 198 } | 239 } |
| 199 return codepointBuffer; | |
| 200 } | 240 } |
| OLD | NEW |