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 */ factory String.fromCharCode(int charCode) { | 10 /* patch */ factory String.fromCharCode(int charCode) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 int get hashCode native "String_getHashCode"; | 48 int get hashCode native "String_getHashCode"; |
49 | 49 |
50 /** | 50 /** |
51 * Create the most efficient string representation for specified | 51 * Create the most efficient string representation for specified |
52 * [codePoints]. | 52 * [codePoints]. |
53 */ | 53 */ |
54 static String createFromCharCodes(Iterable<int> charCodes) { | 54 static String createFromCharCodes(Iterable<int> charCodes) { |
55 if (charCodes != null) { | 55 if (charCodes != null) { |
56 // TODO(srdjan): Also skip copying of wide typed arrays. | 56 // TODO(srdjan): Also skip copying of wide typed arrays. |
57 final ccid = charCodes._cid; | 57 final ccid = ClassID.getID(charCodes); |
58 bool isOneByteString = false; | 58 bool isOneByteString = false; |
59 if ((ccid != _List._classId) && | 59 if ((ccid != _List._classId) && |
60 (ccid != _GrowableList._classId) && | 60 (ccid != _GrowableList._classId) && |
61 (ccid != _ImmutableList._classId)) { | 61 (ccid != _ImmutableList._classId)) { |
62 if ((charCodes is Uint8List) || (charCodes is Int8List)) { | 62 if ((charCodes is Uint8List) || (charCodes is Int8List)) { |
63 isOneByteString = true; | 63 isOneByteString = true; |
64 } else { | 64 } else { |
65 charCodes = new List<int>.from(charCodes, growable: false); | 65 charCodes = new List<int>.from(charCodes, growable: false); |
66 } | 66 } |
67 } | 67 } |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 * Convert all objects in [values] to strings and concat them | 503 * Convert all objects in [values] to strings and concat them |
504 * into a result string. | 504 * into a result string. |
505 */ | 505 */ |
506 static String _interpolate(List<String> values) { | 506 static String _interpolate(List<String> values) { |
507 final numValues = values.length; | 507 final numValues = values.length; |
508 _List stringList = new List<String>(numValues); | 508 _List stringList = new List<String>(numValues); |
509 bool isOneByteString = true; | 509 bool isOneByteString = true; |
510 int totalLength = 0; | 510 int totalLength = 0; |
511 for (int i = 0; i < numValues; i++) { | 511 for (int i = 0; i < numValues; i++) { |
512 var s = values[i].toString(); | 512 var s = values[i].toString(); |
513 if (isOneByteString && (s._cid == _OneByteString._classId)) { | 513 if (isOneByteString && (ClassID.getID(s) == _OneByteString._classId)) { |
514 totalLength += s.length; | 514 totalLength += s.length; |
515 } else { | 515 } else { |
516 isOneByteString = false; | 516 isOneByteString = false; |
517 if (s is! String) { | 517 if (s is! String) { |
518 throw new ArgumentError(s); | 518 throw new ArgumentError(s); |
519 } | 519 } |
520 } | 520 } |
521 stringList[i] = s; | 521 stringList[i] = s; |
522 } | 522 } |
523 if (isOneByteString) { | 523 if (isOneByteString) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 } | 618 } |
619 | 619 |
620 // Call this method if not all list elements are known to be OneByteString(s). | 620 // Call this method if not all list elements are known to be OneByteString(s). |
621 // 'strings' must be an _List or _GrowableList. | 621 // 'strings' must be an _List or _GrowableList. |
622 static String _concatRangeNative(List<String> strings, int start, int end) | 622 static String _concatRangeNative(List<String> strings, int start, int end) |
623 native "String_concatRange"; | 623 native "String_concatRange"; |
624 } | 624 } |
625 | 625 |
626 | 626 |
627 class _OneByteString extends _StringBase implements String { | 627 class _OneByteString extends _StringBase implements String { |
628 static final int _classId = "A"._cid; | 628 static final int _classId = ClassID.getID("A"); |
629 | 629 |
630 factory _OneByteString._uninstantiable() { | 630 factory _OneByteString._uninstantiable() { |
631 throw new UnsupportedError( | 631 throw new UnsupportedError( |
632 "_OneByteString can only be allocated by the VM"); | 632 "_OneByteString can only be allocated by the VM"); |
633 } | 633 } |
634 | 634 |
635 int get hashCode native "String_getHashCode"; | 635 int get hashCode native "String_getHashCode"; |
636 | 636 |
637 bool _isWhitespace(int codePoint) { | 637 bool _isWhitespace(int codePoint) { |
638 return _StringBase._isOneByteWhitespace(codePoint); | 638 return _StringBase._isOneByteWhitespace(codePoint); |
639 } | 639 } |
640 | 640 |
641 bool operator ==(Object other) { | 641 bool operator ==(Object other) { |
642 return super == other; | 642 return super == other; |
643 } | 643 } |
644 | 644 |
645 String _substringUncheckedNative(int startIndex, int endIndex) | 645 String _substringUncheckedNative(int startIndex, int endIndex) |
646 native "OneByteString_substringUnchecked"; | 646 native "OneByteString_substringUnchecked"; |
647 | 647 |
648 List<String> _splitWithCharCode(int charCode) | 648 List<String> _splitWithCharCode(int charCode) |
649 native "OneByteString_splitWithCharCode"; | 649 native "OneByteString_splitWithCharCode"; |
650 | 650 |
651 List<String> split(Pattern pattern) { | 651 List<String> split(Pattern pattern) { |
652 if ((pattern._cid == _OneByteString._classId) && (pattern.length == 1)) { | 652 if ((ClassID.getID(pattern) == _OneByteString._classId) && |
| 653 (pattern.length == 1)) { |
653 return _splitWithCharCode(pattern.codeUnitAt(0)); | 654 return _splitWithCharCode(pattern.codeUnitAt(0)); |
654 } | 655 } |
655 return super.split(pattern); | 656 return super.split(pattern); |
656 } | 657 } |
657 | 658 |
658 // All element of 'strings' must be OneByteStrings. | 659 // All element of 'strings' must be OneByteStrings. |
659 static _concatAll(List<String> strings, int totalLength) { | 660 static _concatAll(List<String> strings, int totalLength) { |
660 // TODO(srdjan): Improve code below and raise or eliminate the limit. | 661 // TODO(srdjan): Improve code below and raise or eliminate the limit. |
661 if (totalLength > 128) { | 662 if (totalLength > 128) { |
662 // Native is quicker. | 663 // Native is quicker. |
663 return _StringBase._concatRangeNative(strings, 0, strings.length); | 664 return _StringBase._concatRangeNative(strings, 0, strings.length); |
664 } | 665 } |
665 var res = _OneByteString._allocate(totalLength); | 666 var res = _OneByteString._allocate(totalLength); |
666 final stringsLength = strings.length; | 667 final stringsLength = strings.length; |
667 int rIx = 0; | 668 int rIx = 0; |
668 for (int i = 0; i < stringsLength; i++) { | 669 for (int i = 0; i < stringsLength; i++) { |
669 _OneByteString e = strings[i]; | 670 _OneByteString e = strings[i]; |
670 final eLength = e.length; | 671 final eLength = e.length; |
671 for (int s = 0; s < eLength; s++) { | 672 for (int s = 0; s < eLength; s++) { |
672 res._setAt(rIx++, e.codeUnitAt(s)); | 673 res._setAt(rIx++, e.codeUnitAt(s)); |
673 } | 674 } |
674 } | 675 } |
675 return res; | 676 return res; |
676 } | 677 } |
677 | 678 |
678 int indexOf(Pattern pattern, [int start = 0]) { | 679 int indexOf(Pattern pattern, [int start = 0]) { |
679 // Specialize for single character pattern. | 680 // Specialize for single character pattern. |
680 final pCid = pattern._cid; | 681 final pCid = ClassID.getID(pattern); |
681 if ((pCid == _OneByteString._classId) || | 682 if ((pCid == _OneByteString._classId) || |
682 (pCid == _TwoByteString._classId) || | 683 (pCid == _TwoByteString._classId) || |
683 (pCid == _ExternalOneByteString._classId)) { | 684 (pCid == _ExternalOneByteString._classId)) { |
684 final len = this.length; | 685 final len = this.length; |
685 if ((pattern.length == 1) && (start >= 0) && (start < len)) { | 686 if ((pattern.length == 1) && (start >= 0) && (start < len)) { |
686 final patternCu0 = pattern.codeUnitAt(0); | 687 final patternCu0 = pattern.codeUnitAt(0); |
687 if (patternCu0 > 0xFF) { | 688 if (patternCu0 > 0xFF) { |
688 return -1; | 689 return -1; |
689 } | 690 } |
690 for (int i = start; i < len; i++) { | 691 for (int i = start; i < len; i++) { |
691 if (this.codeUnitAt(i) == patternCu0) { | 692 if (this.codeUnitAt(i) == patternCu0) { |
692 return i; | 693 return i; |
693 } | 694 } |
694 } | 695 } |
695 return -1; | 696 return -1; |
696 } | 697 } |
697 } | 698 } |
698 return super.indexOf(pattern, start); | 699 return super.indexOf(pattern, start); |
699 } | 700 } |
700 | 701 |
701 bool contains(Pattern pattern, [int start = 0]) { | 702 bool contains(Pattern pattern, [int start = 0]) { |
702 final pCid = pattern._cid; | 703 final pCid = ClassID.getID(pattern); |
703 if ((pCid == _OneByteString._classId) || | 704 if ((pCid == _OneByteString._classId) || |
704 (pCid == _TwoByteString._classId) || | 705 (pCid == _TwoByteString._classId) || |
705 (pCid == _ExternalOneByteString._classId)) { | 706 (pCid == _ExternalOneByteString._classId)) { |
706 final len = this.length; | 707 final len = this.length; |
707 if ((pattern.length == 1) && (start >= 0) && (start < len)) { | 708 if ((pattern.length == 1) && (start >= 0) && (start < len)) { |
708 final patternCu0 = pattern.codeUnitAt(0); | 709 final patternCu0 = pattern.codeUnitAt(0); |
709 if (patternCu0 > 0xFF) { | 710 if (patternCu0 > 0xFF) { |
710 return false; | 711 return false; |
711 } | 712 } |
712 for (int i = start; i < len; i++) { | 713 for (int i = start; i < len; i++) { |
(...skipping 16 matching lines...) Expand all Loading... |
729 int index = 0; | 730 int index = 0; |
730 for (int i = 0; i < times; i ++) { | 731 for (int i = 0; i < times; i ++) { |
731 for (int j = 0; j < length; j++) { | 732 for (int j = 0; j < length; j++) { |
732 result._setAt(index++, this.codeUnitAt(j)); | 733 result._setAt(index++, this.codeUnitAt(j)); |
733 } | 734 } |
734 } | 735 } |
735 return result; | 736 return result; |
736 } | 737 } |
737 | 738 |
738 String padLeft(int width, [String padding = ' ']) { | 739 String padLeft(int width, [String padding = ' ']) { |
739 int padCid = padding._cid; | 740 int padCid = ClassID.getID(padding); |
740 if (padCid != _OneByteString._classId && | 741 if (padCid != _OneByteString._classId && |
741 padCid != _ExternalOneByteString._classId) { | 742 padCid != _ExternalOneByteString._classId) { |
742 return super.padLeft(width, padding); | 743 return super.padLeft(width, padding); |
743 } | 744 } |
744 int length = this.length; | 745 int length = this.length; |
745 int delta = width - length; | 746 int delta = width - length; |
746 if (delta <= 0) return this; | 747 if (delta <= 0) return this; |
747 int padLength = padding.length; | 748 int padLength = padding.length; |
748 int resultLength = padLength * delta + length; | 749 int resultLength = padLength * delta + length; |
749 _OneByteString result = _OneByteString._allocate(resultLength); | 750 _OneByteString result = _OneByteString._allocate(resultLength); |
(...skipping 10 matching lines...) Expand all Loading... |
760 } | 761 } |
761 } | 762 } |
762 } | 763 } |
763 for (int i = 0; i < length; i++) { | 764 for (int i = 0; i < length; i++) { |
764 result._setAt(index++, this.codeUnitAt(i)); | 765 result._setAt(index++, this.codeUnitAt(i)); |
765 } | 766 } |
766 return result; | 767 return result; |
767 } | 768 } |
768 | 769 |
769 String padRight(int width, [String padding = ' ']) { | 770 String padRight(int width, [String padding = ' ']) { |
770 int padCid = padding._cid; | 771 int padCid = ClassID.getID(padding); |
771 if (padCid != _OneByteString._classId && | 772 if (padCid != _OneByteString._classId && |
772 padCid != _ExternalOneByteString._classId) { | 773 padCid != _ExternalOneByteString._classId) { |
773 return super.padRight(width, padding); | 774 return super.padRight(width, padding); |
774 } | 775 } |
775 int length = this.length; | 776 int length = this.length; |
776 int delta = width - length; | 777 int delta = width - length; |
777 if (delta <= 0) return this; | 778 if (delta <= 0) return this; |
778 int padLength = padding.length; | 779 int padLength = padding.length; |
779 int resultLength = length + padLength * delta; | 780 int resultLength = length + padLength * delta; |
780 _OneByteString result = _OneByteString._allocate(resultLength); | 781 _OneByteString result = _OneByteString._allocate(resultLength); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 static _OneByteString _allocateFromOneByteList(List<int> list) | 900 static _OneByteString _allocateFromOneByteList(List<int> list) |
900 native "OneByteString_allocateFromOneByteList"; | 901 native "OneByteString_allocateFromOneByteList"; |
901 | 902 |
902 // This is internal helper method. Code point value must be a valid | 903 // This is internal helper method. Code point value must be a valid |
903 // Latin1 value (0..0xFF), index must be valid. | 904 // Latin1 value (0..0xFF), index must be valid. |
904 void _setAt(int index, int codePoint) native "OneByteString_setAt"; | 905 void _setAt(int index, int codePoint) native "OneByteString_setAt"; |
905 } | 906 } |
906 | 907 |
907 | 908 |
908 class _TwoByteString extends _StringBase implements String { | 909 class _TwoByteString extends _StringBase implements String { |
909 static final int _classId = "\u{FFFF}"._cid; | 910 static final int _classId = ClassID.getID("\u{FFFF}"); |
910 | 911 |
911 factory _TwoByteString._uninstantiable() { | 912 factory _TwoByteString._uninstantiable() { |
912 throw new UnsupportedError( | 913 throw new UnsupportedError( |
913 "_TwoByteString can only be allocated by the VM"); | 914 "_TwoByteString can only be allocated by the VM"); |
914 } | 915 } |
915 | 916 |
916 bool _isWhitespace(int codePoint) { | 917 bool _isWhitespace(int codePoint) { |
917 return _StringBase._isTwoByteWhitespace(codePoint); | 918 return _StringBase._isTwoByteWhitespace(codePoint); |
918 } | 919 } |
919 | 920 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 class _CodeUnits extends Object with ListMixin<int>, | 995 class _CodeUnits extends Object with ListMixin<int>, |
995 UnmodifiableListMixin<int> { | 996 UnmodifiableListMixin<int> { |
996 /** The string that this is the code units of. */ | 997 /** The string that this is the code units of. */ |
997 String _string; | 998 String _string; |
998 | 999 |
999 _CodeUnits(this._string); | 1000 _CodeUnits(this._string); |
1000 | 1001 |
1001 int get length => _string.length; | 1002 int get length => _string.length; |
1002 int operator[](int i) => _string.codeUnitAt(i); | 1003 int operator[](int i) => _string.codeUnitAt(i); |
1003 } | 1004 } |
OLD | NEW |