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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 int last = len - 1; 290 int last = len - 1;
291 for (; last >= first; last--) { 291 for (; last >= first; last--) {
292 if (!_isWhitespace(this.codeUnitAt(last))) { 292 if (!_isWhitespace(this.codeUnitAt(last))) {
293 break; 293 break;
294 } 294 }
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 {
301 return _substringUnchecked(first, last + 1);
302 } 300 }
301 return _substringUnchecked(first, last + 1);
302 }
303
304 String trimLeft() {
305 final len = this.length;
306 int first = 0;
307 for (; first < len; first++) {
308 if (!_isWhitespace(this.codeUnitAt(first))) {
309 break;
310 }
311 }
312 if (len == first) {
313 // String contains only whitespaces.
314 return "";
315 }
316 if (first == 0) {
317 // Returns this string if it does not have leading or trailing
Søren Gjesse 2014/03/10 16:50:03 if -> as
Lasse Reichstein Nielsen 2014/03/11 10:36:29 Rewritten.
318 // whitespaces.
319 return this;
320 }
321 return _substringUnchecked(first, len);
322 }
323
324 String trimRight() {
325 final len = this.length;
326 int last = len - 1;
327 for (; last >= 0; last--) {
328 if (!_isWhitespace(this.codeUnitAt(last))) {
329 break;
330 }
331 }
332 if (last == -1) {
333 // String contains only whitespaces.
334 return "";
335 }
336 if (last == (len - 1)) {
337 // Returns this string if it does not have trailing
Søren Gjesse 2014/03/10 16:50:03 ditto.
Lasse Reichstein Nielsen 2014/03/11 10:36:29 Done.
338 // whitespaces.
339 return this;
340 }
341 return _substringUnchecked(0, last + 1);
303 } 342 }
304 343
305 String operator*(int times) { 344 String operator*(int times) {
306 if (times <= 0) return ""; 345 if (times <= 0) return "";
307 if (times == 1) return this; 346 if (times == 1) return this;
308 StringBuffer buffer = new StringBuffer(this); 347 StringBuffer buffer = new StringBuffer(this);
309 for (int i = 1; i < times; i++) { 348 for (int i = 1; i < times; i++) {
310 buffer.write(this); 349 buffer.write(this);
311 } 350 }
312 return buffer.toString(); 351 return buffer.toString();
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 class _CodeUnits extends Object with ListMixin<int>, 882 class _CodeUnits extends Object with ListMixin<int>,
844 UnmodifiableListMixin<int> { 883 UnmodifiableListMixin<int> {
845 /** The string that this is the code units of. */ 884 /** The string that this is the code units of. */
846 String _string; 885 String _string;
847 886
848 _CodeUnits(this._string); 887 _CodeUnits(this._string);
849 888
850 int get length => _string.length; 889 int get length => _string.length;
851 int operator[](int i) => _string.codeUnitAt(i); 890 int operator[](int i) => _string.codeUnitAt(i);
852 } 891 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | sdk/lib/_internal/lib/js_string.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698