Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: utils/string_encoding/utf16.dart

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

Powered by Google App Engine
This is Rietveld 408576698