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

Unified Diff: sdk/lib/core/string.dart

Issue 172153002: Add String.repeat, String.padLeft, String.padRight. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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/core/string.dart
diff --git a/sdk/lib/core/string.dart b/sdk/lib/core/string.dart
index 799a2a96837e06652a54cb6cfb6b243d1ebea063..12f6790dc50ffad8ff712cf5b3c0a252f892a6b1 100644
--- a/sdk/lib/core/string.dart
+++ b/sdk/lib/core/string.dart
@@ -326,6 +326,78 @@ abstract class String implements Comparable<String>, Pattern {
String trim();
/**
+ * Creates a new string that repeats this string a number of times.
+ *
+ * If [separator] is provided, it is insert between the copies
floitsch 2014/02/19 10:20:36 it is inserted
Lasse Reichstein Nielsen 2014/02/19 11:43:16 Done.
+ * of this string.
+ *
+ * The [times] number must be non-negative.
+ */
+ String repeat(int times, [String separator = ""]);
floitsch 2014/02/19 10:20:36 Do we really need this one? The only good use-cas
Lasse Reichstein Nielsen 2014/02/19 11:43:16 If we have padLeft, you can do str.repeat(n) by do
+
+ /**
+ * Pads this string on the left if it is shorther than [newSize].
+ *
+ * For each position the length of this string is shorter than [newSize],
+ * a copy of [padding] is added on the left.
+ * If the length of this string is already greater than [newSize],
+ * this string is returned unmodified.
+ *
+ * If [padding] has length greater than 1,
+ * the resulting string's length will be greater than [newSize].
+ * This can be used for cases where the logical length of a string
+ * is not its length in code points.
+ * Example:
+ *
+ * "foo".padLeft(10, "&nbsp;"); // String representing single space.
+ * "foo".padLeft(10, "\u{100002}"); // Surrogate pair character.
+ *
+ * If [size] is provided, it is used as instead of `this.length`
+ * as the size of this string.
+ * If the string contains, for example, surrogate pairs,
+ * and it is intended to be treated as charaters, not code points,
+ * [size] can be used to provide the logical length.
+ * Example:
+ *
+ * "D&amp;D".padLeft(10, "&nbsp;", size: 3);
+ *
+ * The [size] number must be non-negative.
+ * If [newSize] is negative, no padding will happen.
+ */
+ String padLeft(int newSize, String padding, { int size });
floitsch 2014/02/19 10:20:36 I'm not convinced that dealing with surrogate pair
Søren Gjesse 2014/02/19 10:44:10 I find the option of padding with size larger than
Lasse Reichstein Nielsen 2014/02/19 11:43:16 This is not (just) dealing with surrogate pairs. I
+
+ /**
+ * Pads this string on the right if it is shorther than [newSize].
+ *
+ * For each position the length of this string is shorter than [newSize],
+ * a copy of [padding] is added on the right.
+ * If the length of this string is already greater than [newSize],
+ * this string is returned unmodified.
+ *
+ * If [padding] has length greater than 1,
+ * the resulting string's length will be greater than [newSize].
+ * This can be used for cases where the logical length of a string
+ * is not its length in code points.
+ * Example:
+ *
+ * "foo".padRight(10, "&nbsp;"); // String representing single space.
+ * "foo".padRight(10, "\u{100002}"); // Surrogate pair character.
+ *
+ * If [size] is provided, it is used as instead of `this.length`
+ * as the size of this string.
+ * If the string contains, for example, surrogate pairs,
+ * and it is intended to be treated as charaters, not code points,
+ * [size] can be used to provide the logical length.
+ * Example:
+ *
+ * "D&amp;D".padLeft(10, "&nbsp;", size: 3);
+ *
+ * The [size] number must be non-negative.
+ * If [newSize] is negative, no padding will happen.
+ */
+ String padRight(int newSize, String padding, {int size});
+
+ /**
* Returns true if this string contains a match of [other]:
*
* var string = 'Dart strings';

Powered by Google App Engine
This is Rietveld 408576698