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

Side by Side Diff: lib/utf/utf_core.dart

Issue 9462001: Unify most of our utf8 implementations. This takes the implementation (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 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 | « lib/utf/utf8.dart ('k') | lib/utf/utf_vm.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #library("unicode_core"); 5 /**
6 * Provide a list of Unicode codepoints for a given string.
7 */
8 List<int> stringToCodepoints(String str) {
9 List<int> codepoints;
10 // TODO _is16BitCodeUnit() is used to work around a bug with frog/dartc
11 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
12 // removing after this issue is resolved.
13 if (_is16BitCodeUnit()) {
14 codepoints = _utf16CodeUnitsToCodepoints(str.charCodes());
15 } else {
16 codepoints = str.charCodes();
17 }
18 return codepoints;
19 }
20
21 /**
22 * Generate a string from the provided Unicode codepoints.
23 */
24 String codepointsToString(List<int> codepoints) {
25 // TODO _is16BitCodeUnit() is used to work around a bug with frog/dartc
26 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
27 // removing after this issue is resolved.
28 if (_is16BitCodeUnit()) {
29 return new String.fromCharCodes(
30 _codepointsToUtf16CodeUnits(codepoints));
31 } else {
32 return new String.fromCharCodes(codepoints);
33 }
34 }
6 35
7 /* 36 /*
8 * Test for presence of bug related to the use of UTF-16 code units for 37 * Test for presence of bug related to the use of UTF-16 code units for
9 * Dart compiled to JS. 38 * Dart compiled to JS.
10 */ 39 */
11 bool _test16BitCodeUnit = null; 40 bool _test16BitCodeUnit = null;
12 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc 41 // TODO _is16BitCodeUnit() is used to work around a bug with frog/dartc
13 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider 42 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
14 // removing after this issue is resolved. 43 // removing after this issue is resolved.
15 bool is16BitCodeUnit() { 44 bool _is16BitCodeUnit() {
16 if (_test16BitCodeUnit == null) { 45 if (_test16BitCodeUnit == null) {
17 _test16BitCodeUnit = (new String.fromCharCodes([0x1D11E])) == 46 _test16BitCodeUnit = (new String.fromCharCodes([0x1D11E])) ==
18 (new String.fromCharCodes([0xD11E])); 47 (new String.fromCharCodes([0xD11E]));
19 } 48 }
20 return _test16BitCodeUnit; 49 return _test16BitCodeUnit;
21 } 50 }
22 51
23 /** 52 /**
24 * Invalid codepoints or encodings may be substituted with the value U+fffd. 53 * Invalid codepoints or encodings may be substituted with the value U+fffd.
25 */ 54 */
(...skipping 10 matching lines...) Expand all
36 final int UNICODE_UTF16_RESERVED_HI = 0xdfff; 65 final int UNICODE_UTF16_RESERVED_HI = 0xdfff;
37 final int UNICODE_UTF16_OFFSET = 0x10000; 66 final int UNICODE_UTF16_OFFSET = 0x10000;
38 final int UNICODE_UTF16_SURROGATE_UNIT_0_BASE = 0xd800; 67 final int UNICODE_UTF16_SURROGATE_UNIT_0_BASE = 0xd800;
39 final int UNICODE_UTF16_SURROGATE_UNIT_1_BASE = 0xdc00; 68 final int UNICODE_UTF16_SURROGATE_UNIT_1_BASE = 0xdc00;
40 final int UNICODE_UTF16_HI_MASK = 0xffc00; 69 final int UNICODE_UTF16_HI_MASK = 0xffc00;
41 final int UNICODE_UTF16_LO_MASK = 0x3ff; 70 final int UNICODE_UTF16_LO_MASK = 0x3ff;
42 71
43 /** 72 /**
44 * Encode code points as UTF16 code units. 73 * Encode code points as UTF16 code units.
45 */ 74 */
46 List<int> codepointsToUtf16CodeUnits( 75 List<int> _codepointsToUtf16CodeUnits(
47 List<int> codepoints, [int offset = 0, int length, 76 List<int> codepoints, [int offset = 0, int length,
48 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { 77 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
49 78
50 ListRange<int> listRange = new ListRange<int>(codepoints, offset, length); 79 _ListRange<int> listRange = new _ListRange<int>(codepoints, offset, length);
51 int encodedLength = 0; 80 int encodedLength = 0;
52 for (int value in listRange) { 81 for (int value in listRange) {
53 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || 82 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) ||
54 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { 83 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) {
55 encodedLength++; 84 encodedLength++;
56 } else if (value > UNICODE_PLANE_ONE_MAX && 85 } else if (value > UNICODE_PLANE_ONE_MAX &&
57 value <= UNICODE_VALID_RANGE_MAX) { 86 value <= UNICODE_VALID_RANGE_MAX) {
58 encodedLength += 2; 87 encodedLength += 2;
59 } else { 88 } else {
60 encodedLength++; 89 encodedLength++;
(...skipping 18 matching lines...) Expand all
79 } else { 108 } else {
80 throw new IllegalArgumentException("Invalid encoding"); 109 throw new IllegalArgumentException("Invalid encoding");
81 } 110 }
82 } 111 }
83 return codeUnitsBuffer; 112 return codeUnitsBuffer;
84 } 113 }
85 114
86 /** 115 /**
87 * Decodes the utf16 codeunits to codepoints. 116 * Decodes the utf16 codeunits to codepoints.
88 */ 117 */
89 List<int> utf16CodeUnitsToCodepoints( 118 List<int> _utf16CodeUnitsToCodepoints(
90 List<int> utf16CodeUnits, [int offset = 0, int length, 119 List<int> utf16CodeUnits, [int offset = 0, int length,
91 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { 120 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) {
92 ListRangeIterator<int> source = 121 _ListRangeIterator<int> source =
93 (new ListRange<int>(utf16CodeUnits, offset, length)).iterator(); 122 (new _ListRange<int>(utf16CodeUnits, offset, length)).iterator();
94 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder 123 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder
95 .fromListRangeIterator(source, replacementCodepoint); 124 .fromListRangeIterator(source, replacementCodepoint);
96 List<int> codepoints = new List<int>(source.remaining); 125 List<int> codepoints = new List<int>(source.remaining);
97 int i = 0; 126 int i = 0;
98 while (decoder.hasNext()) { 127 while (decoder.hasNext()) {
99 codepoints[i++] = decoder.next(); 128 codepoints[i++] = decoder.next();
100 } 129 }
101 if (i == codepoints.length) { 130 if (i == codepoints.length) {
102 return codepoints; 131 return codepoints;
103 } else { 132 } else {
104 List<int> codepointTrunc = new List<int>(i); 133 List<int> codepointTrunc = new List<int>(i);
105 codepointTrunc.setRange(0, i, codepoints); 134 codepointTrunc.setRange(0, i, codepoints);
106 return codepointTrunc; 135 return codepointTrunc;
107 } 136 }
108 } 137 }
109 138
110 /** 139 /**
111 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. 140 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units.
112 * The parameters can override the default Unicode replacement character. Set 141 * The parameters can override the default Unicode replacement character. Set
113 * the replacementCharacter to null to throw an IllegalArgumentException 142 * the replacementCharacter to null to throw an IllegalArgumentException
114 * rather than replace the bad value. 143 * rather than replace the bad value.
115 */ 144 */
116 class Utf16CodeUnitDecoder implements Iterator<int> { 145 class Utf16CodeUnitDecoder implements Iterator<int> {
117 final ListRangeIterator<int> utf16CodeUnitIterator; 146 final _ListRangeIterator<int> utf16CodeUnitIterator;
118 final int replacementCodepoint; 147 final int replacementCodepoint;
119 148
120 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, 149 Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length,
121 int this.replacementCodepoint = 150 int this.replacementCodepoint =
122 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : 151 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
123 utf16CodeUnitIterator = (new ListRange(utf16CodeUnits, offset, length)) 152 utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length))
124 .iterator(); 153 .iterator();
125 154
126 Utf16CodeUnitDecoder.fromListRangeIterator( 155 Utf16CodeUnitDecoder.fromListRangeIterator(
127 ListRangeIterator<int> this.utf16CodeUnitIterator, 156 _ListRangeIterator<int> this.utf16CodeUnitIterator,
128 int this.replacementCodepoint); 157 int this.replacementCodepoint);
129 158
130 Iterator<int> iterator() => this; 159 Iterator<int> iterator() => this;
131 160
132 bool hasNext() => utf16CodeUnitIterator.hasNext(); 161 bool hasNext() => utf16CodeUnitIterator.hasNext();
133 162
134 int next() { 163 int next() {
135 int value = utf16CodeUnitIterator.next(); 164 int value = utf16CodeUnitIterator.next();
136 if (value < 0) { 165 if (value < 0) {
137 if (replacementCodepoint != null) { 166 if (replacementCodepoint != null) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 } else if (replacementCodepoint != null) { 198 } else if (replacementCodepoint != null) {
170 return replacementCodepoint; 199 return replacementCodepoint;
171 } else { 200 } else {
172 throw new IllegalArgumentException( 201 throw new IllegalArgumentException(
173 "Invalid UTF16 at ${utf16CodeUnitIterator.position}"); 202 "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
174 } 203 }
175 } 204 }
176 } 205 }
177 206
178 /** 207 /**
179 * ListRange in an internal type used to create a lightweight Interable on a 208 * _ListRange in an internal type used to create a lightweight Interable on a
180 * range within a source list. DO NOT MODIFY the underlying list while 209 * range within a source list. DO NOT MODIFY the underlying list while
181 * iterating over it. The results of doing so are undefined. 210 * iterating over it. The results of doing so are undefined.
182 */ 211 */
183 class ListRange<T> implements Iterable<T> { 212 class _ListRange<T> implements Iterable<T> {
184 final List<T> _source; 213 final List<T> _source;
185 final int _offset; 214 final int _offset;
186 final int _length; 215 final int _length;
187 216
188 ListRange(List<T> source, [int offset = 0, int length]) : 217 _ListRange(List<T> source, [int offset = 0, int length]) :
189 this._source = source, this._offset = offset, 218 this._source = source, this._offset = offset,
190 this._length = (length == null ? source.length - offset : length) { 219 this._length = (length == null ? source.length - offset : length) {
191 if (_offset < 0 || _offset > _source.length) { 220 if (_offset < 0 || _offset > _source.length) {
192 throw new IndexOutOfRangeException("offset out of range (< 0)"); 221 throw new IndexOutOfRangeException("offset out of range (< 0)");
193 } 222 }
194 if (_length != null && (_length < 0)) { 223 if (_length != null && (_length < 0)) {
195 throw new IndexOutOfRangeException("length out of range (< 0)"); 224 throw new IndexOutOfRangeException("length out of range (< 0)");
196 } 225 }
197 if (_length + _offset > _source.length) { 226 if (_length + _offset > _source.length) {
198 throw new IndexOutOfRangeException("offset + length > source.length"); 227 throw new IndexOutOfRangeException("offset + length > source.length");
199 } 228 }
200 } 229 }
201 230
202 ListRangeIterator<T> iterator() => 231 _ListRangeIterator<T> iterator() =>
203 new ListRangeIteratorImpl(_source, _offset, _offset + _length); 232 new _ListRangeIteratorImpl(_source, _offset, _offset + _length);
204 233
205 int get length() => _length; 234 int get length() => _length;
206 } 235 }
207 236
208 /** 237 /**
209 * The ListRangeIterator provides more capabilities than a standard iterator, 238 * The _ListRangeIterator provides more capabilities than a standard iterator,
210 * including the ability to get the current position, count remaining items, 239 * including the ability to get the current position, count remaining items,
211 * and move forward/backward within the iterator. 240 * and move forward/backward within the iterator.
212 */ 241 */
213 interface ListRangeIterator<T> extends Iterator<T> { 242 interface _ListRangeIterator<T> extends Iterator<T> {
214 bool hasNext(); 243 bool hasNext();
215 T next(); 244 T next();
216 int get position(); 245 int get position();
217 void backup([int by]); 246 void backup([int by]);
218 int get remaining(); 247 int get remaining();
219 void skip([int count]); 248 void skip([int count]);
220 } 249 }
221 250
222 class ListRangeIteratorImpl<T> implements ListRangeIterator<T> { 251 class _ListRangeIteratorImpl<T> implements _ListRangeIterator<T> {
223 final List<T> _source; 252 final List<T> _source;
224 int _offset; 253 int _offset;
225 final int _end; 254 final int _end;
226 255
227 ListRangeIteratorImpl(List<T> source, int offset, int end) : 256 _ListRangeIteratorImpl(List<T> source, int offset, int end) :
228 _source = source, _offset = offset, _end = end; 257 _source = source, _offset = offset, _end = end;
229 258
230 bool hasNext() => _offset < _end; 259 bool hasNext() => _offset < _end;
231 T next() => _source[_offset++]; 260 T next() => _source[_offset++];
232 int get position() => _offset; 261 int get position() => _offset;
233 void backup([int by = 1]) { 262 void backup([int by = 1]) {
234 _offset -= by; 263 _offset -= by;
235 } 264 }
236 int get remaining() => _end - _offset; 265 int get remaining() => _end - _offset;
237 void skip([int count = 1]) { 266 void skip([int count = 1]) {
238 _offset += count; 267 _offset += count;
239 } 268 }
240 } 269 }
270
OLDNEW
« no previous file with comments | « lib/utf/utf8.dart ('k') | lib/utf/utf_vm.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698