Chromium Code Reviews| 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) { |
|
srdjan
2013/11/20 16:06:57
Add parentheses around is tests.
Anders Johnsen
2013/11/21 06:06:27
Done.
| |
| 44 isOneByteString = true; | |
| 45 } else { | |
| 46 charCodes = new List<int>.from(charCodes, growable: false); | |
| 47 } | |
| 43 } | 48 } |
| 44 | 49 |
| 45 bool isOneByteString = true; | 50 if (!isOneByteString) { |
| 46 for (int i = 0; i < charCodes.length; i++) { | 51 for (int i = 0; i < charCodes.length; i++) { |
|
srdjan
2013/11/20 16:06:57
You may want to prefetch chatCodes.length and keep
Anders Johnsen
2013/11/21 06:06:27
Done.
| |
| 47 int e = charCodes[i]; | 52 int e = charCodes[i]; |
| 48 if (e is! _Smi) throw new ArgumentError(e); | 53 if (e is! _Smi) throw new ArgumentError(e); |
| 49 // Is e Latin1? | 54 // Is e Latin1? |
| 50 if ((e < 0) || (e > 0xFF)) { | 55 if ((e < 0) || (e > 0xFF)) { |
| 51 isOneByteString = false; | 56 return _createFromCodePoints(charCodes); |
| 52 break; | 57 } |
| 53 } | 58 } |
| 54 } | 59 } |
| 55 if (isOneByteString) { | 60 |
| 56 var s = _OneByteString._allocate(charCodes.length); | 61 // Allocate a one byte string. |
|
srdjan
2013/11/20 16:06:57
Add a comment that above the threshold, the native
Anders Johnsen
2013/11/21 06:06:27
Just did a test with normal lists. 128 is indeed t
| |
| 57 for (int i = 0; i < charCodes.length; i++) { | 62 if (charCodes.length >= 128) { |
| 58 s._setAt(i, charCodes[i]); | 63 return _OneByteString._allocateFromOneByteList(charCodes); |
| 59 } | |
| 60 return s; | |
| 61 } | 64 } |
| 65 | |
| 66 var s = _OneByteString._allocate(charCodes.length); | |
| 67 for (int i = 0; i < charCodes.length; i++) { | |
| 68 s._setAt(i, charCodes[i]); | |
| 69 } | |
| 70 return s; | |
| 62 } | 71 } |
| 63 return _createFromCodePoints(charCodes); | 72 return _createFromCodePoints(charCodes); |
| 64 } | 73 } |
| 65 | 74 |
| 66 static String _createFromCodePoints(List<int> codePoints) | 75 static String _createFromCodePoints(List<int> codePoints) |
| 67 native "StringBase_createFromCodePoints"; | 76 native "StringBase_createFromCodePoints"; |
| 68 | 77 |
| 69 String operator [](int index) native "String_charAt"; | 78 String operator [](int index) native "String_charAt"; |
| 70 | 79 |
| 71 int codeUnitAt(int index) native "String_codeUnitAt"; | 80 int codeUnitAt(int index) native "String_codeUnitAt"; |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 return false; | 630 return false; |
| 622 } | 631 } |
| 623 } | 632 } |
| 624 return super.contains(pattern, start); | 633 return super.contains(pattern, start); |
| 625 } | 634 } |
| 626 | 635 |
| 627 // Allocates a string of given length, expecting its content to be | 636 // Allocates a string of given length, expecting its content to be |
| 628 // set using _setAt. | 637 // set using _setAt. |
| 629 static _OneByteString _allocate(int length) native "OneByteString_allocate"; | 638 static _OneByteString _allocate(int length) native "OneByteString_allocate"; |
| 630 | 639 |
| 640 | |
| 641 static _OneByteString _allocateFromOneByteList(List<int> list) | |
| 642 native "OneByteString_allocateFromOneByteList"; | |
| 643 | |
| 631 // This is internal helper method. Code point value must be a valid | 644 // This is internal helper method. Code point value must be a valid |
| 632 // Latin1 value (0..0xFF), index must be valid. | 645 // Latin1 value (0..0xFF), index must be valid. |
| 633 void _setAt(int index, int codePoint) native "OneByteString_setAt"; | 646 void _setAt(int index, int codePoint) native "OneByteString_setAt"; |
| 634 } | 647 } |
| 635 | 648 |
| 636 | 649 |
| 637 class _TwoByteString extends _StringBase implements String { | 650 class _TwoByteString extends _StringBase implements String { |
| 638 static final int _classId = "\u{FFFF}"._cid; | 651 static final int _classId = "\u{FFFF}"._cid; |
| 639 | 652 |
| 640 factory _TwoByteString._uninstantiable() { | 653 factory _TwoByteString._uninstantiable() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 class _CodeUnits extends Object with ListMixin<int>, | 736 class _CodeUnits extends Object with ListMixin<int>, |
| 724 UnmodifiableListMixin<int> { | 737 UnmodifiableListMixin<int> { |
| 725 /** The string that this is the code units of. */ | 738 /** The string that this is the code units of. */ |
| 726 String _string; | 739 String _string; |
| 727 | 740 |
| 728 _CodeUnits(this._string); | 741 _CodeUnits(this._string); |
| 729 | 742 |
| 730 int get length => _string.length; | 743 int get length => _string.length; |
| 731 int operator[](int i) => _string.codeUnitAt(i); | 744 int operator[](int i) => _string.codeUnitAt(i); |
| 732 } | 745 } |
| OLD | NEW |