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 part of dart.utf; | |
6 | |
7 /** | |
8 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
9 * instead. | |
10 */ | |
11 @deprecated | |
12 IterableUtf16Decoder decodeUtf16AsIterable(List<int> bytes, [int offset = 0, | |
13 int length, int replacementCodepoint = | |
14 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
15 return new IterableUtf16Decoder._( | |
16 () => new Utf16BytesToCodeUnitsDecoder(bytes, offset, length, | |
17 replacementCodepoint), replacementCodepoint); | |
18 } | |
19 | |
20 /** | |
21 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
22 * instead. | |
23 */ | |
24 @deprecated | |
25 IterableUtf16Decoder decodeUtf16beAsIterable(List<int> bytes, [int offset = 0, | |
26 int length, bool stripBom = true, int replacementCodepoint = | |
27 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
28 return new IterableUtf16Decoder._( | |
29 () => new Utf16beBytesToCodeUnitsDecoder(bytes, offset, length, stripBom, | |
30 replacementCodepoint), replacementCodepoint); | |
31 } | |
32 | |
33 /** | |
34 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
35 * instead. | |
36 */ | |
37 @deprecated | |
38 IterableUtf16Decoder decodeUtf16leAsIterable(List<int> bytes, [int offset = 0, | |
39 int length, bool stripBom = true, int replacementCodepoint = | |
40 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
41 return new IterableUtf16Decoder._( | |
42 () => new Utf16leBytesToCodeUnitsDecoder(bytes, offset, length, stripBom, | |
43 replacementCodepoint), replacementCodepoint); | |
44 } | |
45 | |
46 /** | |
47 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
48 * instead. | |
49 */ | |
50 @deprecated | |
51 String decodeUtf16(List<int> bytes, [int offset = 0, int length, | |
52 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
53 Utf16BytesToCodeUnitsDecoder decoder = new Utf16BytesToCodeUnitsDecoder(bytes, | |
54 offset, length, replacementCodepoint); | |
55 List<int> codeunits = decoder.decodeRest(); | |
56 return new String.fromCharCodes( | |
57 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | |
58 } | |
59 | |
60 /** | |
61 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
62 * instead. | |
63 */ | |
64 @deprecated | |
65 String decodeUtf16be(List<int> bytes, [int offset = 0, int length, | |
66 bool stripBom = true, | |
67 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
68 List<int> codeunits = (new Utf16beBytesToCodeUnitsDecoder(bytes, offset, | |
69 length, stripBom, replacementCodepoint)).decodeRest(); | |
70 return new String.fromCharCodes( | |
71 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | |
72 } | |
73 | |
74 /** | |
75 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
76 * instead. | |
77 */ | |
78 @deprecated | |
79 String decodeUtf16le(List<int> bytes, [int offset = 0, int length, | |
80 bool stripBom = true, | |
81 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
82 List<int> codeunits = (new Utf16leBytesToCodeUnitsDecoder(bytes, offset, | |
83 length, stripBom, replacementCodepoint)).decodeRest(); | |
84 return new String.fromCharCodes( | |
85 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | |
86 } | |
87 | |
88 /** | |
89 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
90 * instead. | |
91 */ | |
92 @deprecated | |
93 List<int> encodeUtf16(String str) => | |
94 encodeUtf16be(str, true); | |
95 | |
96 /** | |
97 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
98 * instead. | |
99 */ | |
100 @deprecated | |
101 List<int> encodeUtf16be(String str, [bool writeBOM = false]) { | |
102 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | |
103 List<int> encoding = | |
104 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | |
105 int i = 0; | |
106 if (writeBOM) { | |
107 encoding[i++] = UNICODE_UTF_BOM_HI; | |
108 encoding[i++] = UNICODE_UTF_BOM_LO; | |
109 } | |
110 for (int unit in utf16CodeUnits) { | |
111 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | |
112 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
113 } | |
114 return encoding; | |
115 } | |
116 | |
117 /** | |
118 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
119 * instead. | |
120 */ | |
121 @deprecated | |
122 List<int> encodeUtf16le(String str, [bool writeBOM = false]) { | |
123 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | |
124 List<int> encoding = | |
125 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0)); | |
126 int i = 0; | |
127 if (writeBOM) { | |
128 encoding[i++] = UNICODE_UTF_BOM_LO; | |
129 encoding[i++] = UNICODE_UTF_BOM_HI; | |
130 } | |
131 for (int unit in utf16CodeUnits) { | |
132 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
133 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8; | |
134 } | |
135 return encoding; | |
136 } | |
137 | |
138 /** | |
139 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
140 * instead. | |
141 */ | |
142 @deprecated | |
143 bool hasUtf16Bom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
144 return hasUtf16beBom(utf32EncodedBytes, offset, length) || | |
145 hasUtf16leBom(utf32EncodedBytes, offset, length); | |
146 } | |
147 | |
148 /** | |
149 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
150 * instead. | |
151 */ | |
152 @deprecated | |
153 bool hasUtf16beBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { | |
154 int end = length != null ? offset + length : utf16EncodedBytes.length; | |
155 return (offset + 2) <= end && | |
156 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_HI && | |
157 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_LO; | |
158 } | |
159 | |
160 /** | |
161 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
162 * instead. | |
163 */ | |
164 @deprecated | |
165 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { | |
166 int end = length != null ? offset + length : utf16EncodedBytes.length; | |
167 return (offset + 2) <= end && | |
168 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO && | |
169 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI; | |
170 } | |
171 | |
172 List<int> _stringToUtf16CodeUnits(String str) { | |
173 return _codepointsToUtf16CodeUnits(str.codeUnits); | |
174 } | |
175 | |
176 typedef _ListRangeIterator _CodeUnitsProvider(); | |
177 | |
178 /** | |
179 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
180 * instead. | |
181 */ | |
182 @deprecated | |
183 class IterableUtf16Decoder extends IterableBase<int> { | |
184 final _CodeUnitsProvider codeunitsProvider; | |
185 final int replacementCodepoint; | |
186 | |
187 IterableUtf16Decoder._(this.codeunitsProvider, this.replacementCodepoint); | |
188 | |
189 Utf16CodeUnitDecoder get iterator => | |
190 new Utf16CodeUnitDecoder.fromListRangeIterator(codeunitsProvider(), | |
191 replacementCodepoint); | |
192 } | |
193 | |
194 /** | |
195 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
196 * instead. | |
197 */ | |
198 @deprecated | |
199 abstract class Utf16BytesToCodeUnitsDecoder implements _ListRangeIterator { | |
200 final _ListRangeIterator utf16EncodedBytesIterator; | |
201 final int replacementCodepoint; | |
202 int _current = null; | |
203 | |
204 Utf16BytesToCodeUnitsDecoder._fromListRangeIterator( | |
205 this.utf16EncodedBytesIterator, this.replacementCodepoint); | |
206 | |
207 factory Utf16BytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | |
208 int offset = 0, int length, | |
209 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
210 if (length == null) { | |
211 length = utf16EncodedBytes.length - offset; | |
212 } | |
213 if (hasUtf16beBom(utf16EncodedBytes, offset, length)) { | |
214 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | |
215 length - 2, false, replacementCodepoint); | |
216 } else if (hasUtf16leBom(utf16EncodedBytes, offset, length)) { | |
217 return new Utf16leBytesToCodeUnitsDecoder(utf16EncodedBytes, offset + 2, | |
218 length - 2, false, replacementCodepoint); | |
219 } else { | |
220 return new Utf16beBytesToCodeUnitsDecoder(utf16EncodedBytes, offset, | |
221 length, false, replacementCodepoint); | |
222 } | |
223 } | |
224 | |
225 /** | |
226 * Provides a fast way to decode the rest of the source bytes in a single | |
227 * call. This method trades memory for improved speed in that it potentially | |
228 * over-allocates the List containing results. | |
229 */ | |
230 List<int> decodeRest() { | |
231 List<int> codeunits = new List<int>(remaining); | |
232 int i = 0; | |
233 while (moveNext()) { | |
234 codeunits[i++] = current; | |
235 } | |
236 if (i == codeunits.length) { | |
237 return codeunits; | |
238 } else { | |
239 List<int> truncCodeunits = new List<int>(i); | |
240 truncCodeunits.setRange(0, i, codeunits); | |
241 return truncCodeunits; | |
242 } | |
243 } | |
244 | |
245 int get current => _current; | |
246 | |
247 bool moveNext() { | |
248 _current = null; | |
249 if (utf16EncodedBytesIterator.remaining < 2) { | |
250 utf16EncodedBytesIterator.moveNext(); | |
251 if (replacementCodepoint != null) { | |
252 _current = replacementCodepoint; | |
253 return true; | |
254 } else { | |
255 throw new ArgumentError( | |
256 "Invalid UTF16 at ${utf16EncodedBytesIterator.position}"); | |
257 } | |
258 } else { | |
259 _current = decode(); | |
260 return true; | |
261 } | |
262 } | |
263 | |
264 int get position => utf16EncodedBytesIterator.position ~/ 2; | |
265 | |
266 void backup([int by = 1]) { | |
267 utf16EncodedBytesIterator.backup(2 * by); | |
268 } | |
269 | |
270 int get remaining => (utf16EncodedBytesIterator.remaining + 1) ~/ 2; | |
271 | |
272 void skip([int count = 1]) { | |
273 utf16EncodedBytesIterator.skip(2 * count); | |
274 } | |
275 | |
276 int decode(); | |
277 } | |
278 | |
279 /** | |
280 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
281 * instead. | |
282 */ | |
283 @deprecated | |
284 class Utf16beBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { | |
285 Utf16beBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | |
286 int offset = 0, int length, bool stripBom = true, | |
287 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | |
288 super._fromListRangeIterator( | |
289 (new _ListRange(utf16EncodedBytes, offset, length)).iterator, | |
290 replacementCodepoint) { | |
291 if (stripBom && hasUtf16beBom(utf16EncodedBytes, offset, length)) { | |
292 skip(); | |
293 } | |
294 } | |
295 | |
296 int decode() { | |
297 utf16EncodedBytesIterator.moveNext(); | |
298 int hi = utf16EncodedBytesIterator.current; | |
299 utf16EncodedBytesIterator.moveNext(); | |
300 int lo = utf16EncodedBytesIterator.current; | |
301 return (hi << 8) + lo; | |
302 } | |
303 } | |
304 | |
305 /** | |
306 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
307 * instead. | |
308 */ | |
309 @deprecated | |
310 class Utf16leBytesToCodeUnitsDecoder extends Utf16BytesToCodeUnitsDecoder { | |
311 Utf16leBytesToCodeUnitsDecoder(List<int> utf16EncodedBytes, [ | |
312 int offset = 0, int length, bool stripBom = true, | |
313 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | |
314 super._fromListRangeIterator( | |
315 (new _ListRange(utf16EncodedBytes, offset, length)).iterator, | |
316 replacementCodepoint) { | |
317 if (stripBom && hasUtf16leBom(utf16EncodedBytes, offset, length)) { | |
318 skip(); | |
319 } | |
320 } | |
321 | |
322 int decode() { | |
323 utf16EncodedBytesIterator.moveNext(); | |
324 int lo = utf16EncodedBytesIterator.current; | |
325 utf16EncodedBytesIterator.moveNext(); | |
326 int hi = utf16EncodedBytesIterator.current; | |
327 return (hi << 8) + lo; | |
328 } | |
329 } | |
OLD | NEW |