| OLD | NEW |
| (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("UnicodeCore"); |
| 6 |
| 7 /* |
| 8 * Test for presence of bug related to the use of UTF-16 code units for |
| 9 * Dart compiled to JS. |
| 10 */ |
| 11 bool _test16BitCodeUnit = null; |
| 12 // TODO is16BitCodeUnit() is used to work around a bug with frog/dartc |
| 13 // (http://code.google.com/p/dart/issues/detail?id=1357). Consider |
| 14 // removing after this issue is resolved. |
| 15 bool is16BitCodeUnit() { |
| 16 if(_test16BitCodeUnit == null) { |
| 17 _test16BitCodeUnit = (new String.fromCharCodes([0x1D11E])) == |
| 18 (new String.fromCharCodes([0xD11E])); |
| 19 } |
| 20 return _test16BitCodeUnit; |
| 21 } |
| 22 |
| 23 |
| 24 /** |
| 25 * Invalid codepoints or encodings may be substituted with the value U+fffd. |
| 26 */ |
| 27 final int REPLACEMENT_CHARACTER_CODEPOINT = 0xfffd; |
| 28 |
| 29 /** |
| 30 * Encode code points as UTF16 code units. |
| 31 */ |
| 32 List<int> codepointsToUtf16CodeUnits(List<int> codepoints, |
| 33 [int start = 0, int length = -1]) { |
| 34 List<int> codeUnitsBuffer = <int>[]; |
| 35 int end = length >= 0 ? Math.min(codepoints.length, start + length) : |
| 36 codepoints.length; |
| 37 int i = start; |
| 38 while (i < end) { |
| 39 int value = codepoints[i++]; |
| 40 if ((value >= 0 && value <0xd800) || (value >= 0xe000 && value <0x10000)) { |
| 41 codeUnitsBuffer.add(value); |
| 42 } else if (value >= 0x10000 && value < 0x110000){ |
| 43 int base = value - 0x10000; |
| 44 codeUnitsBuffer.add(0xd800 + ((base & 0xffc00) >> 10)); |
| 45 codeUnitsBuffer.add(0xdc00 + (base & 0x3ff)); |
| 46 } else { |
| 47 codeUnitsBuffer.add(REPLACEMENT_CHARACTER_CODEPOINT); |
| 48 } |
| 49 } |
| 50 return codeUnitsBuffer; |
| 51 } |
| 52 |
| 53 /** |
| 54 * Decodes the utf16 codeunits to codepoints. |
| 55 */ |
| 56 List<int> utf16CodeUnitsToCodepoints(List<int> utf16CodeUnits, |
| 57 [int start = 0, int length = - 1]) { |
| 58 List<int> codepointBuffer = <int>[]; |
| 59 |
| 60 int end = length >= 0 ? |
| 61 Math.min(utf16CodeUnits.length, start + length) : |
| 62 utf16CodeUnits.length; |
| 63 |
| 64 int i = start; |
| 65 // skip the first entry if it is a BOM. |
| 66 if(end > 0 && utf16CodeUnits[0] == 0xfeff) { |
| 67 i++; |
| 68 } |
| 69 while (i < end) { |
| 70 int value = utf16CodeUnits[i++]; |
| 71 if (value >= 0x0) { |
| 72 if (value < 0xd800 || (value >= 0xe000 && value <= 0xffff)) { |
| 73 // transfer directly |
| 74 codepointBuffer.add(value); |
| 75 } else if (value < 0xdc00 && i < end) { |
| 76 // merge surrogate pair |
| 77 value = (value - 0xd800) * 0x400; |
| 78 int nextValue = utf16CodeUnits[i++]; |
| 79 if (nextValue >= 0xdc00 && nextValue < 0xe000) { |
| 80 value += 0x10000 + (nextValue - 0xdc00); |
| 81 codepointBuffer.add(value); |
| 82 } else { |
| 83 if (nextValue >= 0xd800 && nextValue < 0xdc00) i--; |
| 84 codepointBuffer.add(REPLACEMENT_CHARACTER_CODEPOINT); |
| 85 continue; |
| 86 } |
| 87 } else { |
| 88 codepointBuffer.add(REPLACEMENT_CHARACTER_CODEPOINT); |
| 89 continue; |
| 90 } |
| 91 } else { |
| 92 codepointBuffer.add(REPLACEMENT_CHARACTER_CODEPOINT); |
| 93 continue; |
| 94 } |
| 95 } |
| 96 return codepointBuffer; |
| 97 } |
| OLD | NEW |