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

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: 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 // String contains only whitespaces. 287 // String contains only whitespaces.
288 return ""; 288 return "";
289 } 289 }
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 since 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 since it does not have leading or trailing
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 since it does not have trailing whitespaces.
338 return this;
339 }
340 return _substringUnchecked(0, last + 1);
303 } 341 }
304 342
305 String operator*(int times) { 343 String operator*(int times) {
306 if (times <= 0) return ""; 344 if (times <= 0) return "";
307 if (times == 1) return this; 345 if (times == 1) return this;
308 StringBuffer buffer = new StringBuffer(this); 346 StringBuffer buffer = new StringBuffer(this);
309 for (int i = 1; i < times; i++) { 347 for (int i = 1; i < times; i++) {
310 buffer.write(this); 348 buffer.write(this);
311 } 349 }
312 return buffer.toString(); 350 return buffer.toString();
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 class _CodeUnits extends Object with ListMixin<int>, 881 class _CodeUnits extends Object with ListMixin<int>,
844 UnmodifiableListMixin<int> { 882 UnmodifiableListMixin<int> {
845 /** The string that this is the code units of. */ 883 /** The string that this is the code units of. */
846 String _string; 884 String _string;
847 885
848 _CodeUnits(this._string); 886 _CodeUnits(this._string);
849 887
850 int get length => _string.length; 888 int get length => _string.length;
851 int operator[](int i) => _string.codeUnitAt(i); 889 int operator[](int i) => _string.codeUnitAt(i);
852 } 890 }
OLDNEW
« 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