Chromium Code Reviews| 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("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 if (is16BitCodeUnit()) { | |
| 15 return new String.fromCharCodes(codeUnits); | |
|
Dan Rice
2012/01/31 15:59:55
Comment that this account for platform-specific St
dcarlson
2012/01/31 22:11:38
Done.
| |
| 16 } else { | |
| 17 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits)); | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * Produce a String from a sequence of UTF16-BE encoded bytes. | |
| 23 */ | |
| 24 String decodeFromUtf16be(List<int> bytes) { | |
| 25 List<int> codeUnits = _utf16beToUtf16CodeUnits(bytes); | |
| 26 if (is16BitCodeUnit()) { | |
| 27 return new String.fromCharCodes(codeUnits); | |
|
Dan Rice
2012/01/31 15:59:55
Ditto
dcarlson
2012/01/31 22:11:38
Done.
| |
| 28 } else { | |
| 29 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits)); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Produce a String from a sequence of UTF16-LE encoded bytes. | |
| 35 */ | |
| 36 String decodeFromUtf16le(List<int> bytes) { | |
| 37 List<int> codeUnits = _utf16leToUtf16CodeUnits(bytes); | |
| 38 if (is16BitCodeUnit()) { | |
| 39 return new String.fromCharCodes(codeUnits); | |
| 40 } else { | |
| 41 return new String.fromCharCodes(utf16CodeUnitsToCodepoints(codeUnits)); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Produce a sequence of UTF16 encoded bytes. | |
| 47 */ | |
| 48 List<int> encodeAsUtf16(String str) => encodeAsUtf16be(str); | |
| 49 | |
| 50 /** | |
| 51 * Produce a sequence of UTF16-BE encoded bytes. | |
| 52 */ | |
| 53 List<int> encodeAsUtf16be(String str) { | |
| 54 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | |
| 55 List<int> encoding = <int>[0xfe, 0xff]; | |
|
jat
2012/01/31 15:19:22
Is this supposed to have BOM? I thought UTF16-BE
Dan Rice
2012/01/31 15:59:55
Done.
dcarlson
2012/01/31 22:11:38
It is the BOM -- not required, but currently stri
jat
2012/01/31 22:46:12
My recollection is some libraries treat the presen
dcarlson
2012/02/01 22:18:46
So, I'm switching things up a little. I want to pr
| |
| 56 for (int unit in utf16CodeUnits) { | |
| 57 encoding.add((unit & 0xff00) >> 8); | |
| 58 encoding.add(unit & 0xff); | |
| 59 } | |
| 60 return encoding; | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Produce a sequence of UTF16-LE encoded bytes. | |
| 65 */ | |
| 66 List<int> encodeAsUtf16le(String str) { | |
| 67 List<int> utf16CodeUnits = _stringToUtf16CodeUnits(str); | |
| 68 List<int> encoding = <int>[0xff, 0xfe]; | |
| 69 for (int unit in utf16CodeUnits) { | |
| 70 encoding.add(unit & 0xff); | |
| 71 encoding.add((unit & 0xff00) >> 8); | |
| 72 } | |
| 73 return encoding; | |
| 74 } | |
| 75 | |
| 76 List<int> _stringToUtf16CodeUnits(String str) { | |
| 77 List<int> codepoints = <int>[]; | |
| 78 if (is16BitCodeUnit()) { | |
| 79 return str.charCodes(); | |
| 80 } else { | |
| 81 return codepointsToUtf16CodeUnits(str.charCodes()); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Convert UTF-16BE encoded bytes to utf16 code units by grouping 1-2 bytes | |
| 87 * to produce the code unit (0-(2^16)-1). | |
| 88 */ | |
| 89 List<int> _utf16beToUtf16CodeUnits(List<int> utf16beEncodedBytes, | |
| 90 [int start = 0, int length = - 1]) { | |
|
Dan Rice
2012/01/31 15:59:55
length = null
dcarlson
2012/01/31 22:11:38
Done.
| |
| 91 List<int> codeUnits = <int>[]; | |
| 92 int end = length >= 0 ? | |
| 93 Math.min(utf16beEncodedBytes.length, start + length) : | |
| 94 utf16beEncodedBytes.length; | |
| 95 int i = start; | |
| 96 int lastIndex = end - 1; | |
| 97 while (i < lastIndex) { | |
| 98 int hi = utf16beEncodedBytes[i++]; | |
| 99 int lo = utf16beEncodedBytes[i++]; | |
| 100 codeUnits.add((hi * 256) + lo); | |
|
Dan Rice
2012/01/31 15:59:55
use shift?
dcarlson
2012/01/31 22:11:38
Done.
| |
| 101 } | |
| 102 return codeUnits; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Convert UTF-16LE encoded bytes to utf16 code units by grouping 1-2 bytes | |
| 107 * to produce the code unit (0-(2^16)-1). | |
| 108 */ | |
| 109 List<int> _utf16leToUtf16CodeUnits(List<int> utf16leEncodedBytes, | |
| 110 [int start = 0, int length = - 1]) { | |
|
Dan Rice
2012/01/31 15:59:55
length = null
dcarlson
2012/01/31 22:11:38
Done.
| |
| 111 List<int> codeUnits = <int>[]; | |
| 112 int end = length >= 0 ? | |
| 113 Math.min(utf16leEncodedBytes.length, start + length) : | |
| 114 utf16leEncodedBytes.length; | |
| 115 int i = start; | |
| 116 int lastIndex = end - 1; | |
| 117 while (i < lastIndex) { | |
| 118 int lo = utf16leEncodedBytes[i++]; | |
| 119 int hi = utf16leEncodedBytes[i++]; | |
| 120 codeUnits.add((hi * 256) + lo); | |
| 121 } | |
| 122 return codeUnits; | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Convert UTF-16 encoded bytes to utf16 code units by grouping 1-2 bytes | |
| 127 * to produce the code unit (0-(2^16)-1). Relies on BOM to determine | |
| 128 * endian-ness, and defaults to BE. | |
| 129 */ | |
| 130 List<int> _utf16ToUtf16CodeUnits(List<int> utf16EncodedBytes, | |
| 131 [int start = 0, int length = - 1]) { | |
| 132 int end = length >= 0 ? | |
| 133 Math.min(utf16EncodedBytes.length, start + length) : | |
| 134 utf16EncodedBytes.length; | |
| 135 | |
| 136 if((start + 1 < end) && utf16EncodedBytes[start] == 0xfe && | |
|
Dan Rice
2012/01/31 15:59:55
use 'start + 1 < end' or 'start + 2 <= end' consis
dcarlson
2012/01/31 22:11:38
Done.
| |
| 137 utf16EncodedBytes[start + 1] == 0xff) { | |
| 138 return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start + 2, | |
| 139 end - (start + 2)); | |
| 140 } else if((start + 2 <= end) && utf16EncodedBytes[start] == 0xff && | |
| 141 utf16EncodedBytes[start + 1] == 0xfe) { | |
| 142 return _utf16leToUtf16CodeUnits(utf16EncodedBytes, start + 2, | |
| 143 end - (start + 2)); | |
| 144 } else { | |
| 145 return _utf16beToUtf16CodeUnits(utf16EncodedBytes, start, end - start); | |
| 146 } | |
| 147 } | |
| OLD | NEW |