| 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 part of dart.utf; | 5 part of dart.utf; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert | 8 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert |
| 9 * as much of the input as needed. Determines the byte order from the BOM, | 9 * as much of the input as needed. Determines the byte order from the BOM, |
| 10 * or uses big-endian as a default. This method always strips a leading BOM. | 10 * or uses big-endian as a default. This method always strips a leading BOM. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 List<int> encodeUtf16(String str) => | 106 List<int> encodeUtf16(String str) => |
| 107 encodeUtf16be(str, true); | 107 encodeUtf16be(str, true); |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Produce a list of UTF-16BE encoded bytes. By default, this method produces | 110 * Produce a list of UTF-16BE encoded bytes. By default, this method produces |
| 111 * UTF-16BE bytes with no BOM. | 111 * UTF-16BE bytes with no BOM. |
| 112 */ | 112 */ |
| 113 List<int> encodeUtf16be(String str, [bool writeBOM = false]) { | 113 List<int> encodeUtf16be(String str, [bool writeBOM = false]) { |
| 114 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | 114 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); |
| 115 List<int> encoding = | 115 List<int> encoding = |
| 116 new List<int>.fixedLength(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | 116 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); |
| 117 int i = 0; | 117 int i = 0; |
| 118 if (writeBOM) { | 118 if (writeBOM) { |
| 119 encoding[i++] = UNICODE_UTF_BOM_HI; | 119 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 120 encoding[i++] = UNICODE_UTF_BOM_LO; | 120 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 121 } | 121 } |
| 122 for (int unit in utf16CodeUnits) { | 122 for (int unit in utf16CodeUnits) { |
| 123 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | 123 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; |
| 124 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | 124 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 125 } | 125 } |
| 126 return encoding; | 126 return encoding; |
| 127 } | 127 } |
| 128 | 128 |
| 129 /** | 129 /** |
| 130 * Produce a list of UTF-16LE encoded bytes. By default, this method produces | 130 * Produce a list of UTF-16LE encoded bytes. By default, this method produces |
| 131 * UTF-16LE bytes with no BOM. | 131 * UTF-16LE bytes with no BOM. |
| 132 */ | 132 */ |
| 133 List<int> encodeUtf16le(String str, [bool writeBOM = false]) { | 133 List<int> encodeUtf16le(String str, [bool writeBOM = false]) { |
| 134 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | 134 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); |
| 135 List<int> encoding = | 135 List<int> encoding = |
| 136 new List<int>.fixedLength(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | 136 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); |
| 137 int i = 0; | 137 int i = 0; |
| 138 if (writeBOM) { | 138 if (writeBOM) { |
| 139 encoding[i++] = UNICODE_UTF_BOM_LO; | 139 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 140 encoding[i++] = UNICODE_UTF_BOM_HI; | 140 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 141 } | 141 } |
| 142 for (int unit in utf16CodeUnits) { | 142 for (int unit in utf16CodeUnits) { |
| 143 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | 143 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 144 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | 144 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; |
| 145 } | 145 } |
| 146 return encoding; | 146 return encoding; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 length, false, replacementCodepoint); | 231 length, false, replacementCodepoint); |
| 232 } | 232 } |
| 233 } | 233 } |
| 234 | 234 |
| 235 /** | 235 /** |
| 236 * Provides a fast way to decode the rest of the source bytes in a single | 236 * Provides a fast way to decode the rest of the source bytes in a single |
| 237 * call. This method trades memory for improved speed in that it potentially | 237 * call. This method trades memory for improved speed in that it potentially |
| 238 * over-allocates the List containing results. | 238 * over-allocates the List containing results. |
| 239 */ | 239 */ |
| 240 List<int> decodeRest() { | 240 List<int> decodeRest() { |
| 241 List<int> codeunits = new List<int>.fixedLength(remaining); | 241 List<int> codeunits = new List<int>(remaining); |
| 242 int i = 0; | 242 int i = 0; |
| 243 while (moveNext()) { | 243 while (moveNext()) { |
| 244 codeunits[i++] = current; | 244 codeunits[i++] = current; |
| 245 } | 245 } |
| 246 if (i == codeunits.length) { | 246 if (i == codeunits.length) { |
| 247 return codeunits; | 247 return codeunits; |
| 248 } else { | 248 } else { |
| 249 List<int> truncCodeunits = new List<int>.fixedLength(i); | 249 List<int> truncCodeunits = new List<int>(i); |
| 250 truncCodeunits.setRange(0, i, codeunits); | 250 truncCodeunits.setRange(0, i, codeunits); |
| 251 return truncCodeunits; | 251 return truncCodeunits; |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 | 254 |
| 255 int get current => _current; | 255 int get current => _current; |
| 256 | 256 |
| 257 bool moveNext() { | 257 bool moveNext() { |
| 258 _current = null; | 258 _current = null; |
| 259 if (utf16EncodedBytesIterator.remaining < 2) { | 259 if (utf16EncodedBytesIterator.remaining < 2) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 } | 328 } |
| 329 | 329 |
| 330 int decode() { | 330 int decode() { |
| 331 utf16EncodedBytesIterator.moveNext(); | 331 utf16EncodedBytesIterator.moveNext(); |
| 332 int lo = utf16EncodedBytesIterator.current; | 332 int lo = utf16EncodedBytesIterator.current; |
| 333 utf16EncodedBytesIterator.moveNext(); | 333 utf16EncodedBytesIterator.moveNext(); |
| 334 int hi = utf16EncodedBytesIterator.current; | 334 int hi = utf16EncodedBytesIterator.current; |
| 335 return (hi << 8) + lo; | 335 return (hi << 8) + lo; |
| 336 } | 336 } |
| 337 } | 337 } |
| OLD | NEW |