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 | 5 |
6 /** | 6 /** |
7 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert | 7 * Decodes the UTF-16 bytes as an iterable. Thus, the consumer can only convert |
8 * as much of the input as needed. Determines the byte order from the BOM, | 8 * as much of the input as needed. Determines the byte order from the BOM, |
9 * or uses big-endian as a default. This method always strips a leading BOM. | 9 * or uses big-endian as a default. This method always strips a leading BOM. |
10 * Set the [replacementCodepoint] to null to throw an IllegalArgumentException | 10 * Set the [replacementCodepoint] to null to throw an IllegalArgumentException |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 * Produce a String from a sequence of UTF-16 encoded bytes. This method always | 55 * Produce a String from a sequence of UTF-16 encoded bytes. This method always |
56 * strips a leading BOM. Set the [replacementCodepoint] to null to throw an | 56 * strips a leading BOM. Set the [replacementCodepoint] to null to throw an |
57 * IllegalArgumentException rather than replace the bad value. The default | 57 * IllegalArgumentException rather than replace the bad value. The default |
58 * value for the [replacementCodepoint] is U+FFFD. | 58 * value for the [replacementCodepoint] is U+FFFD. |
59 */ | 59 */ |
60 String decodeUtf16(List<int> bytes, [int offset = 0, int length, | 60 String decodeUtf16(List<int> bytes, [int offset = 0, int length, |
61 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 61 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
62 Utf16BytesToCodeUnitsDecoder decoder = new Utf16BytesToCodeUnitsDecoder(bytes, | 62 Utf16BytesToCodeUnitsDecoder decoder = new Utf16BytesToCodeUnitsDecoder(bytes, |
63 offset, length, replacementCodepoint); | 63 offset, length, replacementCodepoint); |
64 List<int> codeunits = decoder.decodeRest(); | 64 List<int> codeunits = decoder.decodeRest(); |
65 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 65 // TODO is16BitCodeUnit() is used to work around a bug with dart2js |
66 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 66 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
67 // removing after this issue is resolved. | 67 // removing after this issue is resolved. |
68 if (_is16BitCodeUnit()) { | 68 if (_is16BitCodeUnit()) { |
69 return new String.fromCharCodes(codeunits); | 69 return new String.fromCharCodes(codeunits); |
70 } else { | 70 } else { |
71 return new String.fromCharCodes( | 71 return new String.fromCharCodes( |
72 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | 72 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 /** | 76 /** |
77 * Produce a String from a sequence of UTF-16BE encoded bytes. This method | 77 * Produce a String from a sequence of UTF-16BE encoded bytes. This method |
78 * strips a leading BOM by default, but can be overridden by setting the | 78 * strips a leading BOM by default, but can be overridden by setting the |
79 * optional parameter [stripBom] to false. Set the [replacementCodepoint] to | 79 * optional parameter [stripBom] to false. Set the [replacementCodepoint] to |
80 * null to throw an IllegalArgumentException rather than replace the bad value. | 80 * null to throw an IllegalArgumentException rather than replace the bad value. |
81 * The default value for the [replacementCodepoint] is U+FFFD. | 81 * The default value for the [replacementCodepoint] is U+FFFD. |
82 */ | 82 */ |
83 String decodeUtf16be(List<int> bytes, [int offset = 0, int length, | 83 String decodeUtf16be(List<int> bytes, [int offset = 0, int length, |
84 bool stripBom = true, | 84 bool stripBom = true, |
85 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 85 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
86 List<int> codeunits = (new Utf16beBytesToCodeUnitsDecoder(bytes, offset, | 86 List<int> codeunits = (new Utf16beBytesToCodeUnitsDecoder(bytes, offset, |
87 length, stripBom, replacementCodepoint)).decodeRest(); | 87 length, stripBom, replacementCodepoint)).decodeRest(); |
88 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 88 // TODO is16BitCodeUnit() is used to work around a bug with dart2js |
89 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 89 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
90 // removing after this issue is resolved. | 90 // removing after this issue is resolved. |
91 if (_is16BitCodeUnit()) { | 91 if (_is16BitCodeUnit()) { |
92 return new String.fromCharCodes(codeunits); | 92 return new String.fromCharCodes(codeunits); |
93 } else { | 93 } else { |
94 return new String.fromCharCodes( | 94 return new String.fromCharCodes( |
95 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | 95 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 /** | 99 /** |
100 * Produce a String from a sequence of UTF-16LE encoded bytes. This method | 100 * Produce a String from a sequence of UTF-16LE encoded bytes. This method |
101 * strips a leading BOM by default, but can be overridden by setting the | 101 * strips a leading BOM by default, but can be overridden by setting the |
102 * optional parameter [stripBom] to false. Set the [replacementCodepoint] to | 102 * optional parameter [stripBom] to false. Set the [replacementCodepoint] to |
103 * null to throw an IllegalArgumentException rather than replace the bad value. | 103 * null to throw an IllegalArgumentException rather than replace the bad value. |
104 * The default value for the [replacementCodepoint] is U+FFFD. | 104 * The default value for the [replacementCodepoint] is U+FFFD. |
105 */ | 105 */ |
106 String decodeUtf16le(List<int> bytes, [int offset = 0, int length, | 106 String decodeUtf16le(List<int> bytes, [int offset = 0, int length, |
107 bool stripBom = true, | 107 bool stripBom = true, |
108 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 108 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
109 List<int> codeunits = (new Utf16leBytesToCodeUnitsDecoder(bytes, offset, | 109 List<int> codeunits = (new Utf16leBytesToCodeUnitsDecoder(bytes, offset, |
110 length, stripBom, replacementCodepoint)).decodeRest(); | 110 length, stripBom, replacementCodepoint)).decodeRest(); |
111 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 111 // TODO is16BitCodeUnit() is used to work around a bug with dart2js |
112 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 112 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
113 // removing after this issue is resolved. | 113 // removing after this issue is resolved. |
114 if (_is16BitCodeUnit()) { | 114 if (_is16BitCodeUnit()) { |
115 return new String.fromCharCodes(codeunits); | 115 return new String.fromCharCodes(codeunits); |
116 } else { | 116 } else { |
117 return new String.fromCharCodes( | 117 return new String.fromCharCodes( |
118 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); | 118 _utf16CodeUnitsToCodepoints(codeunits, 0, null, replacementCodepoint)); |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 * little-endian byte-order marker (BOM). | 191 * little-endian byte-order marker (BOM). |
192 */ | 192 */ |
193 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { | 193 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) { |
194 int end = length != null ? offset + length : utf16EncodedBytes.length; | 194 int end = length != null ? offset + length : utf16EncodedBytes.length; |
195 return (offset + 2) <= end && | 195 return (offset + 2) <= end && |
196 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO && | 196 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO && |
197 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI; | 197 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI; |
198 } | 198 } |
199 | 199 |
200 List<int> _stringToUtf16CodeUnits(String str) { | 200 List<int> _stringToUtf16CodeUnits(String str) { |
201 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc | 201 // TODO is16BitCodeUnit() is used to work around a bug with dart2js |
202 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider | 202 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
203 // removing after this issue is resolved. | 203 // removing after this issue is resolved. |
204 if (_is16BitCodeUnit()) { | 204 if (_is16BitCodeUnit()) { |
205 return str.charCodes(); | 205 return str.charCodes(); |
206 } else { | 206 } else { |
207 return _codepointsToUtf16CodeUnits(str.charCodes()); | 207 return _codepointsToUtf16CodeUnits(str.charCodes()); |
208 } | 208 } |
209 } | 209 } |
210 | 210 |
211 /** | 211 /** |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 skip(); | 343 skip(); |
344 } | 344 } |
345 } | 345 } |
346 | 346 |
347 int decode() { | 347 int decode() { |
348 int lo = utf16EncodedBytesIterator.next(); | 348 int lo = utf16EncodedBytesIterator.next(); |
349 int hi = utf16EncodedBytesIterator.next(); | 349 int hi = utf16EncodedBytesIterator.next(); |
350 return (hi << 8) + lo; | 350 return (hi << 8) + lo; |
351 } | 351 } |
352 } | 352 } |
OLD | NEW |