| 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 /* patch */ const factory String.fromEnvironment(String name, | 10 /* patch */ const factory String.fromEnvironment(String name, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 Type get runtimeType => String; | 27 Type get runtimeType => String; |
| 28 | 28 |
| 29 int get hashCode native "String_getHashCode"; | 29 int get hashCode native "String_getHashCode"; |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Create the most efficient string representation for specified | 32 * Create the most efficient string representation for specified |
| 33 * [codePoints]. | 33 * [codePoints]. |
| 34 */ | 34 */ |
| 35 static String createFromCharCodes(Iterable<int> charCodes) { | 35 static String createFromCharCodes(Iterable<int> charCodes) { |
| 36 if (charCodes != null) { | 36 if (charCodes != null) { |
| 37 // TODO(srdjan): Also skip copying of typed arrays. | 37 // TODO(srdjan): Also skip copying of wide typed arrays. |
| 38 final ccid = charCodes._cid; | 38 final ccid = charCodes._cid; |
| 39 bool isOneByteString = false; |
| 39 if ((ccid != _List._classId) && | 40 if ((ccid != _List._classId) && |
| 40 (ccid != _GrowableList._classId) && | 41 (ccid != _GrowableList._classId) && |
| 41 (ccid != _ImmutableList._classId)) { | 42 (ccid != _ImmutableList._classId)) { |
| 42 charCodes = new List<int>.from(charCodes, growable: false); | 43 if ((charCodes is Uint8List) || (charCodes is Int8List)) { |
| 43 } | 44 isOneByteString = true; |
| 44 | 45 } else { |
| 45 bool isOneByteString = true; | 46 charCodes = new List<int>.from(charCodes, growable: false); |
| 46 for (int i = 0; i < charCodes.length; i++) { | |
| 47 int e = charCodes[i]; | |
| 48 if (e is! _Smi) throw new ArgumentError(e); | |
| 49 // Is e Latin1? | |
| 50 if ((e < 0) || (e > 0xFF)) { | |
| 51 isOneByteString = false; | |
| 52 break; | |
| 53 } | 47 } |
| 54 } | 48 } |
| 55 if (isOneByteString) { | 49 final len = charCodes.length; |
| 56 var s = _OneByteString._allocate(charCodes.length); | 50 if (!isOneByteString) { |
| 57 for (int i = 0; i < charCodes.length; i++) { | 51 for (int i = 0; i < len; i++) { |
| 58 s._setAt(i, charCodes[i]); | 52 int e = charCodes[i]; |
| 53 if (e is! _Smi) throw new ArgumentError(e); |
| 54 // Is e Latin1? |
| 55 if ((e < 0) || (e > 0xFF)) { |
| 56 return _createFromCodePoints(charCodes); |
| 57 } |
| 59 } | 58 } |
| 60 return s; | |
| 61 } | 59 } |
| 60 // Allocate a one byte string. When the list is 128 entries or longer, |
| 61 // it's faster to perform a runtime-call. |
| 62 if (len >= 128) { |
| 63 return _OneByteString._allocateFromOneByteList(charCodes); |
| 64 } |
| 65 var s = _OneByteString._allocate(len); |
| 66 for (int i = 0; i < len; i++) { |
| 67 s._setAt(i, charCodes[i]); |
| 68 } |
| 69 return s; |
| 62 } | 70 } |
| 63 return _createFromCodePoints(charCodes); | 71 return _createFromCodePoints(charCodes); |
| 64 } | 72 } |
| 65 | 73 |
| 66 static String _createFromCodePoints(List<int> codePoints) | 74 static String _createFromCodePoints(List<int> codePoints) |
| 67 native "StringBase_createFromCodePoints"; | 75 native "StringBase_createFromCodePoints"; |
| 68 | 76 |
| 69 String operator [](int index) native "String_charAt"; | 77 String operator [](int index) native "String_charAt"; |
| 70 | 78 |
| 71 int codeUnitAt(int index) native "String_codeUnitAt"; | 79 int codeUnitAt(int index) native "String_codeUnitAt"; |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 return false; | 629 return false; |
| 622 } | 630 } |
| 623 } | 631 } |
| 624 return super.contains(pattern, start); | 632 return super.contains(pattern, start); |
| 625 } | 633 } |
| 626 | 634 |
| 627 // Allocates a string of given length, expecting its content to be | 635 // Allocates a string of given length, expecting its content to be |
| 628 // set using _setAt. | 636 // set using _setAt. |
| 629 static _OneByteString _allocate(int length) native "OneByteString_allocate"; | 637 static _OneByteString _allocate(int length) native "OneByteString_allocate"; |
| 630 | 638 |
| 639 |
| 640 static _OneByteString _allocateFromOneByteList(List<int> list) |
| 641 native "OneByteString_allocateFromOneByteList"; |
| 642 |
| 631 // This is internal helper method. Code point value must be a valid | 643 // This is internal helper method. Code point value must be a valid |
| 632 // Latin1 value (0..0xFF), index must be valid. | 644 // Latin1 value (0..0xFF), index must be valid. |
| 633 void _setAt(int index, int codePoint) native "OneByteString_setAt"; | 645 void _setAt(int index, int codePoint) native "OneByteString_setAt"; |
| 634 } | 646 } |
| 635 | 647 |
| 636 | 648 |
| 637 class _TwoByteString extends _StringBase implements String { | 649 class _TwoByteString extends _StringBase implements String { |
| 638 static final int _classId = "\u{FFFF}"._cid; | 650 static final int _classId = "\u{FFFF}"._cid; |
| 639 | 651 |
| 640 factory _TwoByteString._uninstantiable() { | 652 factory _TwoByteString._uninstantiable() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 class _CodeUnits extends Object with ListMixin<int>, | 735 class _CodeUnits extends Object with ListMixin<int>, |
| 724 UnmodifiableListMixin<int> { | 736 UnmodifiableListMixin<int> { |
| 725 /** The string that this is the code units of. */ | 737 /** The string that this is the code units of. */ |
| 726 String _string; | 738 String _string; |
| 727 | 739 |
| 728 _CodeUnits(this._string); | 740 _CodeUnits(this._string); |
| 729 | 741 |
| 730 int get length => _string.length; | 742 int get length => _string.length; |
| 731 int operator[](int i) => _string.codeUnitAt(i); | 743 int operator[](int i) => _string.codeUnitAt(i); |
| 732 } | 744 } |
| OLD | NEW |