| 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 return _StringBase.createFromCharCodes(charCodes); | 7 return _StringBase.createFromCharCodes(charCodes); |
| 8 } | 8 } |
| 9 } | 9 } |
| 10 | 10 |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * [_StringBase] contains common methods used by concrete String | 13 * [_StringBase] contains common methods used by concrete String |
| 14 * implementations, e.g., _OneByteString. | 14 * implementations, e.g., _OneByteString. |
| 15 */ | 15 */ |
| 16 class _StringBase { | 16 class _StringBase { |
| 17 | 17 |
| 18 factory _StringBase._uninstantiable() { | 18 factory _StringBase._uninstantiable() { |
| 19 throw new UnsupportedError( | 19 throw new UnsupportedError( |
| 20 "_StringBase can't be instaniated"); | 20 "_StringBase can't be instaniated"); |
| 21 } | 21 } |
| 22 | 22 |
| 23 int get hashCode native "String_getHashCode"; | 23 int get hashCode native "String_getHashCode"; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Create the most efficient string representation for specified | 26 * Create the most efficient string representation for specified |
| 27 * [codePoints]. | 27 * [codePoints]. |
| 28 */ | 28 */ |
| 29 static String createFromCharCodes(Iterable<int> charCodes) { | 29 static String createFromCharCodes(Iterable<int> charCodes) { |
| 30 // TODO(srdjan): Also skip copying of typed arrays. | 30 if (charCodes != null) { |
| 31 if (charCodes is! _ObjectArray && | 31 // TODO(srdjan): Also skip copying of typed arrays. |
| 32 charCodes is! _GrowableObjectArray && | 32 if (charCodes is! _ObjectArray && |
| 33 charCodes is! _ImmutableArray) { | 33 charCodes is! _GrowableObjectArray && |
| 34 charCodes = new List<int>.from(charCodes, growable: false); | 34 charCodes is! _ImmutableArray) { |
| 35 charCodes = new List<int>.from(charCodes, growable: false); |
| 36 } |
| 37 |
| 38 bool isOneByteString = true; |
| 39 for (int i = 0; i < charCodes.length; i++) { |
| 40 int e = charCodes[i]; |
| 41 if (e is! int) throw new ArgumentError(e); |
| 42 // Is e Latin1? |
| 43 if ((e < 0) || (e > 0xFF)) { |
| 44 isOneByteString = false; |
| 45 break; |
| 46 } |
| 47 } |
| 48 if (isOneByteString) { |
| 49 var s = _OneByteString._allocate(charCodes.length); |
| 50 for (int i = 0; i < charCodes.length; i++) { |
| 51 s._setAt(i, charCodes[i]); |
| 52 } |
| 53 return s; |
| 54 } |
| 35 } | 55 } |
| 36 | |
| 37 return _createFromCodePoints(charCodes); | 56 return _createFromCodePoints(charCodes); |
| 38 } | 57 } |
| 39 | 58 |
| 40 static String _createFromCodePoints(List<int> codePoints) | 59 static String _createFromCodePoints(List<int> codePoints) |
| 41 native "StringBase_createFromCodePoints"; | 60 native "StringBase_createFromCodePoints"; |
| 42 | 61 |
| 43 String operator [](int index) native "String_charAt"; | 62 String operator [](int index) native "String_charAt"; |
| 44 | 63 |
| 45 int codeUnitAt(int index) native "String_codeUnitAt"; | 64 int codeUnitAt(int index) native "String_codeUnitAt"; |
| 46 | 65 |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 | 478 |
| 460 List<String> _splitWithCharCode(int charCode) | 479 List<String> _splitWithCharCode(int charCode) |
| 461 native "OneByteString_splitWithCharCode"; | 480 native "OneByteString_splitWithCharCode"; |
| 462 | 481 |
| 463 List<String> split(Pattern pattern) { | 482 List<String> split(Pattern pattern) { |
| 464 if ((pattern is _OneByteString) && (pattern.length == 1)) { | 483 if ((pattern is _OneByteString) && (pattern.length == 1)) { |
| 465 return _splitWithCharCode(pattern.codeUnitAt(0)); | 484 return _splitWithCharCode(pattern.codeUnitAt(0)); |
| 466 } | 485 } |
| 467 return super.split(pattern); | 486 return super.split(pattern); |
| 468 } | 487 } |
| 488 |
| 489 // Allocates a string of given length, expecting its content to be |
| 490 // set using _setAt. |
| 491 static _OneByteString _allocate(int length) native "OneByteString_allocate"; |
| 492 |
| 493 // Code point value must be a valid Latin1 (0..0xFF). Index must be valid. |
| 494 void _setAt(int index, int codePoint) native "OneByteString_setAt"; |
| 469 } | 495 } |
| 470 | 496 |
| 471 | 497 |
| 472 class _TwoByteString extends _StringBase implements String { | 498 class _TwoByteString extends _StringBase implements String { |
| 473 factory _TwoByteString._uninstantiable() { | 499 factory _TwoByteString._uninstantiable() { |
| 474 throw new UnsupportedError( | 500 throw new UnsupportedError( |
| 475 "_TwoByteString can only be allocated by the VM"); | 501 "_TwoByteString can only be allocated by the VM"); |
| 476 } | 502 } |
| 477 | 503 |
| 478 // Checks for one-byte whitespaces only. | 504 // Checks for one-byte whitespaces only. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 class _CodeUnits extends Object with ListMixin<int>, | 615 class _CodeUnits extends Object with ListMixin<int>, |
| 590 UnmodifiableListMixin<int> { | 616 UnmodifiableListMixin<int> { |
| 591 /** The string that this is the code units of. */ | 617 /** The string that this is the code units of. */ |
| 592 String _string; | 618 String _string; |
| 593 | 619 |
| 594 _CodeUnits(this._string); | 620 _CodeUnits(this._string); |
| 595 | 621 |
| 596 int get length => _string.length; | 622 int get length => _string.length; |
| 597 int operator[](int i) => _string.codeUnitAt(i); | 623 int operator[](int i) => _string.codeUnitAt(i); |
| 598 } | 624 } |
| OLD | NEW |