| 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("UnicodeCore.dart"); |
| 7 #import("Unicode.dart"); |
| 8 |
| 9 /** |
| 10 * Produce a String from a sequence of UTF32 encoded bytes. |
| 11 */ |
| 12 String decodeFromUtf32(List<int> bytes) => |
| 13 codepointsToString(_utf32ToCodePoints(bytes)); |
| 14 |
| 15 /** |
| 16 * Produce a String from a sequence of UTF32-BE encoded bytes. |
| 17 */ |
| 18 String decodeFromUtf32be(List<int> bytes) => |
| 19 codepointsToString(_utf32beToCodePoints(bytes)); |
| 20 |
| 21 /** |
| 22 * Produce a String from a sequence of UTF32-LE encoded bytes. |
| 23 */ |
| 24 String decodeFromUtf32le(List<int> bytes) => |
| 25 codepointsToString(_utf32leToCodePoints(bytes)); |
| 26 |
| 27 /** |
| 28 * Produce a sequence of UTF32 encoded bytes. |
| 29 */ |
| 30 List<int> encodeAsUtf32(String str, [bool writeBOM = true]) => |
| 31 encodeAsUtf32be(str, writeBOM); |
| 32 |
| 33 /** |
| 34 * Produce a sequence of UTF32-BE encoded bytes. |
| 35 */ |
| 36 List<int> encodeAsUtf32be(String str, [bool writeBOM = false]) { |
| 37 List<int> utf32CodeUnits = stringToCodepoints(str); |
| 38 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + |
| 39 (writeBOM ? 4 : 0)); |
| 40 int i = 0; |
| 41 if (writeBOM) { |
| 42 encoding[i++] = 0; |
| 43 encoding[i++] = 0; |
| 44 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 45 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 46 } |
| 47 for (int unit in utf32CodeUnits) { |
| 48 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; |
| 49 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; |
| 50 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; |
| 51 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 52 } |
| 53 return encoding; |
| 54 } |
| 55 |
| 56 /** |
| 57 * Produce a sequence of UTF32-LE encoded bytes. |
| 58 */ |
| 59 List<int> encodeAsUtf32le(String str, [bool writeBOM = false]) { |
| 60 List<int> utf32CodeUnits = stringToCodepoints(str); |
| 61 List<int> encoding = new List<int>(4 * utf32CodeUnits.length + |
| 62 (writeBOM ? 4 : 0)); |
| 63 int i = 0; |
| 64 if (writeBOM) { |
| 65 encoding[i++] = UNICODE_UTF_BOM_LO; |
| 66 encoding[i++] = UNICODE_UTF_BOM_HI; |
| 67 encoding[i++] = 0; |
| 68 encoding[i++] = 0; |
| 69 } |
| 70 for (int unit in utf32CodeUnits) { |
| 71 encoding[i++] = unit & UNICODE_BYTE_ZERO_MASK; |
| 72 encoding[i++] = (unit >> 8) & UNICODE_BYTE_ZERO_MASK; |
| 73 encoding[i++] = (unit >> 16) & UNICODE_BYTE_ZERO_MASK; |
| 74 encoding[i++] = (unit >> 24) & UNICODE_BYTE_ZERO_MASK; |
| 75 } |
| 76 return encoding; |
| 77 } |
| 78 |
| 79 /** |
| 80 * Joins groups of 4 bytes (0-255) UTF32-BE to produce single code points. |
| 81 */ |
| 82 List<int> _utf32beToCodePoints(List<int> utf32beEncodedBytes, |
| 83 [int start = 0, int length = null]) { |
| 84 if (!(start >= 0)) { |
| 85 throw new IllegalArgumentException("start"); |
| 86 } |
| 87 |
| 88 if (!(length == null || length >= 0)) { |
| 89 throw new IllegalArgumentException("length"); |
| 90 } |
| 91 |
| 92 int end = length != null ? |
| 93 Math.min(utf32beEncodedBytes.length, start + length) : |
| 94 utf32beEncodedBytes.length; |
| 95 |
| 96 int i = start; |
| 97 int lastIndex = end - 3; |
| 98 |
| 99 if ((start < lastIndex) && utf32beEncodedBytes[start] == 0 && |
| 100 utf32beEncodedBytes[start + 1] == 0 && |
| 101 utf32beEncodedBytes[start + 2] == UNICODE_UTF_BOM_HI && |
| 102 utf32beEncodedBytes[start + 3] == UNICODE_UTF_BOM_LO) { |
| 103 i += 4; |
| 104 } |
| 105 |
| 106 List<int> codepoints = new List<int>(((end - i)/4).ceil().toInt()); |
| 107 int j = 0; |
| 108 while (i < lastIndex) { |
| 109 int value = utf32beEncodedBytes[i++]; |
| 110 value = (value << 8) + utf32beEncodedBytes[i++]; |
| 111 value = (value << 8) + utf32beEncodedBytes[i++]; |
| 112 value = (value << 8) + utf32beEncodedBytes[i++]; |
| 113 codepoints[j++] = _filterValidCodepoint(value); |
| 114 } |
| 115 while (j < codepoints.length) { |
| 116 codepoints[j++] = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT; |
| 117 } |
| 118 return codepoints; |
| 119 } |
| 120 |
| 121 /** |
| 122 * Joins groups of 4 bytes (0-255) UTF32-LE to produce single code points. |
| 123 */ |
| 124 List<int> _utf32leToCodePoints(List<int> utf32leEncodedBytes, |
| 125 [int start = 0, int length = null]) { |
| 126 if (!(start >= 0)) { |
| 127 throw new IllegalArgumentException("start"); |
| 128 } |
| 129 |
| 130 if (!(length == null || length >= 0)) { |
| 131 throw new IllegalArgumentException("length"); |
| 132 } |
| 133 |
| 134 int end = length != null ? |
| 135 Math.min(utf32leEncodedBytes.length, start + length) : |
| 136 utf32leEncodedBytes.length; |
| 137 |
| 138 int i = start; |
| 139 int lastIndex = end - 3; |
| 140 |
| 141 if ((start < lastIndex) && utf32leEncodedBytes[start] == UNICODE_UTF_BOM_LO && |
| 142 utf32leEncodedBytes[start + 1] == UNICODE_UTF_BOM_HI && |
| 143 utf32leEncodedBytes[start + 2] == 0 && |
| 144 utf32leEncodedBytes[start + 3] == 0) { |
| 145 i += 4; |
| 146 } |
| 147 |
| 148 List<int> codepoints = new List<int>(((end - i)/4).ceil().toInt()); |
| 149 int j = 0; |
| 150 while (i < lastIndex) { |
| 151 int value = utf32leEncodedBytes[i+3]; |
| 152 value = (value << 8) + utf32leEncodedBytes[i+2]; |
| 153 value = (value << 8) + utf32leEncodedBytes[i+1]; |
| 154 value = (value << 8) + utf32leEncodedBytes[i]; |
| 155 i += 4; |
| 156 codepoints[j++] = _filterValidCodepoint(value); |
| 157 } |
| 158 while (j < codepoints.length) { |
| 159 codepoints[j++] = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT; |
| 160 } |
| 161 return codepoints; |
| 162 } |
| 163 |
| 164 /** |
| 165 * Joins groups of 4 bytes (0-255) UTF32 to produce single code points. |
| 166 */ |
| 167 List<int> _utf32ToCodePoints(List<int> utf32EncodedBytes, |
| 168 [int start = 0, int length = null]) { |
| 169 if (!(start >= 0)) { |
| 170 throw new IllegalArgumentException("start"); |
| 171 } |
| 172 |
| 173 if (!(length == null || length >= 0)) { |
| 174 throw new IllegalArgumentException("length"); |
| 175 } |
| 176 |
| 177 int end = length != null ? |
| 178 Math.min(utf32EncodedBytes.length, start + length) : |
| 179 utf32EncodedBytes.length; |
| 180 |
| 181 if ((start + 3 < end) && |
| 182 utf32EncodedBytes[start] == 0 && |
| 183 utf32EncodedBytes[start + 1] == 0 && |
| 184 utf32EncodedBytes[start + 2] == UNICODE_UTF_BOM_HI && |
| 185 utf32EncodedBytes[start + 3] == UNICODE_UTF_BOM_LO) { |
| 186 return _utf32beToCodePoints(utf32EncodedBytes, start + 4, |
| 187 end - (start + 4)); |
| 188 } else if ((start + 3 < end) && |
| 189 utf32EncodedBytes[start] == UNICODE_UTF_BOM_LO && |
| 190 utf32EncodedBytes[start + 1] == UNICODE_UTF_BOM_HI && |
| 191 utf32EncodedBytes[start + 2] == 0 && |
| 192 utf32EncodedBytes[start + 3] == 0) { |
| 193 return _utf32leToCodePoints(utf32EncodedBytes, start + 4, |
| 194 end - (start + 4)); |
| 195 } else { |
| 196 return _utf32beToCodePoints(utf32EncodedBytes, start, end - start); |
| 197 } |
| 198 } |
| 199 |
| 200 int _filterValidCodepoint(int codepoint) { |
| 201 if ((codepoint >= 0 && codepoint < UNICODE_UTF16_RESERVED_LO) || |
| 202 (codepoint > UNICODE_UTF16_RESERVED_HI && |
| 203 codepoint < UNICODE_VALID_RANGE_MAX)) { |
| 204 return codepoint; |
| 205 } else { |
| 206 return UNICODE_REPLACEMENT_CHARACTER_CODEPOINT; |
| 207 } |
| 208 } |
| OLD | NEW |