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("utf16"); | 5 #library("utf16"); |
| 6 #import("unicode_core.dart"); | 6 #import("unicode_core.dart"); |
| 7 #import("unicode.dart"); | 7 #import("unicode.dart"); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Produce a String from a sequence of UTF-16 encoded bytes. The parameters | 10 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert |
| 11 * allow an offset into a list of bytes (as int), limiting the length of the | 11 * as much of the input as needed. Determines the byte order from the BOM, |
| 12 * values be decoded and the ability of override the default Unicode | 12 * or uses big-endian as a default. This method always strips a leading BOM. |
| 13 * replacement character. Set the replacementCharacter to null to throw an | 13 * Set the replacementCharacter to null to throw an IllegalArgumentException |
| 14 * rather than replace the bad value. | |
| 15 */ | |
| 16 IterableUtf16Decoder decodeUtf16AsIterable(List<int> bytes, [int offset = 0, | |
| 17 int length, int replacementCodepoint = | |
| 18 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 19 return new IterableUtf16Decoder._( | |
| 20 () => new Utf16BytesToCodeUnitsDecoder(bytes, offset, length, | |
| 21 replacementCodepoint), replacementCodepoint); | |
| 22 } | |
| 23 | |
| 24 /** | |
| 25 * Decodes the UTF-16BE bytes as an iterable. Thus, the consumer can only conver t | |
|
Søren Gjesse
2012/02/16 15:08:18
Long line.
This is repeated in 3 other places.
dcarlson
2012/02/17 17:32:33
Done.
| |
| 26 * as much of the input as needed. This method strips a leading BOM by default, | |
| 27 * but can be overridden by setting the optional parameter [stripBom] to false. | |
| 28 * Set the replacementCharacter to null to throw an IllegalArgumentException | |
|
Søren Gjesse
2012/02/16 15:08:18
replacementCharacter in []s. Maybe also explain th
dcarlson
2012/02/17 17:32:33
Done.
| |
| 29 * rather than replace the bad value. | |
| 30 */ | |
| 31 IterableUtf16Decoder decodeUtf16beAsIterable(List<int> bytes, [int offset = 0, | |
| 32 int length, bool stripBom = true, int replacementCodepoint = | |
| 33 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 34 return new IterableUtf16Decoder._( | |
| 35 () => new Utf16beBytesToCodeUnitsDecoder(bytes, offset, length, stripBom, | |
| 36 replacementCodepoint), replacementCodepoint); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Decodes the UTF-16LE bytes as an iterable. Thus, the consumer can only conver t | |
| 41 * as much of the input as needed. This method strips a leading BOM by default, | |
| 42 * but can be overridden by setting the optional parameter [stripBom] to false. | |
| 43 * Set the replacementCharacter to null to throw an IllegalArgumentException | |
| 44 * rather than replace the bad value. | |
| 45 */ | |
| 46 IterableUtf16Decoder decodeUtf16leAsIterable(List<int> bytes, [int offset = 0, | |
| 47 int length, bool stripBom = true, int replacementCodepoint = | |
| 48 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 49 return new IterableUtf16Decoder._( | |
| 50 () => new Utf16leBytesToCodeUnitsDecoder(bytes, offset, length, stripBom, | |
| 51 replacementCodepoint), replacementCodepoint); | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Produce a String from a sequence of UTF-16 encoded bytes. This method always | |
| 56 * strips a leading BOM. Set the replacementCharacter to null to throw an | |
| 14 * IllegalArgumentException rather than replace the bad value. | 57 * IllegalArgumentException rather than replace the bad value. |
| 15 */ | 58 */ |
| 16 String decodeFromUtf16(List<int> bytes, [int offset = 0, int length, | 59 String decodeUtf16(List<int> bytes, [int offset = 0, int length, |
| 17 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 60 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 18 List<int> codeUnits = | 61 Utf16BytesToCodeUnitsDecoder decoder = new Utf16BytesToCodeUnitsDecoder(bytes, |
| 19 _utf16ToUtf16CodeUnits(bytes, offset, length); | 62 offset, length, replacementCodepoint); |
| 63 List<int> codeunits = decoder.decodeRest(); | |
| 20 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 64 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc |
| 21 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 65 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 22 // removing after this issue is resolved. | 66 // removing after this issue is resolved. |
| 23 if (is16BitCodeUnit()) { | 67 if (is16BitCodeUnit()) { |
| 24 return new String.fromCharCodes(codeUnits); | 68 return new String.fromCharCodes(codeunits); |
| 25 } else { | 69 } else { |
| 26 return new String.fromCharCodes( | 70 return new String.fromCharCodes( |
| 27 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint)); | 71 utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
| 28 } | 72 } |
| 29 } | 73 } |
| 30 | 74 |
| 31 /** | 75 /** |
| 32 * Produce a String from a sequence of UTF-16BE encoded bytes. The parameters | 76 * Produce a String from a sequence of UTF-16BE encoded bytes. This method |
| 33 * allow an offset into a list of bytes (as int), limiting the length of the | 77 * strips a leading BOM by default, but can be overridden by setting the |
| 34 * values be decoded and the ability of override the default Unicode | 78 * optional parameter [stripBom] to false. Set the replacementCharacter to null |
| 35 * replacement character. Set the replacementCharacter to null to throw an | 79 * to throw an IllegalArgumentException rather than replace the bad value. |
| 36 * IllegalArgumentException rather than replace the bad value. | |
| 37 */ | 80 */ |
| 38 String decodeFromUtf16be(List<int> bytes, [int offset = 0, int length, | 81 String decodeUtf16be(List<int> bytes, [int offset = 0, int length, |
| 39 bool stripBom = true, | 82 bool stripBom = true, |
| 40 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 83 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 41 List<int> codeUnits = | 84 List<int> codeunits = (new Utf16beBytesToCodeUnitsDecoder(bytes, offset, |
| 42 _utf16beToUtf16CodeUnits(bytes, offset, length, stripBom); | 85 length, stripBom, replacementCodepoint)).decodeRest(); |
| 43 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 86 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc |
| 44 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 87 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 45 // removing after this issue is resolved. | 88 // removing after this issue is resolved. |
| 46 if (is16BitCodeUnit()) { | 89 if (is16BitCodeUnit()) { |
| 47 return new String.fromCharCodes(codeUnits); | 90 return new String.fromCharCodes(codeunits); |
| 48 } else { | 91 } else { |
| 49 return new String.fromCharCodes( | 92 return new String.fromCharCodes( |
| 50 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint)); | 93 utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
| 51 } | 94 } |
| 52 } | 95 } |
| 53 | 96 |
| 54 /** | 97 /** |
| 55 * Produce a String from a sequence of UTF-16LE encoded bytes. The parameters | 98 * Produce a String from a sequence of UTF-16LE encoded bytes. This method |
| 56 * allow an offset into a list of bytes (as int), limiting the length of the | 99 * strips a leading BOM by default, but can be overridden by setting the |
| 57 * values be decoded and the ability of override the default Unicode | 100 * optional parameter [stripBom] to false. Set the replacementCharacter to null |
| 58 * replacement character. Set the replacementCharacter to null to throw an | 101 * to throw an IllegalArgumentException rather than replace the bad value. |
| 59 * IllegalArgumentException rather than replace the bad value. | |
| 60 */ | 102 */ |
| 61 String decodeFromUtf16le(List<int> bytes, [int offset = 0, int length, | 103 String decodeUtf16le(List<int> bytes, [int offset = 0, int length, |
| 62 bool stripBom = true, | 104 bool stripBom = true, |
| 63 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 105 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 64 List<int> codeUnits = | 106 List<int> codeunits = (new Utf16leBytesToCodeUnitsDecoder(bytes, offset, |
| 65 _utf16leToUtf16CodeUnits(bytes, offset, length, stripBom); | 107 length, stripBom, replacementCodepoint)).decodeRest(); |
| 66 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 108 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc |
| 67 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 109 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 68 // removing after this issue is resolved. | 110 // removing after this issue is resolved. |
| 69 if (is16BitCodeUnit()) { | 111 if (is16BitCodeUnit()) { |
| 70 return new String.fromCharCodes(codeUnits); | 112 return new String.fromCharCodes(codeunits); |
| 71 } else { | 113 } else { |
| 72 return new String.fromCharCodes( | 114 return new String.fromCharCodes( |
| 73 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint)); | 115 utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
| 74 } | 116 } |
| 75 } | 117 } |
| 76 | 118 |
| 77 /** | 119 /** |
| 78 * Produce a sequence of UTF-16 encoded bytes. | 120 * Produce a list of UTF-16 encoded bytes. This method prefixes the resulting |
| 121 * bytes with a big-endian byte-order-marker. | |
| 79 */ | 122 */ |
| 80 List<int> encodeAsUtf16(String str) => | 123 List<int> encodeUtf16(String str) => |
| 81 encodeAsUtf16be(str, true); | 124 encodeUtf16be(str, true); |
| 82 | 125 |
| 83 /** | 126 /** |
| 84 * Produce a sequence of UTF-16BE encoded bytes. | 127 * Produce a list of UTF-16BE encoded bytes. By default, this method produces |
| 128 * UTF-16BE bytes with no BOM. | |
| 85 */ | 129 */ |
| 86 List<int> encodeAsUtf16be(String str, [bool writeBOM = false]) { | 130 List<int> encodeUtf16be(String str, [bool writeBOM = false]) { |
| 87 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | 131 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); |
| 88 List<int> encoding = | 132 List<int> encoding = |
| 89 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | 133 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); |
| 90 int i = 0; | 134 int i = 0; |
| 91 if (writeBOM) { | 135 if (writeBOM) { |
| 92 encoding[i++] = UNICODE_UTF_BOM_HI; | 136 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 93 encoding[i++] = UNICODE_UTF_BOM_LO; | 137 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 94 } | 138 } |
| 95 for (int unit in utf16CodeUnits) { | 139 for (int unit in utf16CodeUnits) { |
| 96 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | 140 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; |
| 97 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | 141 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 98 } | 142 } |
| 99 return encoding; | 143 return encoding; |
| 100 } | 144 } |
| 101 | 145 |
| 102 /** | 146 /** |
| 103 * Produce a sequence of UTF-16LE encoded bytes. | 147 * Produce a list of UTF-16LE encoded bytes. By default, this method produces |
| 148 * UTF-16LE bytes with no BOM. | |
| 104 */ | 149 */ |
| 105 List<int> encodeAsUtf16le(String str, [bool writeBOM = false]) { | 150 List<int> encodeUtf16le(String str, [bool writeBOM = false]) { |
| 106 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | 151 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); |
| 107 List<int> encoding = | 152 List<int> encoding = |
| 108 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | 153 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); |
| 109 int i = 0; | 154 int i = 0; |
| 110 if (writeBOM) { | 155 if (writeBOM) { |
| 111 encoding[i++] = UNICODE_UTF_BOM_LO; | 156 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 112 encoding[i++] = UNICODE_UTF_BOM_HI; | 157 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 113 } | 158 } |
| 114 for (int unit in utf16CodeUnits) { | 159 for (int unit in utf16CodeUnits) { |
| 115 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | 160 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 116 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | 161 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; |
| 117 } | 162 } |
| 118 return encoding; | 163 return encoding; |
| 119 } | 164 } |
| 120 | 165 |
| 166 /** | |
| 167 * Identifies whether a List of bytes starts (based on offset) with a | |
| 168 * byte-order marker (BOM). | |
| 169 */ | |
| 121 bool hasUtf16Bom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | 170 bool hasUtf16Bom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { |
| 122 return hasUtf16beBom(utf32EncodedBytes, offset, length) || | 171 return hasUtf16beBom(utf32EncodedBytes, offset, length) || |
| 123 hasUtf16leBom(utf32EncodedBytes, offset, length); | 172 hasUtf16leBom(utf32EncodedBytes, offset, length); |
| 124 } | 173 } |
| 125 | 174 |
| 175 /** | |
| 176 * Identifies whether a List of bytes starts (based on offset) with a | |
| 177 * big-endian byte-order marker (BOM). | |
| 178 */ | |
| 126 bool hasUtf16beBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { | 179 bool hasUtf16beBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { |
| 127 if (!(offset >= 0)) { | 180 int end = length != null ? offset + length : utf16EncodedBytes.length; |
| 128 throw new IllegalArgumentException("offset"); | |
| 129 } | |
| 130 | |
| 131 if (!(length == null || length >= 0)) { | |
| 132 throw new IllegalArgumentException("length"); | |
| 133 } | |
| 134 | |
| 135 int end = length != null ? | |
| 136 Math.min(utf16EncodedBytes.length, offset + length) : | |
| 137 utf16EncodedBytes.length; | |
| 138 | |
| 139 return (offset + 2) <= end && | 181 return (offset + 2) <= end && |
| 140 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_HI && | 182 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_HI && |
| 141 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_LO; | 183 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_LO; |
| 142 } | 184 } |
| 143 | 185 |
| 186 /** | |
| 187 * Identifies whether a List of bytes starts (based on offset) with a | |
| 188 * little-endian byte-order marker (BOM). | |
| 189 */ | |
| 144 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { | 190 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { |
| 145 if (!(offset >= 0)) { | 191 int end = length != null ? offset + length : utf16EncodedBytes.length; |
| 146 throw new IllegalArgumentException("offset"); | |
| 147 } | |
| 148 | |
| 149 if (!(length == null || length >= 0)) { | |
| 150 throw new IllegalArgumentException("length"); | |
| 151 } | |
| 152 | |
| 153 int end = length != null ? | |
| 154 Math.min(utf16EncodedBytes.length, offset + length) : | |
| 155 utf16EncodedBytes.length; | |
| 156 | |
| 157 return (offset + 2) <= end && | 192 return (offset + 2) <= end && |
| 158 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO && | 193 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO && |
| 159 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI; | 194 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI; |
| 160 } | 195 } |
| 161 | 196 |
| 162 int _sizeCodeUnits(int utf16CodeUnitsLength) { | |
| 163 int v = ((utf16CodeUnitsLength)/2).floor().toInt(); | |
| 164 return v; | |
| 165 } | |
| 166 | |
| 167 List<int> _stringToUtf16CodeUnits(String str) { | 197 List<int> _stringToUtf16CodeUnits(String str) { |
| 168 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 198 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc |
| 169 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 199 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 170 // removing after this issue is resolved. | 200 // removing after this issue is resolved. |
| 171 if (is16BitCodeUnit()) { | 201 if (is16BitCodeUnit()) { |
| 172 return str.charCodes(); | 202 return str.charCodes(); |
| 173 } else { | 203 } else { |
| 174 return codepointsToUtf16CodeUnits(str.charCodes()); | 204 return codepointsToUtf16CodeUnits(str.charCodes()); |
| 175 } | 205 } |
| 176 } | 206 } |
| 177 | 207 |
| 178 /** | 208 /** |
| 209 * Return type of [decodeUtf16AsIterable] and variants. The Iterable type | |
| 210 * provides an iterator on demand and the iterator will only translate bytes | |
| 211 * as requested by the user of the iterator. (Note: results are not cached.) | |
| 212 */ | |
| 213 class IterableUtf16Decoder implements Iterable<int> { | |
| 214 final Function codeunitsProvider; | |
| 215 final int replacementCodepoint; | |
| 216 | |
| 217 IterableUtf16Decoder._(ListRangeIterator<int> this.codeunitsProvider(), | |
| 218 int this.replacementCodepoint); | |
| 219 | |
| 220 Utf16CodeUnitDecoder iterator() => | |
| 221 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), | |
| 222 replacementCodepoint); | |
| 223 } | |
| 224 | |
| 225 /** | |
| 226 * Convert UTF-16 encoded bytes to UTF-16 code units by grouping 1-2 bytes | |
| 227 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine | |
| 228 * endian-ness, and defaults to BE. | |
| 229 */ | |
| 230 class Utf16BytesToCodeUnitsDecoder implements ListRangeIterator<int> { | |
| 231 final ListRangeIterator<int> utf16EncodedBytesIterator; | |
| 232 final int replacementCodepoint; | |
| 233 | |
| 234 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( | |
| 235 ListRangeIterator<int> this.utf16EncodedBytesIterator, | |
| 236 int this.replacementCodepoint); | |
| 237 | |
| 238 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | |
| 239 int offset = 0, int length, | |
| 240 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 241 int _length = length != null ? length : utf16EncodedBytes.length - offset; | |
|
Søren Gjesse
2012/02/16 15:08:18
Can't you just use length instead of introducing _
dcarlson
2012/02/17 17:32:33
Done.
| |
| 242 if (hasUtf16beBom(utf16EncodedBytes, offset, _length)) { | |
| 243 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | |
| 244 _length - 2, false, replacementCodepoint); | |
| 245 } else if (hasUtf16leBom(utf16EncodedBytes, offset, _length)) { | |
| 246 return new Utf16leBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | |
| 247 _length - 2, false, replacementCodepoint); | |
| 248 } else { | |
| 249 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset, | |
| 250 _length, false, replacementCodepoint); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 List<int> decodeRest() { | |
| 255 List<int> codeunits = new List<int>(remaining); | |
| 256 int i = 0; | |
| 257 while (hasNext()) { | |
| 258 codeunits[i++] = next(); | |
| 259 } | |
| 260 if (i == codeunits.length) { | |
| 261 return codeunits; | |
| 262 } else { | |
| 263 List<int> truncCodeunits = new List<int>(i); | |
| 264 truncCodeunits.setRange(0, i, codeunits); | |
| 265 return truncCodeunits; | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 bool hasNext() => utf16EncodedBytesIterator.hasNext(); | |
| 270 | |
| 271 int next() { | |
| 272 if (utf16EncodedBytesIterator.remaining < 2) { | |
| 273 utf16EncodedBytesIterator.next(); | |
| 274 if (replacementCodepoint != null) { | |
| 275 return replacementCodepoint; | |
| 276 } else { | |
| 277 throw new IllegalArgumentException( | |
| 278 "Invalid UTF16 at ${utf16EncodedBytesIterator.position}"); | |
| 279 } | |
| 280 } else { | |
| 281 return decode(); | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 int get position() => utf16EncodedBytesIterator.position ~/ 2; | |
| 286 | |
| 287 void backup([int by = 1]) { | |
| 288 utf16EncodedBytesIterator.backup(2 * by); | |
| 289 } | |
| 290 | |
| 291 int get remaining() => (utf16EncodedBytesIterator.remaining + 1) ~/ 2; | |
| 292 | |
| 293 void skip([int count = 1]) { | |
| 294 utf16EncodedBytesIterator.skip(2 * count); | |
| 295 } | |
| 296 | |
| 297 abstract int decode(); | |
| 298 } | |
| 299 | |
| 300 /** | |
| 179 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes | 301 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes |
| 180 * to produce the code unit (0-(2^16)-1). | 302 * to produce the code unit (0-(2^16)-1). |
| 181 */ | 303 */ |
| 182 List<int> _utf16beToUtf16CodeUnits( | 304 class Utf16beBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { |
| 183 List<int> utf16beEncodedBytes, [int offset = 0, int length, | 305 Utf16beBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 184 bool stripBom = true]) { | 306 int offset = 0, int length, bool stripBom = true, |
| 185 if (!(offset >= 0)) { | 307 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 186 throw new IllegalArgumentException("offset"); | 308 super._fromListRangeIterator((new ListRange(utf16EncodedBytes, offset, |
| 309 length)).iterator(), replacementCodepoint) { | |
| 310 if (stripBom && hasUtf16beBom(utf16EncodedBytes, offset, length)) { | |
| 311 skip(); | |
| 312 } | |
| 187 } | 313 } |
| 188 | 314 |
| 189 if (!(length == null || length >= 0)) { | 315 int decode() { |
| 190 throw new IllegalArgumentException("length"); | 316 int hi = utf16EncodedBytesIterator.next(); |
| 317 int lo = utf16EncodedBytesIterator.next(); | |
| 318 return (hi << 8) + lo; | |
| 191 } | 319 } |
| 192 | |
| 193 int end = length != null ? | |
| 194 Math.min(utf16beEncodedBytes.length, offset + length) : | |
| 195 utf16beEncodedBytes.length; | |
| 196 | |
| 197 int i = (stripBom && hasUtf16beBom(utf16beEncodedBytes, offset, length)) ? | |
| 198 offset + 2 : offset; | |
| 199 List<int> codeUnits = | |
| 200 new List<int>(_sizeCodeUnits(end - i)); | |
| 201 int lastIndex = end - 1; | |
| 202 int j = 0; | |
| 203 while (i < lastIndex) { | |
| 204 int hi = utf16beEncodedBytes[i++]; | |
| 205 int lo = utf16beEncodedBytes[i++]; | |
| 206 codeUnits[j++] = (hi << 8) | lo; | |
| 207 } | |
| 208 return codeUnits; | |
| 209 } | 320 } |
| 210 | 321 |
| 211 /** | 322 /** |
| 212 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes | 323 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes |
| 213 * to produce the code unit (0-(2^16)-1). | 324 * to produce the code unit (0-(2^16)-1). |
| 214 */ | 325 */ |
| 215 List<int> _utf16leToUtf16CodeUnits( | 326 class Utf16leBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { |
| 216 List<int> utf16leEncodedBytes, [int offset = 0, int length, | 327 Utf16leBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ |
| 217 bool stripBom = true]) { | 328 int offset = 0, int length, bool stripBom = true, |
| 218 if (!(offset >= 0)) { | 329 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 219 throw new IllegalArgumentException("offset"); | 330 super._fromListRangeIterator((new ListRange(utf16EncodedBytes, offset, |
| 331 length)).iterator(), replacementCodepoint) { | |
| 332 if (stripBom && hasUtf16leBom(utf16EncodedBytes, offset, length)) { | |
| 333 skip(); | |
| 334 } | |
| 220 } | 335 } |
| 221 | 336 |
| 222 if (!(length == null || length >= 0)) { | 337 int decode() { |
| 223 throw new IllegalArgumentException("length"); | 338 int lo = utf16EncodedBytesIterator.next(); |
| 224 } | 339 int hi = utf16EncodedBytesIterator.next(); |
| 225 | 340 return (hi << 8) + lo; |
| 226 int end = length != null ? | |
| 227 Math.min(utf16leEncodedBytes.length, offset + length) : | |
| 228 utf16leEncodedBytes.length; | |
| 229 | |
| 230 int i = (stripBom && hasUtf16leBom(utf16leEncodedBytes, offset, length)) ? | |
| 231 offset + 2 : offset; | |
| 232 List<int> codeUnits = | |
| 233 new List<int>(_sizeCodeUnits(end - i)); | |
| 234 int lastIndex = end - 1; | |
| 235 int j = 0; | |
| 236 while (i < lastIndex) { | |
| 237 int lo = utf16leEncodedBytes[i++]; | |
| 238 int hi = utf16leEncodedBytes[i++]; | |
| 239 codeUnits[j] = (hi << 8) | lo; | |
| 240 } | |
| 241 return codeUnits; | |
| 242 } | |
| 243 | |
| 244 /** | |
| 245 * Convert UTF-16 encoded bytes to utf16 code units by grouping 1-2 bytes | |
| 246 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine | |
| 247 * endian-ness, and defaults to BE. | |
| 248 */ | |
| 249 List<int> _utf16ToUtf16CodeUnits( | |
| 250 List<int> utf16EncodedBytes, [int offset = 0, int length]) { | |
| 251 if (!(offset >= 0)) { | |
| 252 throw new IllegalArgumentException("offset"); | |
| 253 } | |
| 254 | |
| 255 if (!(length == null || length >= 0)) { | |
| 256 throw new IllegalArgumentException("length"); | |
| 257 } | |
| 258 | |
| 259 int end = length != null ? | |
| 260 Math.min(utf16EncodedBytes.length, offset + length) : | |
| 261 utf16EncodedBytes.length; | |
| 262 | |
| 263 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { | |
| 264 return _utf16beToUtf16CodeUnits(utf16EncodedBytes, offset + 2, | |
| 265 end - (offset + 2), false); | |
| 266 } else if (hasUtf16leBom(utf16EncodedBytes, offset, length)) { | |
| 267 return _utf16leToUtf16CodeUnits(utf16EncodedBytes, offset + 2, | |
| 268 end - (offset + 2), false); | |
| 269 } else { | |
| 270 return _utf16beToUtf16CodeUnits( | |
| 271 utf16EncodedBytes, offset, end - offset, false); | |
| 272 } | 341 } |
| 273 } | 342 } |
| OLD | NEW |