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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 = ClassID.getID(charCodes); | 57 final ccid = ClassID.getID(charCodes); |
58 bool isOneByteString = false; | 58 bool isOneByteString = false; |
59 if ((ccid != _List._classId) && | 59 if ((ccid != ClassID.cidArray) && |
60 (ccid != _GrowableList._classId) && | 60 (ccid != ClassID.cidGrowableObjectArray) && |
61 (ccid != _ImmutableList._classId)) { | 61 (ccid != ClassID.cidImmutableArray)) { |
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 } |
68 final len = charCodes.length; | 68 final len = charCodes.length; |
69 if (!isOneByteString) { | 69 if (!isOneByteString) { |
70 for (int i = 0; i < len; i++) { | 70 for (int i = 0; i < len; i++) { |
71 int e = charCodes[i]; | 71 int e = charCodes[i]; |
(...skipping 431 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 && (ClassID.getID(s) == _OneByteString._classId)) { | 513 if (isOneByteString && (ClassID.getID(s) == ClassID.cidOneByteString)) { |
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 = ClassID.getID("A"); | |
629 | 628 |
630 factory _OneByteString._uninstantiable() { | 629 factory _OneByteString._uninstantiable() { |
631 throw new UnsupportedError( | 630 throw new UnsupportedError( |
632 "_OneByteString can only be allocated by the VM"); | 631 "_OneByteString can only be allocated by the VM"); |
633 } | 632 } |
634 | 633 |
635 int get hashCode native "String_getHashCode"; | 634 int get hashCode native "String_getHashCode"; |
636 | 635 |
637 bool _isWhitespace(int codePoint) { | 636 bool _isWhitespace(int codePoint) { |
638 return _StringBase._isOneByteWhitespace(codePoint); | 637 return _StringBase._isOneByteWhitespace(codePoint); |
639 } | 638 } |
640 | 639 |
641 bool operator ==(Object other) { | 640 bool operator ==(Object other) { |
642 return super == other; | 641 return super == other; |
643 } | 642 } |
644 | 643 |
645 String _substringUncheckedNative(int startIndex, int endIndex) | 644 String _substringUncheckedNative(int startIndex, int endIndex) |
646 native "OneByteString_substringUnchecked"; | 645 native "OneByteString_substringUnchecked"; |
647 | 646 |
648 List<String> _splitWithCharCode(int charCode) | 647 List<String> _splitWithCharCode(int charCode) |
649 native "OneByteString_splitWithCharCode"; | 648 native "OneByteString_splitWithCharCode"; |
650 | 649 |
651 List<String> split(Pattern pattern) { | 650 List<String> split(Pattern pattern) { |
652 if ((ClassID.getID(pattern) == _OneByteString._classId) && | 651 if ((ClassID.getID(pattern) == ClassID.cidOneByteString) && |
653 (pattern.length == 1)) { | 652 (pattern.length == 1)) { |
654 return _splitWithCharCode(pattern.codeUnitAt(0)); | 653 return _splitWithCharCode(pattern.codeUnitAt(0)); |
655 } | 654 } |
656 return super.split(pattern); | 655 return super.split(pattern); |
657 } | 656 } |
658 | 657 |
659 // All element of 'strings' must be OneByteStrings. | 658 // All element of 'strings' must be OneByteStrings. |
660 static _concatAll(List<String> strings, int totalLength) { | 659 static _concatAll(List<String> strings, int totalLength) { |
661 // TODO(srdjan): Improve code below and raise or eliminate the limit. | 660 // TODO(srdjan): Improve code below and raise or eliminate the limit. |
662 if (totalLength > 128) { | 661 if (totalLength > 128) { |
663 // Native is quicker. | 662 // Native is quicker. |
664 return _StringBase._concatRangeNative(strings, 0, strings.length); | 663 return _StringBase._concatRangeNative(strings, 0, strings.length); |
665 } | 664 } |
666 var res = _OneByteString._allocate(totalLength); | 665 var res = _OneByteString._allocate(totalLength); |
667 final stringsLength = strings.length; | 666 final stringsLength = strings.length; |
668 int rIx = 0; | 667 int rIx = 0; |
669 for (int i = 0; i < stringsLength; i++) { | 668 for (int i = 0; i < stringsLength; i++) { |
670 _OneByteString e = strings[i]; | 669 _OneByteString e = strings[i]; |
671 final eLength = e.length; | 670 final eLength = e.length; |
672 for (int s = 0; s < eLength; s++) { | 671 for (int s = 0; s < eLength; s++) { |
673 res._setAt(rIx++, e.codeUnitAt(s)); | 672 res._setAt(rIx++, e.codeUnitAt(s)); |
674 } | 673 } |
675 } | 674 } |
676 return res; | 675 return res; |
677 } | 676 } |
678 | 677 |
679 int indexOf(Pattern pattern, [int start = 0]) { | 678 int indexOf(Pattern pattern, [int start = 0]) { |
680 // Specialize for single character pattern. | 679 // Specialize for single character pattern. |
681 final pCid = ClassID.getID(pattern); | 680 final pCid = ClassID.getID(pattern); |
682 if ((pCid == _OneByteString._classId) || | 681 if ((pCid == ClassID.cidOneByteString) || |
683 (pCid == _TwoByteString._classId) || | 682 (pCid == ClassID.cidTwoByteString) || |
684 (pCid == _ExternalOneByteString._classId)) { | 683 (pCid == ClassID.cidExternalOneByteString)) { |
685 final len = this.length; | 684 final len = this.length; |
686 if ((pattern.length == 1) && (start >= 0) && (start < len)) { | 685 if ((pattern.length == 1) && (start >= 0) && (start < len)) { |
687 final patternCu0 = pattern.codeUnitAt(0); | 686 final patternCu0 = pattern.codeUnitAt(0); |
688 if (patternCu0 > 0xFF) { | 687 if (patternCu0 > 0xFF) { |
689 return -1; | 688 return -1; |
690 } | 689 } |
691 for (int i = start; i < len; i++) { | 690 for (int i = start; i < len; i++) { |
692 if (this.codeUnitAt(i) == patternCu0) { | 691 if (this.codeUnitAt(i) == patternCu0) { |
693 return i; | 692 return i; |
694 } | 693 } |
695 } | 694 } |
696 return -1; | 695 return -1; |
697 } | 696 } |
698 } | 697 } |
699 return super.indexOf(pattern, start); | 698 return super.indexOf(pattern, start); |
700 } | 699 } |
701 | 700 |
702 bool contains(Pattern pattern, [int start = 0]) { | 701 bool contains(Pattern pattern, [int start = 0]) { |
703 final pCid = ClassID.getID(pattern); | 702 final pCid = ClassID.getID(pattern); |
704 if ((pCid == _OneByteString._classId) || | 703 if ((pCid == ClassID.cidOneByteString) || |
705 (pCid == _TwoByteString._classId) || | 704 (pCid == ClassID.cidTwoByteString) || |
706 (pCid == _ExternalOneByteString._classId)) { | 705 (pCid == ClassID.cidExternalOneByteString)) { |
707 final len = this.length; | 706 final len = this.length; |
708 if ((pattern.length == 1) && (start >= 0) && (start < len)) { | 707 if ((pattern.length == 1) && (start >= 0) && (start < len)) { |
709 final patternCu0 = pattern.codeUnitAt(0); | 708 final patternCu0 = pattern.codeUnitAt(0); |
710 if (patternCu0 > 0xFF) { | 709 if (patternCu0 > 0xFF) { |
711 return false; | 710 return false; |
712 } | 711 } |
713 for (int i = start; i < len; i++) { | 712 for (int i = start; i < len; i++) { |
714 if (this.codeUnitAt(i) == patternCu0) { | 713 if (this.codeUnitAt(i) == patternCu0) { |
715 return true; | 714 return true; |
716 } | 715 } |
(...skipping 14 matching lines...) Expand all Loading... |
731 for (int i = 0; i < times; i ++) { | 730 for (int i = 0; i < times; i ++) { |
732 for (int j = 0; j < length; j++) { | 731 for (int j = 0; j < length; j++) { |
733 result._setAt(index++, this.codeUnitAt(j)); | 732 result._setAt(index++, this.codeUnitAt(j)); |
734 } | 733 } |
735 } | 734 } |
736 return result; | 735 return result; |
737 } | 736 } |
738 | 737 |
739 String padLeft(int width, [String padding = ' ']) { | 738 String padLeft(int width, [String padding = ' ']) { |
740 int padCid = ClassID.getID(padding); | 739 int padCid = ClassID.getID(padding); |
741 if (padCid != _OneByteString._classId && | 740 if ((padCid != ClassID.cidOneByteString) && |
742 padCid != _ExternalOneByteString._classId) { | 741 (padCid != ClassID.cidExternalOneByteString)) { |
743 return super.padLeft(width, padding); | 742 return super.padLeft(width, padding); |
744 } | 743 } |
745 int length = this.length; | 744 int length = this.length; |
746 int delta = width - length; | 745 int delta = width - length; |
747 if (delta <= 0) return this; | 746 if (delta <= 0) return this; |
748 int padLength = padding.length; | 747 int padLength = padding.length; |
749 int resultLength = padLength * delta + length; | 748 int resultLength = padLength * delta + length; |
750 _OneByteString result = _OneByteString._allocate(resultLength); | 749 _OneByteString result = _OneByteString._allocate(resultLength); |
751 int index = 0; | 750 int index = 0; |
752 if (padLength == 1) { | 751 if (padLength == 1) { |
753 int padChar = padding.codeUnitAt(0); | 752 int padChar = padding.codeUnitAt(0); |
754 for (int i = 0; i < delta; i++) { | 753 for (int i = 0; i < delta; i++) { |
755 result._setAt(index++, padChar); | 754 result._setAt(index++, padChar); |
756 } | 755 } |
757 } else { | 756 } else { |
758 for (int i = 0; i < delta; i++) { | 757 for (int i = 0; i < delta; i++) { |
759 for (int j = 0; j < padLength; j++) { | 758 for (int j = 0; j < padLength; j++) { |
760 result._setAt(index++, padding.codeUnitAt(j)); | 759 result._setAt(index++, padding.codeUnitAt(j)); |
761 } | 760 } |
762 } | 761 } |
763 } | 762 } |
764 for (int i = 0; i < length; i++) { | 763 for (int i = 0; i < length; i++) { |
765 result._setAt(index++, this.codeUnitAt(i)); | 764 result._setAt(index++, this.codeUnitAt(i)); |
766 } | 765 } |
767 return result; | 766 return result; |
768 } | 767 } |
769 | 768 |
770 String padRight(int width, [String padding = ' ']) { | 769 String padRight(int width, [String padding = ' ']) { |
771 int padCid = ClassID.getID(padding); | 770 int padCid = ClassID.getID(padding); |
772 if (padCid != _OneByteString._classId && | 771 if ((padCid != ClassID.cidOneByteString) && |
773 padCid != _ExternalOneByteString._classId) { | 772 (padCid != ClassID.cidExternalOneByteString)) { |
774 return super.padRight(width, padding); | 773 return super.padRight(width, padding); |
775 } | 774 } |
776 int length = this.length; | 775 int length = this.length; |
777 int delta = width - length; | 776 int delta = width - length; |
778 if (delta <= 0) return this; | 777 if (delta <= 0) return this; |
779 int padLength = padding.length; | 778 int padLength = padding.length; |
780 int resultLength = length + padLength * delta; | 779 int resultLength = length + padLength * delta; |
781 _OneByteString result = _OneByteString._allocate(resultLength); | 780 _OneByteString result = _OneByteString._allocate(resultLength); |
782 int index = 0; | 781 int index = 0; |
783 for (int i = 0; i < length; i++) { | 782 for (int i = 0; i < length; i++) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
900 static _OneByteString _allocateFromOneByteList(List<int> list) | 899 static _OneByteString _allocateFromOneByteList(List<int> list) |
901 native "OneByteString_allocateFromOneByteList"; | 900 native "OneByteString_allocateFromOneByteList"; |
902 | 901 |
903 // This is internal helper method. Code point value must be a valid | 902 // This is internal helper method. Code point value must be a valid |
904 // Latin1 value (0..0xFF), index must be valid. | 903 // Latin1 value (0..0xFF), index must be valid. |
905 void _setAt(int index, int codePoint) native "OneByteString_setAt"; | 904 void _setAt(int index, int codePoint) native "OneByteString_setAt"; |
906 } | 905 } |
907 | 906 |
908 | 907 |
909 class _TwoByteString extends _StringBase implements String { | 908 class _TwoByteString extends _StringBase implements String { |
910 static final int _classId = ClassID.getID("\u{FFFF}"); | |
911 | |
912 factory _TwoByteString._uninstantiable() { | 909 factory _TwoByteString._uninstantiable() { |
913 throw new UnsupportedError( | 910 throw new UnsupportedError( |
914 "_TwoByteString can only be allocated by the VM"); | 911 "_TwoByteString can only be allocated by the VM"); |
915 } | 912 } |
916 | 913 |
917 bool _isWhitespace(int codePoint) { | 914 bool _isWhitespace(int codePoint) { |
918 return _StringBase._isTwoByteWhitespace(codePoint); | 915 return _StringBase._isTwoByteWhitespace(codePoint); |
919 } | 916 } |
920 | 917 |
921 bool operator ==(Object other) { | 918 bool operator ==(Object other) { |
922 return super == other; | 919 return super == other; |
923 } | 920 } |
924 } | 921 } |
925 | 922 |
926 | 923 |
927 class _ExternalOneByteString extends _StringBase implements String { | 924 class _ExternalOneByteString extends _StringBase implements String { |
928 static final int _classId = _getCid(); | |
929 | |
930 factory _ExternalOneByteString._uninstantiable() { | 925 factory _ExternalOneByteString._uninstantiable() { |
931 throw new UnsupportedError( | 926 throw new UnsupportedError( |
932 "_ExternalOneByteString can only be allocated by the VM"); | 927 "_ExternalOneByteString can only be allocated by the VM"); |
933 } | 928 } |
934 | 929 |
935 bool _isWhitespace(int codePoint) { | 930 bool _isWhitespace(int codePoint) { |
936 return _StringBase._isOneByteWhitespace(codePoint); | 931 return _StringBase._isOneByteWhitespace(codePoint); |
937 } | 932 } |
938 | 933 |
939 bool operator ==(Object other) { | 934 bool operator ==(Object other) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 class _CodeUnits extends Object with ListMixin<int>, | 990 class _CodeUnits extends Object with ListMixin<int>, |
996 UnmodifiableListMixin<int> { | 991 UnmodifiableListMixin<int> { |
997 /** The string that this is the code units of. */ | 992 /** The string that this is the code units of. */ |
998 String _string; | 993 String _string; |
999 | 994 |
1000 _CodeUnits(this._string); | 995 _CodeUnits(this._string); |
1001 | 996 |
1002 int get length => _string.length; | 997 int get length => _string.length; |
1003 int operator[](int i) => _string.codeUnitAt(i); | 998 int operator[](int i) => _string.codeUnitAt(i); |
1004 } | 999 } |
OLD | NEW |