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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 } | 295 } |
296 if ((first == 0) && (last == (len - 1))) { | 296 if ((first == 0) && (last == (len - 1))) { |
297 // Returns this string if it does not have leading or trailing | 297 // Returns this string if it does not have leading or trailing |
298 // whitespaces. | 298 // whitespaces. |
299 return this; | 299 return this; |
300 } else { | 300 } else { |
301 return _substringUnchecked(first, last + 1); | 301 return _substringUnchecked(first, last + 1); |
302 } | 302 } |
303 } | 303 } |
304 | 304 |
305 String repeat(int times, [String separator = ""]) { | 305 String operator*(int times) { |
306 if (times < 0) throw new RangeError.value(times); | 306 if (times <= 0) return ""; |
307 if (times == 0) return ""; | |
308 if (times == 1) return this; | 307 if (times == 1) return this; |
309 StringBuffer buffer = new StringBuffer(this); | 308 StringBuffer buffer = new StringBuffer(this); |
310 if (separator.isEmpty) { | |
311 for (int i = 1; i < times; i++) { | |
312 buffer.write(this); | |
313 } | |
314 return buffer.toString(); | |
315 } | |
316 for (int i = 1; i < times; i++) { | 309 for (int i = 1; i < times; i++) { |
317 buffer.write(separator); | |
318 buffer.write(this); | 310 buffer.write(this); |
319 } | 311 } |
320 return buffer.toString(); | 312 return buffer.toString(); |
321 } | 313 } |
322 | 314 |
323 String padLeft(int newLength, String padding) { | 315 String padLeft(int width, [String padding = ' ']) { |
324 if (padding.length != 1) throw new ArgumentError(padding); | 316 int delta = width - this.length; |
325 int delta = newLength - this.length; | |
326 if (delta <= 0) return this; | 317 if (delta <= 0) return this; |
327 StringBuffer buffer = new StringBuffer(); | 318 StringBuffer buffer = new StringBuffer(); |
328 for (int i = 0; i < delta; i++) { | 319 for (int i = 0; i < delta; i++) { |
329 buffer.write(padding); | 320 buffer.write(padding); |
330 } | 321 } |
331 buffer.write(this); | 322 buffer.write(this); |
332 return buffer.toString(); | 323 return buffer.toString(); |
333 } | 324 } |
334 | 325 |
335 String padRight(int newLength, String padding, { int size }) { | 326 String padRight(int width, [String padding = ' ']) { |
336 if (padding.length != 1) throw new ArgumentError(padding); | 327 int delta = width - this.length; |
337 int delta = newLength - this.length; | |
338 if (delta <= 0) return this; | 328 if (delta <= 0) return this; |
339 StringBuffer buffer = new StringBuffer(); | 329 StringBuffer buffer = new StringBuffer(this); |
340 buffer.write(this); | |
341 for (int i = 0; i < delta; i++) { | 330 for (int i = 0; i < delta; i++) { |
342 buffer.write(padding); | 331 buffer.write(padding); |
343 } | 332 } |
344 return buffer.toString(); | 333 return buffer.toString(); |
345 } | 334 } |
346 | 335 |
347 bool contains(Pattern pattern, [int startIndex = 0]) { | 336 bool contains(Pattern pattern, [int startIndex = 0]) { |
348 if (pattern is String) { | 337 if (pattern is String) { |
349 if (startIndex < 0 || startIndex > this.length) { | 338 if (startIndex < 0 || startIndex > this.length) { |
350 throw new RangeError.range(startIndex, 0, this.length); | 339 throw new RangeError.range(startIndex, 0, this.length); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
667 if (this.codeUnitAt(i) == patternCu0) { | 656 if (this.codeUnitAt(i) == patternCu0) { |
668 return true; | 657 return true; |
669 } | 658 } |
670 } | 659 } |
671 return false; | 660 return false; |
672 } | 661 } |
673 } | 662 } |
674 return super.contains(pattern, start); | 663 return super.contains(pattern, start); |
675 } | 664 } |
676 | 665 |
677 String repeat(int times, [String separator = ""]) { | 666 String operator*(int times) { |
Lasse Reichstein Nielsen
2014/02/20 07:16:40
These one-byte optimizations are missing from _Ext
| |
678 if (times == 0) return ""; | 667 if (times <= 0) return ""; |
679 if (times == 1) return this; | 668 if (times == 1) return this; |
680 if (times < 0) throw new RangeError.value(times); | |
681 if (separator.isEmpty) { | |
682 int length = this.length; | |
683 if (this.isEmpty) return this; // Don't clone empty string. | |
684 _OneByteString result = _OneByteString._allocate(length * times); | |
685 int index = 0; | |
686 for (int i = 0; i < times; i ++) { | |
687 for (int j = 0; j < length; j++) { | |
688 result._setAt(index++, this.codeUnitAt(j)); | |
689 } | |
690 } | |
691 return result; | |
692 } | |
693 int sepCid = separator._cid; | |
694 if (sepCid != _OneByteString._classId && | |
695 sepCid != _ExternalOneByteString._classId) { | |
696 return super.repeat(times, separator); | |
697 } | |
698 int length = this.length; | 669 int length = this.length; |
699 int sepLength = separator.length; | 670 if (this.isEmpty) return this; // Don't clone empty string. |
700 _OneByteString result = | 671 _OneByteString result = _OneByteString._allocate(length * times); |
701 _OneByteString._allocate(length * times + sepLength * (times - 1)); | |
702 int index = 0; | 672 int index = 0; |
703 for (int j = 0; j < length; j++) { | 673 for (int i = 0; i < times; i ++) { |
704 result._setAt(index++, this.codeUnitAt(j)); | |
705 } | |
706 for (int i = 1; i < times; i ++) { | |
707 for (int j = 0; j < sepLength; j++) { | |
708 result._setAt(index++, separator.codeUnitAt(j)); | |
709 } | |
710 for (int j = 0; j < length; j++) { | 674 for (int j = 0; j < length; j++) { |
711 result._setAt(index++, this.codeUnitAt(j)); | 675 result._setAt(index++, this.codeUnitAt(j)); |
712 } | 676 } |
713 } | 677 } |
714 return result; | 678 return result; |
715 } | 679 } |
716 | 680 |
717 String padLeft(int newLength, String padding) { | 681 String padLeft(int width, [String padding = ' ']) { |
718 if (padding.length != 1) throw new ArgumentError(padding); | |
719 int padCid = padding._cid; | 682 int padCid = padding._cid; |
720 if (padCid != _OneByteString._classId && | 683 if (padCid != _OneByteString._classId && |
721 padCid != _ExternalOneByteString._classId) { | 684 padCid != _ExternalOneByteString._classId) { |
722 return super.padLeft(newLength, padding); | 685 return super.padLeft(width, padding); |
723 } | 686 } |
724 int length = this.length; | 687 int length = this.length; |
725 int delta = newLength - length; | 688 int delta = width - length; |
726 if (delta <= 0) return this; | 689 if (delta <= 0) return this; |
727 _OneByteString result = _OneByteString._allocate(newLength); | 690 int padLength = padding.length; |
691 int resultLength = padLength * delta + length; | |
692 _OneByteString result = _OneByteString._allocate(resultLength); | |
728 int index = 0; | 693 int index = 0; |
729 int padChar = padding.codeUnitAt(0); | 694 if (padLength == 1) { |
730 for (int i = 0; i < delta; i++) { | 695 int padChar = padding.codeUnitAt(0); |
731 result._setAt(index++, padChar); | 696 for (int i = 0; i < delta; i++) { |
697 result._setAt(index++, padChar); | |
698 } | |
699 } else { | |
700 for (int i = 0; i < delta; i++) { | |
701 for (int j = 0; j < padLength; j++) { | |
702 result._setAt(index++, padding.codeUnitAt(j)); | |
703 } | |
704 } | |
732 } | 705 } |
733 for (int i = 0; i < length; i++) { | 706 for (int i = 0; i < length; i++) { |
734 result._setAt(index++, this.codeUnitAt(i)); | 707 result._setAt(index++, this.codeUnitAt(i)); |
735 } | 708 } |
736 return result; | 709 return result; |
737 } | 710 } |
738 | 711 |
739 String padRight(int newLength, String padding, { int size }) { | 712 String padRight(int width, [String padding = ' ']) { |
740 if (padding.length != 1) throw new ArgumentError(padding); | |
741 int padCid = padding._cid; | 713 int padCid = padding._cid; |
742 if (padCid != _OneByteString._classId && | 714 if (padCid != _OneByteString._classId && |
743 padCid != _ExternalOneByteString._classId) { | 715 padCid != _ExternalOneByteString._classId) { |
744 return super.padRight(newLength, padding); | 716 return super.padRight(width, padding); |
745 } | 717 } |
746 int length = this.length; | 718 int length = this.length; |
747 int delta = newLength - length; | 719 int delta = width - length; |
748 if (delta <= 0) return this; | 720 if (delta <= 0) return this; |
749 _OneByteString result = _OneByteString._allocate(newLength); | 721 int padLength = padding.length; |
722 int resultLength = length + padLength * delta; | |
723 _OneByteString result = _OneByteString._allocate(resultLength); | |
750 int index = 0; | 724 int index = 0; |
751 for (int i = 0; i < length; i++) { | 725 for (int i = 0; i < length; i++) { |
752 result._setAt(index++, this.codeUnitAt(i)); | 726 result._setAt(index++, this.codeUnitAt(i)); |
753 } | 727 } |
754 int padChar = padding.codeUnitAt(0); | 728 if (padLength == 1) { |
755 for (int i = 0; i < delta; i++) { | 729 int padChar = padding.codeUnitAt(0); |
756 result._setAt(index++, padChar); | 730 for (int i = 0; i < delta; i++) { |
731 result._setAt(index++, padChar); | |
732 } | |
733 } else { | |
734 for (int i = 0; i < delta; i++) { | |
735 for (int j = 0; j < padLength; j++) { | |
736 result._setAt(index++, padding.codeUnitAt(j)); | |
737 } | |
738 } | |
757 } | 739 } |
758 return result; | 740 return result; |
759 } | 741 } |
760 | 742 |
761 // Allocates a string of given length, expecting its content to be | 743 // Allocates a string of given length, expecting its content to be |
762 // set using _setAt. | 744 // set using _setAt. |
763 static _OneByteString _allocate(int length) native "OneByteString_allocate"; | 745 static _OneByteString _allocate(int length) native "OneByteString_allocate"; |
764 | 746 |
765 | 747 |
766 static _OneByteString _allocateFromOneByteList(List<int> list) | 748 static _OneByteString _allocateFromOneByteList(List<int> list) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
861 class _CodeUnits extends Object with ListMixin<int>, | 843 class _CodeUnits extends Object with ListMixin<int>, |
862 UnmodifiableListMixin<int> { | 844 UnmodifiableListMixin<int> { |
863 /** The string that this is the code units of. */ | 845 /** The string that this is the code units of. */ |
864 String _string; | 846 String _string; |
865 | 847 |
866 _CodeUnits(this._string); | 848 _CodeUnits(this._string); |
867 | 849 |
868 int get length => _string.length; | 850 int get length => _string.length; |
869 int operator[](int i) => _string.codeUnitAt(i); | 851 int operator[](int i) => _string.codeUnitAt(i); |
870 } | 852 } |
OLD | NEW |