| 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 patch class String { | 5 patch class String { |
| 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes, | 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes, |
| 7 [int start = 0, int end]) { | 7 [int start = 0, int end]) { |
| 8 return _StringBase.createFromCharCodes(charCodes, start, end); | 8 return _StringBase.createFromCharCodes(charCodes, start, end); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 50 |
| 51 int get hashCode native "String_getHashCode"; | 51 int get hashCode native "String_getHashCode"; |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Create the most efficient string representation for specified | 54 * Create the most efficient string representation for specified |
| 55 * [codePoints]. | 55 * [codePoints]. |
| 56 */ | 56 */ |
| 57 static String createFromCharCodes(Iterable<int> charCodes, | 57 static String createFromCharCodes(Iterable<int> charCodes, |
| 58 int start, int end) { | 58 int start, int end) { |
| 59 if (charCodes == null) throw new ArgumentError(charCodes); | 59 if (charCodes == null) throw new ArgumentError(charCodes); |
| 60 if (start < 0) throw new RangeError.value(start); | |
| 61 // TODO(srdjan): Also skip copying of wide typed arrays. | 60 // TODO(srdjan): Also skip copying of wide typed arrays. |
| 62 final ccid = ClassID.getID(charCodes); | 61 final ccid = ClassID.getID(charCodes); |
| 63 bool isOneByteString = false; | 62 bool isOneByteString = false; |
| 64 if ((ccid != ClassID.cidArray) && | 63 if ((ccid != ClassID.cidArray) && |
| 65 (ccid != ClassID.cidGrowableObjectArray) && | 64 (ccid != ClassID.cidGrowableObjectArray) && |
| 66 (ccid != ClassID.cidImmutableArray)) { | 65 (ccid != ClassID.cidImmutableArray)) { |
| 67 if ((charCodes is Uint8List) || (charCodes is Int8List)) { | 66 if (charCodes is Uint8List) { |
| 68 isOneByteString = true; | 67 isOneByteString = true; |
| 69 } else { | 68 } else { |
| 70 charCodes = new List.from(charCodes, growable: false); | 69 // Treat charCodes as Iterable. |
| 70 if (start < 0) throw new RangeError.range(start, 0, charCodes.length); |
| 71 if (end != null && end < start) { |
| 72 throw new RangeError.value(end, start, charCodes.length); |
| 73 } |
| 74 var it = charCodes.iterator; |
| 75 for (int i = 0; i < start; i++) { |
| 76 if (!it.moveNext()) { |
| 77 throw new RangeError.range(start, 0, i); |
| 78 } |
| 79 } |
| 80 int bits = 0; // Bitwise or of all char codes in list. |
| 81 var list = []; |
| 82 if (end == null) { |
| 83 while (it.moveNext()) { |
| 84 int code = it.current; |
| 85 bits |= code; |
| 86 list.add(code); |
| 87 } |
| 88 } else { |
| 89 for (int i = start; i < end; i++) { |
| 90 if (!it.moveNext()) { |
| 91 throw new RangeError.range(end, start, i); |
| 92 } |
| 93 int code = it.current; |
| 94 bits |= code; |
| 95 list.add(code); |
| 96 } |
| 97 } |
| 98 charCodes = list; |
| 99 isOneByteString = (bits >= 0 && bits <= 0xff); |
| 100 start = 0; |
| 101 end = list.length; |
| 71 } | 102 } |
| 72 } | 103 } |
| 73 if ((end == null) || (end > charCodes.length)) { | 104 int codeCount = charCodes.length; |
| 74 end = charCodes.length; | 105 if (start < 0 || start > codeCount) { |
| 106 throw new RangeError.range(start, 0, codeCount); |
| 75 } | 107 } |
| 76 if (end <= start) return ""; | 108 if (end == null) { |
| 109 end = codeCount; |
| 110 } else if (end < start || end > codeCount) { |
| 111 throw new RangeError.range(end, start, codeCount); |
| 112 } |
| 77 final len = end - start; | 113 final len = end - start; |
| 78 if (!isOneByteString) { | 114 if (!isOneByteString) { |
| 79 for (int i = start; i < end; i++) { | 115 for (int i = start; i < end; i++) { |
| 80 int e = charCodes[i]; | 116 int e = charCodes[i]; |
| 81 if (e is! _Smi) throw new ArgumentError(e); | 117 if (e is! _Smi) throw new ArgumentError(e); |
| 82 // Is e Latin1? | 118 // Is e Latin1? |
| 83 if ((e < 0) || (e > 0xFF)) { | 119 if ((e < 0) || (e > 0xFF)) { |
| 84 return _createFromCodePoints(charCodes, start, end); | 120 return _createFromCodePoints(charCodes, start, end); |
| 85 } | 121 } |
| 86 } | 122 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 } | 298 } |
| 263 return _substringUncheckedNative(startIndex, endIndex); | 299 return _substringUncheckedNative(startIndex, endIndex); |
| 264 } | 300 } |
| 265 | 301 |
| 266 String _substringUncheckedNative(int startIndex, int endIndex) | 302 String _substringUncheckedNative(int startIndex, int endIndex) |
| 267 native "StringBase_substringUnchecked"; | 303 native "StringBase_substringUnchecked"; |
| 268 | 304 |
| 269 // Checks for one-byte whitespaces only. | 305 // Checks for one-byte whitespaces only. |
| 270 static bool _isOneByteWhitespace(int codeUnit) { | 306 static bool _isOneByteWhitespace(int codeUnit) { |
| 271 if (codeUnit <= 32) { | 307 if (codeUnit <= 32) { |
| 272 return ((codeUnit == 32) || // Space. | 308 return ((codeUnit == 32) || // Space. |
| 273 ((codeUnit <= 13) && (codeUnit >= 9))); // CR, LF, TAB, etc. | 309 ((codeUnit <= 13) && (codeUnit >= 9))); // CR, LF, TAB, etc. |
| 274 } | 310 } |
| 275 return (codeUnit == 0x85) || (codeUnit == 0xA0); | 311 return (codeUnit == 0x85) || (codeUnit == 0xA0); // NEL, NBSP. |
| 276 } | 312 } |
| 277 | 313 |
| 278 // Characters with Whitespace property (Unicode 6.2). | 314 // Characters with Whitespace property (Unicode 6.2). |
| 279 // 0009..000D ; White_Space # Cc <control-0009>..<control-000D> | 315 // 0009..000D ; White_Space # Cc <control-0009>..<control-000D> |
| 280 // 0020 ; White_Space # Zs SPACE | 316 // 0020 ; White_Space # Zs SPACE |
| 281 // 0085 ; White_Space # Cc <control-0085> | 317 // 0085 ; White_Space # Cc <control-0085> |
| 282 // 00A0 ; White_Space # Zs NO-BREAK SPACE | 318 // 00A0 ; White_Space # Zs NO-BREAK SPACE |
| 283 // 1680 ; White_Space # Zs OGHAM SPACE MARK | 319 // 1680 ; White_Space # Zs OGHAM SPACE MARK |
| 284 // 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR | 320 // 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR |
| 285 // 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE | 321 // 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE |
| (...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 class _CodeUnits extends Object with ListMixin<int>, | 1067 class _CodeUnits extends Object with ListMixin<int>, |
| 1032 UnmodifiableListMixin<int> { | 1068 UnmodifiableListMixin<int> { |
| 1033 /** The string that this is the code units of. */ | 1069 /** The string that this is the code units of. */ |
| 1034 String _string; | 1070 String _string; |
| 1035 | 1071 |
| 1036 _CodeUnits(this._string); | 1072 _CodeUnits(this._string); |
| 1037 | 1073 |
| 1038 int get length => _string.length; | 1074 int get length => _string.length; |
| 1039 int operator[](int i) => _string.codeUnitAt(i); | 1075 int operator[](int i) => _string.codeUnitAt(i); |
| 1040 } | 1076 } |
| OLD | NEW |