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 = ""]) { | |
306 if (times < 0) throw new RangeError.value(times); | |
307 if (times == 0) return ""; | |
308 if (times == 1) return this; | |
309 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++) { | |
317 buffer.write(separator); | |
318 buffer.write(this); | |
319 } | |
320 return buffer.toString(); | |
321 } | |
322 | |
323 String padLeft(int newSize, String padding, { int size }) { | |
324 if (newSize < 0) throw new RangeError.value(newSize); | |
325 if (size == null) { | |
326 size = this.length; | |
327 } else if (size < 0) { | |
328 throw new RangeError.value(size); | |
329 } | |
330 int delta = newSize - size; | |
331 if (delta <= 0) return this; | |
332 StringBuffer buffer = new StringBuffer(); | |
333 for (int i = 0; i < delta; i++) { | |
floitsch
2014/02/19 10:20:36
In theory one could do the same trick as for effic
Lasse Reichstein Nielsen
2014/02/19 11:43:16
It is cute, though :)
| |
334 buffer.write(padding); | |
335 } | |
336 buffer.write(this); | |
337 return buffer.toString(); | |
338 } | |
339 | |
340 String padRight(int newSize, String padding, { int size }) { | |
341 if (newSize < 0) throw new RangeError.value(newSize); | |
342 if (size == null) { | |
343 size = this.length; | |
344 } else if (size < 0) { | |
345 throw new RangeError.value(size); | |
346 } | |
347 int delta = newSize - size; | |
348 if (delta <= 0) return this; | |
349 StringBuffer buffer = new StringBuffer(this); | |
350 for (int i = 0; i < delta; i++) { | |
351 buffer.write(padding); | |
352 } | |
353 return buffer.toString(); | |
354 } | |
355 | |
305 bool contains(Pattern pattern, [int startIndex = 0]) { | 356 bool contains(Pattern pattern, [int startIndex = 0]) { |
306 if (pattern is String) { | 357 if (pattern is String) { |
307 if (startIndex < 0 || startIndex > this.length) { | 358 if (startIndex < 0 || startIndex > this.length) { |
308 throw new RangeError.range(startIndex, 0, this.length); | 359 throw new RangeError.range(startIndex, 0, this.length); |
309 } | 360 } |
310 return indexOf(pattern, startIndex) >= 0; | 361 return indexOf(pattern, startIndex) >= 0; |
311 } | 362 } |
312 return pattern.allMatches(this.substring(startIndex)).isNotEmpty; | 363 return pattern.allMatches(this.substring(startIndex)).isNotEmpty; |
313 } | 364 } |
314 | 365 |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
625 if (this.codeUnitAt(i) == patternCu0) { | 676 if (this.codeUnitAt(i) == patternCu0) { |
626 return true; | 677 return true; |
627 } | 678 } |
628 } | 679 } |
629 return false; | 680 return false; |
630 } | 681 } |
631 } | 682 } |
632 return super.contains(pattern, start); | 683 return super.contains(pattern, start); |
633 } | 684 } |
634 | 685 |
686 String repeat(int times, [String separator = ""]) { | |
687 if (times == 0) return ""; | |
688 if (times == 1) return this; | |
689 if (times < 0) throw new RangeError.value(times); | |
690 if (separator.isEmpty) { | |
691 int length = this.length; | |
692 if (this.isEmpty) return this; // Don't clone empty string. | |
693 _OneByteString result = _OneByteString._allocate(length * times); | |
694 int index = 0; | |
695 for (int i = 0; i < times; i ++) { | |
696 for (int j = 0; j < length; j++) { | |
697 result._setAt(index++, this.codeUnitAt(j)); | |
698 } | |
699 } | |
700 return result; | |
701 } | |
702 int sepCid = separator._cid; | |
703 if (sepCid != _OneByteString._classId && | |
704 sepCid != _ExternalOneByteString._classId) { | |
705 return super.repeat(times, separator); | |
706 } | |
707 int length = this.length; | |
708 int sepLength = separator.length; | |
709 _OneByteString result = | |
710 _OneByteString._allocate(length * times + sepLength * (times - 1)); | |
711 int index = 0; | |
712 for (int j = 0; j < length; j++) { | |
713 result._setAt(index++, this.codeUnitAt(j)); | |
714 } | |
715 for (int i = 1; i < times; i ++) { | |
716 for (int j = 0; j < sepLength; j++) { | |
717 result._setAt(index++, separator.codeUnitAt(j)); | |
718 } | |
719 for (int j = 0; j < length; j++) { | |
720 result._setAt(index++, this.codeUnitAt(j)); | |
721 } | |
722 } | |
723 return result; | |
724 } | |
725 | |
726 String padLeft(int newSize, String padding, { int size }) { | |
727 int padCid = padding._cid; | |
728 if (padCid != _OneByteString._classId && | |
729 padCid != _ExternalOneByteString._classId) { | |
730 return super.padLeft(newSize, padding, size: size); | |
731 } | |
732 int length = this.length; | |
733 if (size == null) { | |
734 size = length; | |
735 } else if (size < 0) { | |
736 throw new RangeError.value(size); | |
737 } | |
738 int delta = newSize - size; | |
739 if (delta <= 0) return this; | |
740 int padLength = padding.length; | |
741 _OneByteString result = | |
742 _OneByteString._allocate(length + delta * padLength); | |
743 int index = 0; | |
744 for (int i = 0; i < delta; i++) { | |
745 for (int j = 0; j < padLength; j++) { | |
746 result._setAt(index++, padding.codeUnitAt(j)); | |
747 } | |
748 } | |
749 for (int i = 0; i < length; i++) { | |
750 result._setAt(index++, this.codeUnitAt(i)); | |
751 } | |
752 return result; | |
753 } | |
754 | |
755 String padRight(int newSize, String padding, { int size }) { | |
756 int padCid = padding._cid; | |
757 if (padCid != _OneByteString._classId && | |
758 padCid != _ExternalOneByteString._classId) { | |
759 return super.padRight(newSize, padding, size: size); | |
760 } | |
761 int length = this.length; | |
762 if (size == null) { | |
763 size = length; | |
764 } else if (size < 0) { | |
765 throw new RangeError.value(size); | |
766 } | |
767 int delta = newSize - size; | |
768 if (delta <= 0) return this; | |
769 int padLength = padding.length; | |
770 _OneByteString result = | |
771 _OneByteString._allocate(length + delta * padLength); | |
772 int index = 0; | |
773 for (int i = 0; i < length; i++) { | |
774 result._setAt(index++, this.codeUnitAt(i)); | |
775 } | |
776 for (int i = 0; i < delta; i++) { | |
777 for (int j = 0; j < padLength; j++) { | |
778 result._setAt(index++, padding.codeUnitAt(j)); | |
779 } | |
780 } | |
781 return result; | |
782 } | |
783 | |
635 // Allocates a string of given length, expecting its content to be | 784 // Allocates a string of given length, expecting its content to be |
636 // set using _setAt. | 785 // set using _setAt. |
637 static _OneByteString _allocate(int length) native "OneByteString_allocate"; | 786 static _OneByteString _allocate(int length) native "OneByteString_allocate"; |
638 | 787 |
639 | 788 |
640 static _OneByteString _allocateFromOneByteList(List<int> list) | 789 static _OneByteString _allocateFromOneByteList(List<int> list) |
641 native "OneByteString_allocateFromOneByteList"; | 790 native "OneByteString_allocateFromOneByteList"; |
642 | 791 |
643 // This is internal helper method. Code point value must be a valid | 792 // This is internal helper method. Code point value must be a valid |
644 // Latin1 value (0..0xFF), index must be valid. | 793 // Latin1 value (0..0xFF), index must be valid. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
735 class _CodeUnits extends Object with ListMixin<int>, | 884 class _CodeUnits extends Object with ListMixin<int>, |
736 UnmodifiableListMixin<int> { | 885 UnmodifiableListMixin<int> { |
737 /** The string that this is the code units of. */ | 886 /** The string that this is the code units of. */ |
738 String _string; | 887 String _string; |
739 | 888 |
740 _CodeUnits(this._string); | 889 _CodeUnits(this._string); |
741 | 890 |
742 int get length => _string.length; | 891 int get length => _string.length; |
743 int operator[](int i) => _string.codeUnitAt(i); | 892 int operator[](int i) => _string.codeUnitAt(i); |
744 } | 893 } |
OLD | NEW |