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

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

Issue 9310038: Fix mac/git renaming case failure. Aaarrrrgggghhhh. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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.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
(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 #library("utf16");
6 #import("unicode_core.dart");
7 #import("unicode.dart");
8
9 /**
10 * Produce a String from a sequence of UTF-16 encoded bytes. The parameters
11 * allow an offset into a list of bytes (as int), limiting the length of the
12 * values be decoded and the ability of override the default Unicode
13 * replacement character. Set the replacementCharacter to null to throw an
14 * IllegalArgumentException rather than replace the bad value.
15 */
16 String decodeFromUtf16(List<int> bytes, [int offset = 0, int length,
17 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
18 List<int> codeUnits =
19 _utf16ToUtf16CodeUnits(bytes, offset, length);
20 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
21 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
22 // removing after this issue is resolved.
23 if (is16BitCodeUnit()) {
24 return new String.fromCharCodes(codeUnits);
25 } else {
26 return new String.fromCharCodes(
27 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint));
28 }
29 }
30
31 /**
32 * Produce a String from a sequence of UTF-16BE encoded bytes. The parameters
33 * allow an offset into a list of bytes (as int), limiting the length of the
34 * values be decoded and the ability of override the default Unicode
35 * replacement character. Set the replacementCharacter to null to throw an
36 * IllegalArgumentException rather than replace the bad value.
37 */
38 String decodeFromUtf16be(List<int> bytes, [int offset = 0, int length,
39 bool stripBom = true,
40 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
41 List<int> codeUnits =
42 _utf16beToUtf16CodeUnits(bytes, offset, length, stripBom);
43 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
44 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
45 // removing after this issue is resolved.
46 if (is16BitCodeUnit()) {
47 return new String.fromCharCodes(codeUnits);
48 } else {
49 return new String.fromCharCodes(
50 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint));
51 }
52 }
53
54 /**
55 * Produce a String from a sequence of UTF-16LE encoded bytes. The parameters
56 * allow an offset into a list of bytes (as int), limiting the length of the
57 * values be decoded and the ability of override the default Unicode
58 * replacement character. Set the replacementCharacter to null to throw an
59 * IllegalArgumentException rather than replace the bad value.
60 */
61 String decodeFromUtf16le(List<int> bytes, [int offset = 0, int length,
62 bool stripBom = true,
63 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
64 List<int> codeUnits =
65 _utf16leToUtf16CodeUnits(bytes, offset, length, stripBom);
66 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
67 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
68 // removing after this issue is resolved.
69 if (is16BitCodeUnit()) {
70 return new String.fromCharCodes(codeUnits);
71 } else {
72 return new String.fromCharCodes(
73 utf16CodeUnitsToCodepoints(codeUnits, 0, null, replacementCodepoint));
74 }
75 }
76
77 /**
78 * Produce a sequence of UTF-16 encoded bytes.
79 */
80 List<int> encodeAsUtf16(String str) =>
81 encodeAsUtf16be(str, true);
82
83 /**
84 * Produce a sequence of UTF-16BE encoded bytes.
85 */
86 List<int> encodeAsUtf16be(String str, [bool writeBOM = false]) {
87 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
88 List<int> encoding =
89 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0));
90 int i = 0;
91 if (writeBOM) {
92 encoding[i++] = UNICODE_UTF_BOM_HI;
93 encoding[i++] = UNICODE_UTF_BOM_LO;
94 }
95 for (int unit in utf16CodeUnits) {
96 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8;
97 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK;
98 }
99 return encoding;
100 }
101
102 /**
103 * Produce a sequence of UTF-16LE encoded bytes.
104 */
105 List<int> encodeAsUtf16le(String str, [bool writeBOM = false]) {
106 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
107 List<int> encoding =
108 new List<int>(2 * utf16CodeUnits.length + (writeBOM ? 2 : 0));
109 int i = 0;
110 if (writeBOM) {
111 encoding[i++] = UNICODE_UTF_BOM_LO;
112 encoding[i++] = UNICODE_UTF_BOM_HI;
113 }
114 for (int unit in utf16CodeUnits) {
115 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK;
116 encoding[i++] = (unit & UNICODE_BYTE_ONE_MASK) >> 8;
117 }
118 return encoding;
119 }
120
121 bool hasUtf16Bom(List<int> utf32EncodedBytes, [int offset = 0, int length]) {
122 return hasUtf16beBom(utf32EncodedBytes, offset, length) ||
123 hasUtf16leBom(utf32EncodedBytes, offset, length);
124 }
125
126 bool hasUtf16beBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) {
127 if (!(offset >= 0)) {
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 &&
140 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_HI &&
141 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_LO;
142 }
143
144 bool hasUtf16leBom(List<int> utf16EncodedBytes, [int offset = 0, int length]) {
145 if (!(offset >= 0)) {
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 &&
158 utf16EncodedBytes[offset] == UNICODE_UTF_BOM_LO &&
159 utf16EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI;
160 }
161
162 int _sizeCodeUnits(int utf16CodeUnitsLength) {
163 int v = ((utf16CodeUnitsLength)/2).floor().toInt();
164 return v;
165 }
166
167 List<int> _stringToUtf16CodeUnits(String str) {
168 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
169 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
170 // removing after this issue is resolved.
171 if (is16BitCodeUnit()) {
172 return str.charCodes();
173 } else {
174 return codepointsToUtf16CodeUnits(str.charCodes());
175 }
176 }
177
178 /**
179 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes
180 * to produce the code unit (0-(2^16)-1).
181 */
182 List<int> _utf16beToUtf16CodeUnits(
183 List<int> utf16beEncodedBytes, [int offset = 0, int length,
184 bool stripBom = true]) {
185 if (!(offset >= 0)) {
186 throw new IllegalArgumentException("offset");
187 }
188
189 if (!(length == null || length >= 0)) {
190 throw new IllegalArgumentException("length");
191 }
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 }
210
211 /**
212 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes
213 * to produce the code unit (0-(2^16)-1).
214 */
215 List<int> _utf16leToUtf16CodeUnits(
216 List<int> utf16leEncodedBytes, [int offset = 0, int length,
217 bool stripBom = true]) {
218 if (!(offset >= 0)) {
219 throw new IllegalArgumentException("offset");
220 }
221
222 if (!(length == null || length >= 0)) {
223 throw new IllegalArgumentException("length");
224 }
225
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 }
273 }
OLDNEW
« no previous file with comments | « utils/string_encoding/Unicode.dart ('k') | utils/string_encoding/Utf32.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698