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

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

Issue 9233041: String encoding utility methods and tests for Unicode, UTF-8, -16 and -32. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updates from initial review. 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
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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("UnicodeCore.dart");
7 #import("Unicode.dart");
8
9 /**
10 * Produce a String from a sequence of UTF16 encoded bytes.
11 */
12 String decodeFromUtf16(List<int> bytes) {
13 List<int> codeUnits = _utf16ToUtf16CodeUnits(bytes);
14 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
15 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
16 // removing after this issue is resolved.
17 if (is16BitCodeUnit()) {
18 return new String.fromCharCodes(codeUnits);
19 } else {
20 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
21 }
22 }
23
24 /**
25 * Produce a String from a sequence of UTF16-BE encoded bytes.
26 */
27 String decodeFromUtf16be(List<int> bytes) {
28 List<int> codeUnits = _utf16beToUtf16CodeUnits(bytes);
29 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
30 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
31 // removing after this issue is resolved.
32 if (is16BitCodeUnit()) {
33 return new String.fromCharCodes(codeUnits);
34 } else {
35 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
36 }
37 }
38
39 /**
40 * Produce a String from a sequence of UTF16-LE encoded bytes.
41 */
42 String decodeFromUtf16le(List<int> bytes) {
43 List<int> codeUnits = _utf16leToUtf16CodeUnits(bytes);
44 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
45 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
46 // removing after this issue is resolved.
47 if (is16BitCodeUnit()) {
48 return new String.fromCharCodes(codeUnits);
49 } else {
50 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits));
51 }
52 }
53
54 /**
55 * Produce a sequence of UTF16 encoded bytes.
56 */
57 List<int> encodeAsUtf16(String str) => encodeAsUtf16be(str);
58
59 /**
60 * Produce a sequence of UTF16-BE encoded bytes.
61 */
62 List<int> encodeAsUtf16be(String str) {
63 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
64 List<int> encoding = <int>[0xfe, 0xff];
65 for (int unit in utf16CodeUnits) {
66 encoding.add((unit & 0xff00) >> 8);
67 encoding.add(unit & 0xff);
68 }
69 return encoding;
70 }
71
72 /**
73 * Produce a sequence of UTF16-LE encoded bytes.
74 */
75 List<int> encodeAsUtf16le(String str) {
76 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str);
77 List<int> encoding = <int>[0xff, 0xfe];
78 for (int unit in utf16CodeUnits) {
79 encoding.add(unit & 0xff);
80 encoding.add((unit & 0xff00) >> 8);
81 }
82 return encoding;
83 }
84
85 List<int> _stringToUtf16CodeUnits(String str) {
86 List<int> codepoints = <int>[];
87 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc
88 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
89 // removing after this issue is resolved.
90 if (is16BitCodeUnit()) {
91 return str.charCodes();
92 } else {
93 return codepointsToUtf16CodeUnits(str.charCodes());
94 }
95 }
96
97 /**
98 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes
99 * to produce the code unit (0-(2^16)-1).
100 */
101 List<int> _utf16beToUtf16CodeUnits(List<int> utf16beEncodedBytes,
102 [int start = 0, int length = - 1]) {
103 List<int> codeUnits = <int>[];
104 int end = length >= 0 ?
105 Math.min(utf16beEncodedBytes.length, start + length) :
106 utf16beEncodedBytes.length;
107 int i = start;
108 int lastIndex = end - 1;
109 while (i < lastIndex) {
110 int hi = utf16beEncodedBytes[i++];
111 int lo = utf16beEncodedBytes[i++];
112 codeUnits.add((hi * 256) + lo);
113 }
114 return codeUnits;
115 }
116
117 /**
118 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes
119 * to produce the code unit (0-(2^16)-1).
120 */
121 List<int> _utf16leToUtf16CodeUnits(List<int> utf16leEncodedBytes,
122 [int start = 0, int length = - 1]) {
123 List<int> codeUnits = <int>[];
124 int end = length >= 0 ?
125 Math.min(utf16leEncodedBytes.length, start + length) :
126 utf16leEncodedBytes.length;
127 int i = start;
128 int lastIndex = end - 1;
129 while (i < lastIndex) {
130 int lo = utf16leEncodedBytes[i++];
131 int hi = utf16leEncodedBytes[i++];
132 codeUnits.add((hi * 256) + lo);
133 }
134 return codeUnits;
135 }
136
137 /**
138 * Convert UTF-16 encoded bytes to utf16 code units by grouping 1-2 bytes
139 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine
140 * endian-ness, and defaults to BE.
141 */
142 List<int> _utf16ToUtf16CodeUnits(List<int> utf16EncodedBytes,
143 [int start = 0, int length = - 1]) {
144 int end = length >= 0 ?
145 Math.min(utf16EncodedBytes.length, start + length) :
146 utf16EncodedBytes.length;
147
148 if((start + 1 < end) && utf16EncodedBytes[start] == 0xfe &&
149 utf16EncodedBytes[start + 1] == 0xff) {
150 return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start + 2,
151 end - (start + 2));
152 } else if((start + 2 <= end) && utf16EncodedBytes[start] == 0xff &&
153 utf16EncodedBytes[start + 1] == 0xfe) {
154 return _utf16leToUtf16CodeUnits(utf16EncodedBytes, start + 2,
155 end - (start + 2));
156 } else {
157 return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start, end - start);
158 }
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698