| 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 const int _UTF8_ONE_BYTE_MAX = 0x7f; | 5 const int _UTF8_ONE_BYTE_MAX = 0x7f; |
| 6 const int _UTF8_TWO_BYTE_MAX = 0x7ff; | 6 const int _UTF8_TWO_BYTE_MAX = 0x7ff; |
| 7 const int _UTF8_THREE_BYTE_MAX = 0xffff; | 7 const int _UTF8_THREE_BYTE_MAX = 0xffff; |
| 8 | 8 |
| 9 const int _UTF8_LO_SIX_BIT_MASK = 0x3f; | 9 const int _UTF8_LO_SIX_BIT_MASK = 0x3f; |
| 10 | 10 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 * Return type of [decodeUtf8AsIterable] and variants. The Iterable type | 126 * Return type of [decodeUtf8AsIterable] and variants. The Iterable type |
| 127 * provides an iterator on demand and the iterator will only translate bytes | 127 * provides an iterator on demand and the iterator will only translate bytes |
| 128 * as requested by the user of the iterator. (Note: results are not cached.) | 128 * as requested by the user of the iterator. (Note: results are not cached.) |
| 129 */ | 129 */ |
| 130 class IterableUtf8Decoder implements Iterable<int> { | 130 class IterableUtf8Decoder implements Iterable<int> { |
| 131 final List<int> bytes; | 131 final List<int> bytes; |
| 132 final int offset; | 132 final int offset; |
| 133 final int length; | 133 final int length; |
| 134 final int replacementCodepoint; | 134 final int replacementCodepoint; |
| 135 | 135 |
| 136 IterableUtf8Decoder(List<int> this.bytes, [int this.offset = 0, | 136 IterableUtf8Decoder(this.bytes, [this.offset = 0, this.length = null, |
| 137 int this.length = null, | 137 this.replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]); |
| 138 int this.replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]); | |
| 139 | 138 |
| 140 Utf8Decoder iterator() => new Utf8Decoder(bytes, offset, length, | 139 Utf8Decoder iterator() => new Utf8Decoder(bytes, offset, length, |
| 141 replacementCodepoint); | 140 replacementCodepoint); |
| 142 } | 141 } |
| 143 | 142 |
| 144 /** | 143 /** |
| 145 * Provides an iterator of Unicode codepoints from UTF-8 encoded bytes. The | 144 * 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 | 145 * 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 | 146 * of the values to be decoded, and override the default Unicode replacement |
| 148 * character. Set the replacementCharacter to null to throw an | 147 * character. Set the replacementCharacter to null to throw an |
| 149 * ArgumentError rather than replace the bad value. The return value | 148 * ArgumentError 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). | 149 * from this method can be used as an Iterable (e.g. in a for-loop). |
| 151 */ | 150 */ |
| 152 class Utf8Decoder implements Iterator<int> { | 151 class Utf8Decoder implements Iterator<int> { |
| 153 final _ListRangeIterator utf8EncodedBytesIterator; | 152 final _ListRangeIterator utf8EncodedBytesIterator; |
| 154 final int replacementCodepoint; | 153 final int replacementCodepoint; |
| 155 | 154 |
| 156 Utf8Decoder(List<int> utf8EncodedBytes, [int offset = 0, int length, | 155 Utf8Decoder(List<int> utf8EncodedBytes, [int offset = 0, int length, |
| 157 int this.replacementCodepoint = | 156 this.replacementCodepoint = |
| 158 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 157 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 159 utf8EncodedBytesIterator = (new _ListRange(utf8EncodedBytes, offset, | 158 utf8EncodedBytesIterator = (new _ListRange(utf8EncodedBytes, offset, |
| 160 length)).iterator(); | 159 length)).iterator(); |
| 161 | 160 |
| 162 | 161 |
| 163 Utf8Decoder._fromListRangeIterator(_ListRange source, [ | 162 Utf8Decoder._fromListRangeIterator(_ListRange source, [ |
| 164 int this.replacementCodepoint = | 163 this.replacementCodepoint = |
| 165 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : | 164 UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) : |
| 166 utf8EncodedBytesIterator = source.iterator(); | 165 utf8EncodedBytesIterator = source.iterator(); |
| 167 | 166 |
| 168 /** Decode the remaininder of the characters in this decoder | 167 /** Decode the remaininder of the characters in this decoder |
| 169 * into a [List<int>]. | 168 * into a [List<int>]. |
| 170 */ | 169 */ |
| 171 List<int> decodeRest() { | 170 List<int> decodeRest() { |
| 172 List<int> codepoints = new List<int>(utf8EncodedBytesIterator.remaining); | 171 List<int> codepoints = new List<int>(utf8EncodedBytesIterator.remaining); |
| 173 int i = 0; | 172 int i = 0; |
| 174 while (hasNext()) { | 173 while (hasNext()) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 if (validSequence && nonOverlong && inRange) { | 251 if (validSequence && nonOverlong && inRange) { |
| 253 return value; | 252 return value; |
| 254 } else if (replacementCodepoint != null) { | 253 } else if (replacementCodepoint != null) { |
| 255 return replacementCodepoint; | 254 return replacementCodepoint; |
| 256 } else { | 255 } else { |
| 257 throw new ArgumentError( | 256 throw new ArgumentError( |
| 258 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); | 257 "Invalid UTF8 at ${utf8EncodedBytesIterator.position - j}"); |
| 259 } | 258 } |
| 260 } | 259 } |
| 261 } | 260 } |
| OLD | NEW |