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

Unified Diff: runtime/lib/string_patch.dart

Issue 190853009: Add string.trimLeft/trimRight. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove left-over debugging aid. Created 6 years, 9 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
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/string_patch.dart
diff --git a/runtime/lib/string_patch.dart b/runtime/lib/string_patch.dart
index 1bececf5b3965a666d8fd2f99cd2e6c6884e39bb..667a08772516cf18f3c5b8f8886ceddcd538fa33 100644
--- a/runtime/lib/string_patch.dart
+++ b/runtime/lib/string_patch.dart
@@ -294,12 +294,50 @@ class _StringBase {
}
}
if ((first == 0) && (last == (len - 1))) {
- // Returns this string if it does not have leading or trailing
+ // Returns this string since it does not have leading or trailing
// whitespaces.
return this;
- } else {
- return _substringUnchecked(first, last + 1);
}
+ return _substringUnchecked(first, last + 1);
+ }
+
+ String trimLeft() {
+ final len = this.length;
+ int first = 0;
+ for (; first < len; first++) {
+ if (!_isWhitespace(this.codeUnitAt(first))) {
+ break;
+ }
+ }
+ if (len == first) {
+ // String contains only whitespaces.
+ return "";
+ }
+ if (first == 0) {
+ // Returns this string since it does not have leading or trailing
+ // whitespaces.
+ return this;
+ }
+ return _substringUnchecked(first, len);
+ }
+
+ String trimRight() {
+ final len = this.length;
+ int last = len - 1;
+ for (; last >= 0; last--) {
+ if (!_isWhitespace(this.codeUnitAt(last))) {
+ break;
+ }
+ }
+ if (last == -1) {
+ // String contains only whitespaces.
+ return "";
+ }
+ if (last == (len - 1)) {
+ // Returns this string since it does not have trailing whitespaces.
+ return this;
+ }
+ return _substringUnchecked(0, last + 1);
}
String operator*(int times) {
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698