| OLD | NEW |
| 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 final int _UTF8_ONE_BYTE_MAX = 0x7f; | 5 final int _UTF8_ONE_BYTE_MAX = 0x7f; |
| 6 final int _UTF8_TWO_BYTE_MAX = 0x7ff; | 6 final int _UTF8_TWO_BYTE_MAX = 0x7ff; |
| 7 final int _UTF8_THREE_BYTE_MAX = 0xffff; | 7 final int _UTF8_THREE_BYTE_MAX = 0xffff; |
| 8 | 8 |
| 9 final int _UTF8_LO_SIX_BIT_MASK = 0x3f; | 9 final int _UTF8_LO_SIX_BIT_MASK = 0x3f; |
| 10 | 10 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 bytes--; | 60 bytes--; |
| 61 } | 61 } |
| 62 return value; | 62 return value; |
| 63 } | 63 } |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * Encode code points as UTF-8 code units. | 66 * Encode code points as UTF-8 code units. |
| 67 */ | 67 */ |
| 68 List<int> codepointsToUtf8( | 68 List<int> codepointsToUtf8( |
| 69 List<int> codepoints, [int offset = 0, int length]) { | 69 List<int> codepoints, [int offset = 0, int length]) { |
| 70 _ListRange<int> source = new _ListRange(codepoints, offset, length); | 70 _ListRange source = new _ListRange(codepoints, offset, length); |
| 71 | 71 |
| 72 int encodedLength = 0; | 72 int encodedLength = 0; |
| 73 for (int value in source) { | 73 for (int value in source) { |
| 74 if (value < 0 || value > UNICODE_VALID_RANGE_MAX) { | 74 if (value < 0 || value > UNICODE_VALID_RANGE_MAX) { |
| 75 encodedLength += 3; | 75 encodedLength += 3; |
| 76 } else if (value <= _UTF8_ONE_BYTE_MAX) { | 76 } else if (value <= _UTF8_ONE_BYTE_MAX) { |
| 77 encodedLength++; | 77 encodedLength++; |
| 78 } else if (value <= _UTF8_TWO_BYTE_MAX) { | 78 } else if (value <= _UTF8_TWO_BYTE_MAX) { |
| 79 encodedLength += 2; | 79 encodedLength += 2; |
| 80 } else if (value <= _UTF8_THREE_BYTE_MAX) { | 80 } else if (value <= _UTF8_THREE_BYTE_MAX) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 /** | 144 /** |
| 145 * Provides an iterator of Unicode codepoints from UTF-8 encoded bytes. The | 145 * Provides an iterator of Unicode codepoints from UTF-8 encoded bytes. The |
| 146 * parameters can set an offset into a list of bytes (as int), limit the length | 146 * parameters can set an offset into a list of bytes (as int), limit the length |
| 147 * of the values to be decoded, and override the default Unicode replacement | 147 * of the values to be decoded, and override the default Unicode replacement |
| 148 * character. Set the replacementCharacter to null to throw an | 148 * character. Set the replacementCharacter to null to throw an |
| 149 * IllegalArgumentException rather than replace the bad value. The return value | 149 * IllegalArgumentException rather than replace the bad value. The return value |
| 150 * from this method can be used as an Iterable (e.g. in a for-loop). | 150 * from this method can be used as an Iterable (e.g. in a for-loop). |
| 151 */ | 151 */ |
| 152 class Utf8Decoder implements Iterator<int> { | 152 class Utf8Decoder implements Iterator<int> { |
| 153 final _ListRangeIterator<int> utf8EncodedBytesIterator; | 153 final _ListRangeIterator utf8EncodedBytesIterator; |
| 154 final int replacementCodepoint; | 154 final int replacementCodepoint; |
| 155 | 155 |
| 156 Utf8Decoder(List<int> utf8EncodedBytes, [int offset = 0, int length, | 156 Utf8Decoder(List<int> utf8EncodedBytes, [int offset = 0, int length, |
| 157 int this.replacementCodepoint = | 157 int this.replacementCodepoint = |
| 158 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 158 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 159 utf8EncodedBytesIterator = (new _ListRange(utf8EncodedBytes, offset, | 159 utf8EncodedBytesIterator = (new _ListRange(utf8EncodedBytes, offset, |
| 160 length)).iterator(); | 160 length)).iterator(); |
| 161 | 161 |
| 162 | 162 |
| 163 Utf8Decoder._fromListRangeIterator(_ListRange<int> source, [ | 163 Utf8Decoder._fromListRangeIterator(_ListRange source, [ |
| 164 int this.replacementCodepoint = | 164 int this.replacementCodepoint = |
| 165 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 165 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 166 utf8EncodedBytesIterator = source.iterator(); | 166 utf8EncodedBytesIterator = source.iterator(); |
| 167 | 167 |
| 168 /** Decode the remaininder of the characters in this decoder | 168 /** Decode the remaininder of the characters in this decoder |
| 169 * into a [List<int>]. | 169 * into a [List<int>]. |
| 170 */ | 170 */ |
| 171 List<int> decodeRest() { | 171 List<int> decodeRest() { |
| 172 List<int> codepoints = new List<int>(utf8EncodedBytesIterator.remaining); | 172 List<int> codepoints = new List<int>(utf8EncodedBytesIterator.remaining); |
| 173 int i = 0; | 173 int i = 0; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 if (validSequence && nonOverlong && inRange) { | 252 if (validSequence && nonOverlong && inRange) { |
| 253 return value; | 253 return value; |
| 254 } else if (replacementCodepoint != null) { | 254 } else if (replacementCodepoint != null) { |
| 255 return replacementCodepoint; | 255 return replacementCodepoint; |
| 256 } else { | 256 } else { |
| 257 throw new IllegalArgumentException( | 257 throw new IllegalArgumentException( |
| 258 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); | 258 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 } | 261 } |
| OLD | NEW |