Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Unified Diff: sdk/lib/_internal/lib/js_string.dart

Issue 171773003: Allow multi-codeunit padding in String.padLeft/padRight. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Type typo. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/lib/js_string.dart
diff --git a/sdk/lib/_internal/lib/js_string.dart b/sdk/lib/_internal/lib/js_string.dart
index 6482d8bf0dc0c08f8bab289be3b2604cfd66506f..4055647e46b5407a8260eac22f03a0469332feb3 100644
--- a/sdk/lib/_internal/lib/js_string.dart
+++ b/sdk/lib/_internal/lib/js_string.dart
@@ -185,7 +185,7 @@ class JSString extends Interceptor implements String, JSIndexable {
// Start by doing JS trim. Then check if it leaves a NEL or BOM at
// either end of the string.
- String result = JS("String", "#.trim()", this);
+ String result = JS('String', '#.trim()', this);
if (result.length == 0) return result;
int firstCode = result.codeUnitAt(0);
@@ -226,34 +226,34 @@ class JSString extends Interceptor implements String, JSIndexable {
return JS('String', r'#.substring(#, #)', result, startIndex, endIndex);
}
- String repeat(int times, [String separator = ""]) {
- if (times < 0) throw new RangeError.value(times);
- if (times == 0) return "";
- if (separator.isEmpty) {
- return JS('String', "new Array(# + 1).join(#)", times, this);
- } else {
- var list = new JSArray.growable(times);
- for (int i = 0; i < times; i++) list[i] = this;
- return JS('String', "#.join(#)", list, separator);
+ String operator*(int times) {
+ if (0 >= times) return ''; // Unnecessary but hoists argument type check.
+ if (times == 1 || this.length == 0) return this;
+ if (times != JS('JSUInt32', '# >>> 0', times)) {
+ // times >= 2^32. We can't create a string that big.
+ throw const OutOfMemoryError();
+ }
+ var result = '';
+ var s = this;
+ while (true) {
+ if (times & 1 == 1) result = s + result;
+ times = JS('JSUInt31', '# >>> 1', times);
+ if (times == 0) break;
+ s += s;
}
+ return result;
}
- String padLeft(int newLength, String padding) {
- if (padding.length != 1) throw new ArgumentError(padding);
- int delta = newLength - this.length;
+ String padLeft(int width, [String padding = ' ']) {
+ int delta = width - this.length;
if (delta <= 0) return this;
- var list = new JSArray.growable(delta + 1);
- list[delta] = this;
- return JS("String", "#.join(#)", list, padding);
+ return padding * delta + this;
}
- String padRight(int newLength, String padding) {
- if (padding.length != 1) throw new ArgumentError(padding);
- int delta = newLength - this.length;
+ String padRight(int width, [String padding = ' ']) {
+ int delta = width - this.length;
if (delta <= 0) return this;
- var list = new JSArray.growable(delta + 1);
- list[0] = this;
- return JS("String", "#.join(#)", list, padding);
+ return this + padding * delta;
}
List<int> get codeUnits => new _CodeUnits(this);

Powered by Google App Engine
This is Rietveld 408576698