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

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

Issue 11368138: Add some support for the code-point code-unit distinction. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Implemented feedback from patch set 3 Created 8 years, 1 month 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
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 // TODO(jmesserly): would be nice to have this on String (dartbug.com/6501).
6 /**
7 * Provide a list of Unicode codepoints for a given string.
8 */
9 List<int> stringToCodepoints(String str) {
10 // Note: str.charCodes gives us 16-bit code units on all Dart implementations.
11 // So we need to convert. The same is not true of "new String.fromCharCodes",
12 // which accepts code points on the VM but not dart2js (dartbug.com/1357).
13 return _utf16CodeUnitsToCodepoints(str.charCodes);
14 }
15
16 /**
17 * Generate a string from the provided Unicode codepoints.
18 */
19 String codepointsToString(List<int> codepoints) {
20 // TODO _is16BitCodeUnit() is used to work around a bug with dart2js
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(
25 _codepointsToUtf16CodeUnits(codepoints));
26 } else {
27 return new String.fromCharCodes(codepoints);
28 }
29 }
30
31 /*
32 * Test for presence of bug related to the use of UTF-16 code units for
33 * Dart compiled to JS.
34 */
35 bool _test16BitCodeUnit = null;
36 // TODO _is16BitCodeUnit() is used to work around a bug with dart2js
37 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider
38 // removing after this issue is resolved.
39 bool _is16BitCodeUnit() {
40 if (_test16BitCodeUnit == null) {
41 _test16BitCodeUnit = (new String.fromCharCodes([0x1D11E])) ==
42 (new String.fromCharCodes([0xD11E]));
43 }
44 return _test16BitCodeUnit;
45 }
46
47 /** 5 /**
48 * Invalid codepoints or encodings may be substituted with the value U+fffd. 6 * Invalid codepoints or encodings may be substituted with the value U+fffd.
49 */ 7 */
50 const int UNICODE_REPLACEMENT_CHARACTER_CODEPOINT = 0xfffd; 8 const int UNICODE_REPLACEMENT_CHARACTER_CODEPOINT = 0xfffd;
51 const int UNICODE_BOM = 0xfeff; 9 const int UNICODE_BOM = 0xfeff;
52 const int UNICODE_UTF_BOM_LO = 0xff; 10 const int UNICODE_UTF_BOM_LO = 0xff;
53 const int UNICODE_UTF_BOM_HI = 0xfe; 11 const int UNICODE_UTF_BOM_HI = 0xfe;
54 12
55 const int UNICODE_BYTE_ZERO_MASK = 0xff; 13 const int UNICODE_BYTE_ZERO_MASK = 0xff;
56 const int UNICODE_BYTE_ONE_MASK = 0xff00; 14 const int UNICODE_BYTE_ONE_MASK = 0xff00;
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 _offset -= by; 219 _offset -= by;
262 } 220 }
263 221
264 int get remaining => _end - _offset; 222 int get remaining => _end - _offset;
265 223
266 void skip([int count = 1]) { 224 void skip([int count = 1]) {
267 _offset += count; 225 _offset += count;
268 } 226 }
269 } 227 }
270 228
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698