| 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 part of dart.utf; | 5 part of dart.utf; |
| 6 | 6 |
| 7 // TODO(jmesserly): would be nice to have this on String (dartbug.com/6501). | 7 // TODO(jmesserly): would be nice to have this on String (dartbug.com/6501). |
| 8 /** | 8 /** |
| 9 * Provide a list of Unicode codepoints for a given string. | 9 * Provide a list of Unicode codepoints for a given string. |
| 10 */ | 10 */ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 57 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
| 58 encodedLength++; | 58 encodedLength++; |
| 59 } else if (value > UNICODE_PLANE_ONE_MAX && | 59 } else if (value > UNICODE_PLANE_ONE_MAX && |
| 60 value <= UNICODE_VALID_RANGE_MAX) { | 60 value <= UNICODE_VALID_RANGE_MAX) { |
| 61 encodedLength += 2; | 61 encodedLength += 2; |
| 62 } else { | 62 } else { |
| 63 encodedLength++; | 63 encodedLength++; |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 List<int> codeUnitsBuffer = new List<int>.fixedLength(encodedLength); | 67 List<int> codeUnitsBuffer = new List<int>(encodedLength); |
| 68 int j = 0; | 68 int j = 0; |
| 69 for (int value in listRange) { | 69 for (int value in listRange) { |
| 70 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || | 70 if ((value >= 0 && value < UNICODE_UTF16_RESERVED_LO) || |
| 71 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { | 71 (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) { |
| 72 codeUnitsBuffer[j++] = value; | 72 codeUnitsBuffer[j++] = value; |
| 73 } else if (value > UNICODE_PLANE_ONE_MAX && | 73 } else if (value > UNICODE_PLANE_ONE_MAX && |
| 74 value <= UNICODE_VALID_RANGE_MAX) { | 74 value <= UNICODE_VALID_RANGE_MAX) { |
| 75 int base = value - UNICODE_UTF16_OFFSET; | 75 int base = value - UNICODE_UTF16_OFFSET; |
| 76 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_0_BASE + | 76 codeUnitsBuffer[j++] = UNICODE_UTF16_SURROGATE_UNIT_0_BASE + |
| 77 ((base & UNICODE_UTF16_HI_MASK) >> 10); | 77 ((base & UNICODE_UTF16_HI_MASK) >> 10); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 /** | 89 /** |
| 90 * Decodes the utf16 codeunits to codepoints. | 90 * Decodes the utf16 codeunits to codepoints. |
| 91 */ | 91 */ |
| 92 List<int> _utf16CodeUnitsToCodepoints( | 92 List<int> _utf16CodeUnitsToCodepoints( |
| 93 List<int> utf16CodeUnits, [int offset = 0, int length, | 93 List<int> utf16CodeUnits, [int offset = 0, int length, |
| 94 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { | 94 int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) { |
| 95 _ListRangeIterator source = | 95 _ListRangeIterator source = |
| 96 (new _ListRange(utf16CodeUnits, offset, length)).iterator; | 96 (new _ListRange(utf16CodeUnits, offset, length)).iterator; |
| 97 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder | 97 Utf16CodeUnitDecoder decoder = new Utf16CodeUnitDecoder |
| 98 .fromListRangeIterator(source, replacementCodepoint); | 98 .fromListRangeIterator(source, replacementCodepoint); |
| 99 List<int> codepoints = new List<int>.fixedLength(source.remaining); | 99 List<int> codepoints = new List<int>(source.remaining); |
| 100 int i = 0; | 100 int i = 0; |
| 101 while (decoder.moveNext()) { | 101 while (decoder.moveNext()) { |
| 102 codepoints[i++] = decoder.current; | 102 codepoints[i++] = decoder.current; |
| 103 } | 103 } |
| 104 if (i == codepoints.length) { | 104 if (i == codepoints.length) { |
| 105 return codepoints; | 105 return codepoints; |
| 106 } else { | 106 } else { |
| 107 List<int> codepointTrunc = new List<int>.fixedLength(i); | 107 List<int> codepointTrunc = new List<int>(i); |
| 108 codepointTrunc.setRange(0, i, codepoints); | 108 codepointTrunc.setRange(0, i, codepoints); |
| 109 return codepointTrunc; | 109 return codepointTrunc; |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 /** | 113 /** |
| 114 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. | 114 * An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. |
| 115 * The parameters can override the default Unicode replacement character. Set | 115 * The parameters can override the default Unicode replacement character. Set |
| 116 * the replacementCharacter to null to throw an ArgumentError | 116 * the replacementCharacter to null to throw an ArgumentError |
| 117 * rather than replace the bad value. | 117 * rather than replace the bad value. |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 _offset -= by; | 248 _offset -= by; |
| 249 } | 249 } |
| 250 | 250 |
| 251 int get remaining => _end - _offset - 1; | 251 int get remaining => _end - _offset - 1; |
| 252 | 252 |
| 253 void skip([int count = 1]) { | 253 void skip([int count = 1]) { |
| 254 _offset += count; | 254 _offset += count; |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 | 257 |
| OLD | NEW |