Chromium Code Reviews| Index: runtime/lib/string_patch.dart |
| diff --git a/runtime/lib/string_patch.dart b/runtime/lib/string_patch.dart |
| index 2604cb80ff899ada73774f69bc68b10fede34c16..49e78ea37826000ca725bec314d878564186edac 100644 |
| --- a/runtime/lib/string_patch.dart |
| +++ b/runtime/lib/string_patch.dart |
| @@ -302,6 +302,57 @@ class _StringBase { |
| } |
| } |
| + String repeat(int times, [String separator = ""]) { |
| + if (times < 0) throw new RangeError.value(times); |
| + if (times == 0) return ""; |
| + if (times == 1) return this; |
| + StringBuffer buffer = new StringBuffer(this); |
| + if (separator.isEmpty) { |
| + for (int i = 1; i < times; i++) { |
| + buffer.write(this); |
| + } |
| + return buffer.toString(); |
| + } |
| + for (int i = 1; i < times; i++) { |
| + buffer.write(separator); |
| + buffer.write(this); |
| + } |
| + return buffer.toString(); |
| + } |
| + |
| + String padLeft(int newSize, String padding, { int size }) { |
| + if (newSize < 0) throw new RangeError.value(newSize); |
| + if (size == null) { |
| + size = this.length; |
| + } else if (size < 0) { |
| + throw new RangeError.value(size); |
| + } |
| + int delta = newSize - size; |
| + if (delta <= 0) return this; |
| + StringBuffer buffer = new StringBuffer(); |
| + 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 :)
|
| + buffer.write(padding); |
| + } |
| + buffer.write(this); |
| + return buffer.toString(); |
| + } |
| + |
| + String padRight(int newSize, String padding, { int size }) { |
| + if (newSize < 0) throw new RangeError.value(newSize); |
| + if (size == null) { |
| + size = this.length; |
| + } else if (size < 0) { |
| + throw new RangeError.value(size); |
| + } |
| + int delta = newSize - size; |
| + if (delta <= 0) return this; |
| + StringBuffer buffer = new StringBuffer(this); |
| + for (int i = 0; i < delta; i++) { |
| + buffer.write(padding); |
| + } |
| + return buffer.toString(); |
| + } |
| + |
| bool contains(Pattern pattern, [int startIndex = 0]) { |
| if (pattern is String) { |
| if (startIndex < 0 || startIndex > this.length) { |
| @@ -632,6 +683,104 @@ class _OneByteString extends _StringBase implements String { |
| return super.contains(pattern, start); |
| } |
| + String repeat(int times, [String separator = ""]) { |
| + if (times == 0) return ""; |
| + if (times == 1) return this; |
| + if (times < 0) throw new RangeError.value(times); |
| + if (separator.isEmpty) { |
| + int length = this.length; |
| + if (this.isEmpty) return this; // Don't clone empty string. |
| + _OneByteString result = _OneByteString._allocate(length * times); |
| + int index = 0; |
| + for (int i = 0; i < times; i ++) { |
| + for (int j = 0; j < length; j++) { |
| + result._setAt(index++, this.codeUnitAt(j)); |
| + } |
| + } |
| + return result; |
| + } |
| + int sepCid = separator._cid; |
| + if (sepCid != _OneByteString._classId && |
| + sepCid != _ExternalOneByteString._classId) { |
| + return super.repeat(times, separator); |
| + } |
| + int length = this.length; |
| + int sepLength = separator.length; |
| + _OneByteString result = |
| + _OneByteString._allocate(length * times + sepLength * (times - 1)); |
| + int index = 0; |
| + for (int j = 0; j < length; j++) { |
| + result._setAt(index++, this.codeUnitAt(j)); |
| + } |
| + for (int i = 1; i < times; i ++) { |
| + for (int j = 0; j < sepLength; j++) { |
| + result._setAt(index++, separator.codeUnitAt(j)); |
| + } |
| + for (int j = 0; j < length; j++) { |
| + result._setAt(index++, this.codeUnitAt(j)); |
| + } |
| + } |
| + return result; |
| + } |
| + |
| + String padLeft(int newSize, String padding, { int size }) { |
| + int padCid = padding._cid; |
| + if (padCid != _OneByteString._classId && |
| + padCid != _ExternalOneByteString._classId) { |
| + return super.padLeft(newSize, padding, size: size); |
| + } |
| + int length = this.length; |
| + if (size == null) { |
| + size = length; |
| + } else if (size < 0) { |
| + throw new RangeError.value(size); |
| + } |
| + int delta = newSize - size; |
| + if (delta <= 0) return this; |
| + int padLength = padding.length; |
| + _OneByteString result = |
| + _OneByteString._allocate(length + delta * padLength); |
| + int index = 0; |
| + for (int i = 0; i < delta; i++) { |
| + for (int j = 0; j < padLength; j++) { |
| + result._setAt(index++, padding.codeUnitAt(j)); |
| + } |
| + } |
| + for (int i = 0; i < length; i++) { |
| + result._setAt(index++, this.codeUnitAt(i)); |
| + } |
| + return result; |
| + } |
| + |
| + String padRight(int newSize, String padding, { int size }) { |
| + int padCid = padding._cid; |
| + if (padCid != _OneByteString._classId && |
| + padCid != _ExternalOneByteString._classId) { |
| + return super.padRight(newSize, padding, size: size); |
| + } |
| + int length = this.length; |
| + if (size == null) { |
| + size = length; |
| + } else if (size < 0) { |
| + throw new RangeError.value(size); |
| + } |
| + int delta = newSize - size; |
| + if (delta <= 0) return this; |
| + int padLength = padding.length; |
| + _OneByteString result = |
| + _OneByteString._allocate(length + delta * padLength); |
| + int index = 0; |
| + for (int i = 0; i < length; i++) { |
| + result._setAt(index++, this.codeUnitAt(i)); |
| + } |
| + for (int i = 0; i < delta; i++) { |
| + for (int j = 0; j < padLength; j++) { |
| + result._setAt(index++, padding.codeUnitAt(j)); |
| + } |
| + } |
| + return result; |
| + } |
| + |
| // Allocates a string of given length, expecting its content to be |
| // set using _setAt. |
| static _OneByteString _allocate(int length) native "OneByteString_allocate"; |