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 IterableUtf32Decoder decodeUtf32AsIterable(List<int> bytes, [ | |
13 int offset = 0, int length, | |
14 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
15 return new IterableUtf32Decoder._( | |
16 () => new Utf32BytesDecoder(bytes, offset, length, replacementCodepoint)); | |
17 } | |
18 | |
19 /** | |
20 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
21 * instead. | |
22 */ | |
23 @deprecated | |
24 IterableUtf32Decoder decodeUtf32beAsIterable(List<int> bytes, [ | |
25 int offset = 0, int length, bool stripBom = true, | |
26 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
27 return new IterableUtf32Decoder._( | |
28 () => new Utf32beBytesDecoder(bytes, offset, length, stripBom, | |
29 replacementCodepoint)); | |
30 } | |
31 | |
32 /** | |
33 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
34 * instead. | |
35 */ | |
36 @deprecated | |
37 IterableUtf32Decoder decodeUtf32leAsIterable(List<int> bytes, [ | |
38 int offset = 0, int length, bool stripBom = true, | |
39 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
40 return new IterableUtf32Decoder._( | |
41 () => new Utf32leBytesDecoder(bytes, offset, length, stripBom, | |
42 replacementCodepoint)); | |
43 } | |
44 | |
45 /** | |
46 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
47 * instead. | |
48 */ | |
49 @deprecated | |
50 String decodeUtf32(List<int> bytes, [int offset = 0, int length, | |
51 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
52 return new String.fromCharCodes((new Utf32BytesDecoder(bytes, offset, length, | |
53 replacementCodepoint)).decodeRest()); | |
54 } | |
55 | |
56 /** | |
57 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
58 * instead. | |
59 */ | |
60 @deprecated | |
61 String decodeUtf32be( | |
62 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | |
63 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | |
64 new String.fromCharCodes((new Utf32beBytesDecoder(bytes, offset, length, | |
65 stripBom, replacementCodepoint)).decodeRest()); | |
66 | |
67 /** | |
68 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
69 * instead. | |
70 */ | |
71 @deprecated | |
72 String decodeUtf32le( | |
73 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | |
74 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | |
75 new String.fromCharCodes((new Utf32leBytesDecoder(bytes, offset, length, | |
76 stripBom, replacementCodepoint)).decodeRest()); | |
77 | |
78 /** | |
79 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
80 * instead. | |
81 */ | |
82 @deprecated | |
83 List<int> encodeUtf32(String str) => | |
84 encodeUtf32be(str, true); | |
85 | |
86 /** | |
87 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
88 * instead. | |
89 */ | |
90 @deprecated | |
91 List<int> encodeUtf32be(String str, [bool writeBOM = false]) { | |
92 List<int> utf32CodeUnits = stringToCodepoints(str); | |
93 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + | |
94 (writeBOM ? 4 : 0)); | |
95 int i = 0; | |
96 if (writeBOM) { | |
97 encoding[i++] = 0; | |
98 encoding[i++] = 0; | |
99 encoding[i++] = UNICODE_UTF_BOM_HI; | |
100 encoding[i++] = UNICODE_UTF_BOM_LO; | |
101 } | |
102 for (int unit in utf32CodeUnits) { | |
103 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; | |
104 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; | |
105 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; | |
106 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
107 } | |
108 return encoding; | |
109 } | |
110 | |
111 /** | |
112 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
113 * instead. | |
114 */ | |
115 @deprecated | |
116 List<int> encodeUtf32le(String str, [bool writeBOM = false]) { | |
117 List<int> utf32CodeUnits = stringToCodepoints(str); | |
118 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + | |
119 (writeBOM ? 4 : 0)); | |
120 int i = 0; | |
121 if (writeBOM) { | |
122 encoding[i++] = UNICODE_UTF_BOM_LO; | |
123 encoding[i++] = UNICODE_UTF_BOM_HI; | |
124 encoding[i++] = 0; | |
125 encoding[i++] = 0; | |
126 } | |
127 for (int unit in utf32CodeUnits) { | |
128 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
129 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; | |
130 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; | |
131 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; | |
132 } | |
133 return encoding; | |
134 } | |
135 | |
136 /** | |
137 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
138 * instead. | |
139 */ | |
140 @deprecated | |
141 bool hasUtf32Bom( | |
142 List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
143 return hasUtf32beBom(utf32EncodedBytes, offset, length) || | |
144 hasUtf32leBom(utf32EncodedBytes, offset, length); | |
145 } | |
146 | |
147 /** | |
148 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
149 * instead. | |
150 */ | |
151 @deprecated | |
152 bool hasUtf32beBom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
153 int end = length != null ? offset + length : utf32EncodedBytes.length; | |
154 return (offset + 4) <= end && | |
155 utf32EncodedBytes[offset] == 0 && utf32EncodedBytes[offset + 1] == 0 && | |
156 utf32EncodedBytes[offset + 2] == UNICODE_UTF_BOM_HI && | |
157 utf32EncodedBytes[offset + 3] == 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 hasUtf32leBom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
166 int end = length != null ? offset + length : utf32EncodedBytes.length; | |
167 return (offset + 4) <= end && | |
168 utf32EncodedBytes[offset] == UNICODE_UTF_BOM_LO && | |
169 utf32EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI && | |
170 utf32EncodedBytes[offset + 2] == 0 && utf32EncodedBytes[offset + 3] == 0; | |
171 } | |
172 | |
173 typedef Utf32BytesDecoder Utf32BytesDecoderProvider(); | |
174 | |
175 /** | |
176 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
177 * instead. | |
178 */ | |
179 @deprecated | |
180 class IterableUtf32Decoder extends IterableBase<int> { | |
181 final Utf32BytesDecoderProvider codeunitsProvider; | |
182 | |
183 IterableUtf32Decoder._(this.codeunitsProvider); | |
184 | |
185 Utf32BytesDecoder get iterator => codeunitsProvider(); | |
186 } | |
187 | |
188 /** | |
189 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
190 * instead. | |
191 */ | |
192 @deprecated | |
193 abstract class Utf32BytesDecoder implements _ListRangeIterator { | |
194 final _ListRangeIterator utf32EncodedBytesIterator; | |
195 final int replacementCodepoint; | |
196 int _current = null; | |
197 | |
198 Utf32BytesDecoder._fromListRangeIterator( | |
199 this.utf32EncodedBytesIterator, this.replacementCodepoint); | |
200 | |
201 factory Utf32BytesDecoder(List<int> utf32EncodedBytes, [ | |
202 int offset = 0, int length, | |
203 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
204 if (length == null) { | |
205 length = utf32EncodedBytes.length - offset; | |
206 } | |
207 if (hasUtf32beBom(utf32EncodedBytes, offset, length)) { | |
208 return new Utf32beBytesDecoder(utf32EncodedBytes, offset + 4, length - 4, | |
209 false, replacementCodepoint); | |
210 } else if (hasUtf32leBom(utf32EncodedBytes, offset, length)) { | |
211 return new Utf32leBytesDecoder(utf32EncodedBytes, offset + 4, length - 4, | |
212 false, replacementCodepoint); | |
213 } else { | |
214 return new Utf32beBytesDecoder(utf32EncodedBytes, offset, length, false, | |
215 replacementCodepoint); | |
216 } | |
217 } | |
218 | |
219 List<int> decodeRest() { | |
220 List<int> codeunits = new List<int>(remaining); | |
221 int i = 0; | |
222 while (moveNext()) { | |
223 codeunits[i++] = current; | |
224 } | |
225 return codeunits; | |
226 } | |
227 | |
228 int get current => _current; | |
229 | |
230 bool moveNext() { | |
231 _current = null; | |
232 if (utf32EncodedBytesIterator.remaining < 4) { | |
233 utf32EncodedBytesIterator.skip(utf32EncodedBytesIterator.remaining); | |
234 if (replacementCodepoint != null) { | |
235 _current = replacementCodepoint; | |
236 return true; | |
237 } else { | |
238 throw new ArgumentError( | |
239 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); | |
240 } | |
241 } else { | |
242 int codepoint = decode(); | |
243 if (_validCodepoint(codepoint)) { | |
244 _current = codepoint; | |
245 return true; | |
246 } else if (replacementCodepoint != null) { | |
247 _current = replacementCodepoint; | |
248 return true; | |
249 } else { | |
250 throw new ArgumentError( | |
251 "Invalid UTF32 at ${utf32EncodedBytesIterator.position}"); | |
252 } | |
253 } | |
254 } | |
255 | |
256 int get position => utf32EncodedBytesIterator.position ~/ 4; | |
257 | |
258 void backup([int by = 1]) { | |
259 utf32EncodedBytesIterator.backup(4 * by); | |
260 } | |
261 | |
262 int get remaining => (utf32EncodedBytesIterator.remaining + 3) ~/ 4; | |
263 | |
264 void skip([int count = 1]) { | |
265 utf32EncodedBytesIterator.skip(4 * count); | |
266 } | |
267 | |
268 int decode(); | |
269 } | |
270 | |
271 /** | |
272 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
273 * instead. | |
274 */ | |
275 @deprecated | |
276 class Utf32beBytesDecoder extends Utf32BytesDecoder { | |
277 Utf32beBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, | |
278 int length, bool stripBom = true, | |
279 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | |
280 super._fromListRangeIterator( | |
281 (new _ListRange(utf32EncodedBytes, offset, length)).iterator, | |
282 replacementCodepoint) { | |
283 if (stripBom && hasUtf32beBom(utf32EncodedBytes, offset, length)) { | |
284 skip(); | |
285 } | |
286 } | |
287 | |
288 int decode() { | |
289 utf32EncodedBytesIterator.moveNext(); | |
290 int value = utf32EncodedBytesIterator.current; | |
291 utf32EncodedBytesIterator.moveNext(); | |
292 value = (value << 8) + utf32EncodedBytesIterator.current; | |
293 utf32EncodedBytesIterator.moveNext(); | |
294 value = (value << 8) + utf32EncodedBytesIterator.current; | |
295 utf32EncodedBytesIterator.moveNext(); | |
296 value = (value << 8) + utf32EncodedBytesIterator.current; | |
297 return value; | |
298 } | |
299 } | |
300 | |
301 /** | |
302 * *DEPRECATED*: Use `package:utf/utf.dart` or, when applicable, `dart:convert` | |
303 * instead. | |
304 */ | |
305 @deprecated | |
306 class Utf32leBytesDecoder extends Utf32BytesDecoder { | |
307 Utf32leBytesDecoder(List<int> utf32EncodedBytes, [int offset = 0, | |
308 int length, bool stripBom = true, | |
309 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | |
310 super._fromListRangeIterator( | |
311 (new _ListRange(utf32EncodedBytes, offset, length)).iterator, | |
312 replacementCodepoint) { | |
313 if (stripBom && hasUtf32leBom(utf32EncodedBytes, offset, length)) { | |
314 skip(); | |
315 } | |
316 } | |
317 | |
318 int decode() { | |
319 utf32EncodedBytesIterator.moveNext(); | |
320 int value = utf32EncodedBytesIterator.current; | |
321 utf32EncodedBytesIterator.moveNext(); | |
322 value += (utf32EncodedBytesIterator.current << 8); | |
323 utf32EncodedBytesIterator.moveNext(); | |
324 value += (utf32EncodedBytesIterator.current << 16); | |
325 utf32EncodedBytesIterator.moveNext(); | |
326 value += (utf32EncodedBytesIterator.current << 24); | |
327 return value; | |
328 } | |
329 } | |
330 | |
331 bool _validCodepoint(int codepoint) { | |
332 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || | |
333 (codepoint > UNICODE_UTF16_RESERVED_HI && | |
334 codepoint < UNICODE_VALID_RANGE_MAX); | |
335 } | |
OLD | NEW |