| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library("utf32"); | |
| 6 #import("unicode_core.dart"); | |
| 7 #import("unicode.dart"); | |
| 8 | |
| 9 /** | |
| 10 * Produce a String from a sequence of UTF-32 encoded bytes. The parameters | |
| 11 * allow an offset into a list of bytes (as int), limiting the length of the | |
| 12 * values be decoded and the ability of override the default Unicode | |
| 13 * replacement character. Set the replacementCharacter to null to throw an | |
| 14 * IllegalArgumentException rather than replace the bad value. | |
| 15 */ | |
| 16 String decodeFromUtf32(List<int> bytes, [int offset = 0, int length, | |
| 17 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | |
| 18 codepointsToString(_utf32ToCodePoints(bytes, offset, length, | |
| 19 replacementCodepoint)); | |
| 20 | |
| 21 /** | |
| 22 * Produce a String from a sequence of UTF-32BE encoded bytes. The parameters | |
| 23 * allow an offset into a list of bytes (as int), limiting the length of the | |
| 24 * values be decoded and the ability of override the default Unicode | |
| 25 * replacement character. Set the replacementCharacter to null to throw an | |
| 26 * IllegalArgumentException rather than replace the bad value. | |
| 27 */ | |
| 28 String decodeFromUtf32be( | |
| 29 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | |
| 30 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | |
| 31 codepointsToString(_utf32beToCodePoints(bytes, offset, length, stripBom, | |
| 32 replacementCodepoint)); | |
| 33 | |
| 34 /** | |
| 35 * Produce a String from a sequence of UTF-32LE encoded bytes. The parameters | |
| 36 * allow an offset into a list of bytes (as int), limiting the length of the | |
| 37 * values be decoded and the ability of override the default Unicode | |
| 38 * replacement character. Set the replacementCharacter to null to throw an | |
| 39 * IllegalArgumentException rather than replace the bad value. | |
| 40 */ | |
| 41 String decodeFromUtf32le( | |
| 42 List<int> bytes, [int offset = 0, int length, bool stripBom = true, | |
| 43 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) => | |
| 44 codepointsToString(_utf32leToCodePoints(bytes, offset, length, stripBom, | |
| 45 replacementCodepoint)); | |
| 46 | |
| 47 /** | |
| 48 * Produce a sequence of UTF-32 encoded bytes. | |
| 49 */ | |
| 50 List<int> encodeAsUtf32(String str) => | |
| 51 encodeAsUtf32be(str, true); | |
| 52 | |
| 53 /** | |
| 54 * Produce a sequence of UTF-32BE encoded bytes. | |
| 55 */ | |
| 56 List<int> encodeAsUtf32be(String str, [bool writeBOM = false]) { | |
| 57 List<int> utf32CodeUnits = stringToCodepoints(str); | |
| 58 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + | |
| 59 (writeBOM ? 4 : 0)); | |
| 60 int i = 0; | |
| 61 if (writeBOM) { | |
| 62 encoding[i++] = 0; | |
| 63 encoding[i++] = 0; | |
| 64 encoding[i++] = UNICODE_UTF_BOM_HI; | |
| 65 encoding[i++] = UNICODE_UTF_BOM_LO; | |
| 66 } | |
| 67 for (int unit in utf32CodeUnits) { | |
| 68 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; | |
| 69 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; | |
| 70 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; | |
| 71 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
| 72 } | |
| 73 return encoding; | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Produce a sequence of UTF-32LE encoded bytes. | |
| 78 */ | |
| 79 List<int> encodeAsUtf32le(String str, [bool writeBOM = false]) { | |
| 80 List<int> utf32CodeUnits = stringToCodepoints(str); | |
| 81 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + | |
| 82 (writeBOM ? 4 : 0)); | |
| 83 int i = 0; | |
| 84 if (writeBOM) { | |
| 85 encoding[i++] = UNICODE_UTF_BOM_LO; | |
| 86 encoding[i++] = UNICODE_UTF_BOM_HI; | |
| 87 encoding[i++] = 0; | |
| 88 encoding[i++] = 0; | |
| 89 } | |
| 90 for (int unit in utf32CodeUnits) { | |
| 91 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; | |
| 92 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; | |
| 93 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; | |
| 94 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; | |
| 95 } | |
| 96 return encoding; | |
| 97 } | |
| 98 | |
| 99 bool hasUtf32Bom( | |
| 100 List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
| 101 return hasUtf32beBom(utf32EncodedBytes, offset, length) || | |
| 102 hasUtf32leBom(utf32EncodedBytes, offset, length); | |
| 103 } | |
| 104 | |
| 105 bool hasUtf32beBom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
| 106 if (!(offset >= 0)) { | |
| 107 throw new IllegalArgumentException("offset"); | |
| 108 } | |
| 109 | |
| 110 if (!(length == null || length >= 0)) { | |
| 111 throw new IllegalArgumentException("length"); | |
| 112 } | |
| 113 | |
| 114 int end = length != null ? | |
| 115 Math.min(utf32EncodedBytes.length, offset + length) : | |
| 116 utf32EncodedBytes.length; | |
| 117 | |
| 118 return (offset + 4) <= end && | |
| 119 utf32EncodedBytes[offset] == 0 && | |
| 120 utf32EncodedBytes[offset + 1] == 0 && | |
| 121 utf32EncodedBytes[offset + 2] == UNICODE_UTF_BOM_HI && | |
| 122 utf32EncodedBytes[offset + 3] == UNICODE_UTF_BOM_LO; | |
| 123 } | |
| 124 | |
| 125 bool hasUtf32leBom(List<int> utf32EncodedBytes, [int offset = 0, int length]) { | |
| 126 if (!(offset >= 0)) { | |
| 127 throw new IllegalArgumentException("offset"); | |
| 128 } | |
| 129 | |
| 130 if (!(length == null || length >= 0)) { | |
| 131 throw new IllegalArgumentException("length"); | |
| 132 } | |
| 133 | |
| 134 int end = length != null ? | |
| 135 Math.min(utf32EncodedBytes.length, offset + length) : | |
| 136 utf32EncodedBytes.length; | |
| 137 | |
| 138 return (offset + 4) <= end && | |
| 139 utf32EncodedBytes[offset] == UNICODE_UTF_BOM_LO && | |
| 140 utf32EncodedBytes[offset + 1] == UNICODE_UTF_BOM_HI && | |
| 141 utf32EncodedBytes[offset + 2] == 0 && | |
| 142 utf32EncodedBytes[offset + 3] == 0; | |
| 143 } | |
| 144 | |
| 145 void _addReplacementCodepoint(List<int> codepointBuffer, int offset, | |
| 146 int replacementCodepoint) { | |
| 147 if(replacementCodepoint != null) { | |
| 148 codepointBuffer[offset] = replacementCodepoint; | |
| 149 } else { | |
| 150 throw new IllegalArgumentException("Invalid encoding"); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 int _sizeCodepoints(int utf32BytesLength) => | |
| 155 ((utf32BytesLength)/4).ceil().toInt(); | |
| 156 | |
| 157 /** | |
| 158 * Joins groups of 4 bytes (0-255) UTF-32BE to produce single code points. | |
| 159 */ | |
| 160 List<int> _utf32beToCodePoints( | |
| 161 List<int> utf32beEncodedBytes, [int offset = 0, int length, | |
| 162 bool stripBom = true, | |
| 163 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 164 if (!(offset >= 0)) { | |
| 165 throw new IllegalArgumentException("offset"); | |
| 166 } | |
| 167 | |
| 168 if (!(length == null || length >= 0)) { | |
| 169 throw new IllegalArgumentException("length"); | |
| 170 } | |
| 171 | |
| 172 int end = length != null ? | |
| 173 Math.min(utf32beEncodedBytes.length, offset + length) : | |
| 174 utf32beEncodedBytes.length; | |
| 175 | |
| 176 int i = (stripBom && hasUtf32beBom(utf32beEncodedBytes, offset, length)) ? | |
| 177 offset + 4 : offset; | |
| 178 int lastIndex = end - 3; | |
| 179 List<int> codepoints = new List<int>(_sizeCodepoints(end - i)); | |
| 180 int j = 0; | |
| 181 while (i < lastIndex) { | |
| 182 int value = utf32beEncodedBytes[i++]; | |
| 183 value = (value << 8) + utf32beEncodedBytes[i++]; | |
| 184 value = (value << 8) + utf32beEncodedBytes[i++]; | |
| 185 value = (value << 8) + utf32beEncodedBytes[i++]; | |
| 186 if (_validCodepoint(value)) { | |
| 187 codepoints[j++] = value; | |
| 188 } else { | |
| 189 _addReplacementCodepoint(codepoints, j++, replacementCodepoint); | |
| 190 } | |
| 191 } | |
| 192 while (j < codepoints.length) { | |
| 193 _addReplacementCodepoint(codepoints, j++, replacementCodepoint); | |
| 194 } | |
| 195 return codepoints; | |
| 196 } | |
| 197 | |
| 198 /** | |
| 199 * Joins groups of 4 bytes (0-255) UTF-32LE to produce single code points. | |
| 200 */ | |
| 201 List<int> _utf32leToCodePoints( | |
| 202 List<int> utf32leEncodedBytes, [int offset = 0, int length, | |
| 203 bool stripBom = true, | |
| 204 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 205 if (!(offset >= 0)) { | |
| 206 throw new IllegalArgumentException("offset"); | |
| 207 } | |
| 208 | |
| 209 if (!(length == null || length >= 0)) { | |
| 210 throw new IllegalArgumentException("length"); | |
| 211 } | |
| 212 | |
| 213 int end = length != null ? | |
| 214 Math.min(utf32leEncodedBytes.length, offset + length) : | |
| 215 utf32leEncodedBytes.length; | |
| 216 | |
| 217 int i = (stripBom && hasUtf32leBom(utf32leEncodedBytes, offset, length)) ? | |
| 218 offset + 4 : offset; | |
| 219 int lastIndex = end - 3; | |
| 220 List<int> codepoints = new List<int>(_sizeCodepoints(end - i)); | |
| 221 int j = 0; | |
| 222 while (i < lastIndex) { | |
| 223 int value = utf32leEncodedBytes[i+3]; | |
| 224 value = (value << 8) + utf32leEncodedBytes[i+2]; | |
| 225 value = (value << 8) + utf32leEncodedBytes[i+1]; | |
| 226 value = (value << 8) + utf32leEncodedBytes[i]; | |
| 227 i += 4; | |
| 228 if (_validCodepoint(value)) { | |
| 229 codepoints[j++] = value; | |
| 230 } else { | |
| 231 _addReplacementCodepoint(codepoints, j++, replacementCodepoint); | |
| 232 } | |
| 233 } | |
| 234 while (j < codepoints.length) { | |
| 235 _addReplacementCodepoint(codepoints, j++, replacementCodepoint); | |
| 236 } | |
| 237 return codepoints; | |
| 238 } | |
| 239 | |
| 240 /** | |
| 241 * Joins groups of 4 bytes (0-255) UTF-32 to produce single code points. | |
| 242 */ | |
| 243 List<int> _utf32ToCodePoints(List<int> utf32EncodedBytes, [int offset = 0, | |
| 244 int length, | |
| 245 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | |
| 246 if (!(offset >= 0)) { | |
| 247 throw new IllegalArgumentException("offset"); | |
| 248 } | |
| 249 | |
| 250 if (!(length == null || length >= 0)) { | |
| 251 throw new IllegalArgumentException("length"); | |
| 252 } | |
| 253 | |
| 254 int end = length != null ? | |
| 255 Math.min(utf32EncodedBytes.length, offset + length) : | |
| 256 utf32EncodedBytes.length; | |
| 257 | |
| 258 if (hasUtf32beBom(utf32EncodedBytes, offset, length)) { | |
| 259 return _utf32beToCodePoints(utf32EncodedBytes, offset + 4, | |
| 260 end - (offset + 4), false); | |
| 261 } else if (hasUtf32leBom(utf32EncodedBytes, offset, length)) { | |
| 262 return _utf32leToCodePoints(utf32EncodedBytes, offset + 4, | |
| 263 end - (offset + 4), false); | |
| 264 } else { | |
| 265 return _utf32beToCodePoints(utf32EncodedBytes, offset, end - offset); | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 bool _validCodepoint(int codepoint) { | |
| 270 return (codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || | |
| 271 (codepoint > UNICODE_UTF16_RESERVED_HI && | |
| 272 codepoint < UNICODE_VALID_RANGE_MAX); | |
| 273 } | |
| OLD | NEW |