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

Side by Side Diff: sdk/lib/_internal/lib/js_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 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 part of _interceptors; 5 part of _interceptors;
6 6
7 /** 7 /**
8 * The interceptor class for [String]. The compiler recognizes this 8 * The interceptor class for [String]. The compiler recognizes this
9 * class as an interceptor, and changes references to [:this:] to 9 * class as an interceptor, and changes references to [:this:] to
10 * actually use the receiver of the method, which is generated as an extra 10 * actually use the receiver of the method, which is generated as an extra
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 default: 173 default:
174 return false; 174 return false;
175 } 175 }
176 } 176 }
177 177
178 // Dart2js can't use JavaScript trim, because JavaScript does not trim 178 // Dart2js can't use JavaScript trim, because JavaScript does not trim
179 // the NEXT LINE character (0x85) and BOMs (0xFEFF). 179 // the NEXT LINE character (0x85) and BOMs (0xFEFF).
180 String trim() { 180 String trim() {
181 const int CARRIAGE_RETURN = 0x0D; 181 const int CARRIAGE_RETURN = 0x0D;
182 const int SPACE = 0x20; 182 const int SPACE = 0x20;
183 const int NEL = 0x85;
184 const int BOM = 0xFEFF;
183 185
186 // Start by doing JS trim. Then check if it leaves a
floitsch 2014/02/19 10:20:36 Unfinished sentence.
Søren Gjesse 2014/02/19 10:44:10 Leaves a...?
Lasse Reichstein Nielsen 2014/02/19 11:43:16 Done.
187 Strint result = JS("String", "#.trim()", this);
Søren Gjesse 2014/02/19 10:44:10 What type is Strint?
Lasse Reichstein Nielsen 2014/02/19 11:43:16 Done.
188
189 if (result.length == 0) return result;
190 int firstCode = result.codeUnitAt(0);
184 int startIndex = 0; 191 int startIndex = 0;
185 while (startIndex < this.length) { 192 if (firstCode == NEL || firstCode == BOM) {
186 int codeUnit = this.codeUnitAt(startIndex); 193 startIndex++;
187 if (codeUnit == SPACE || 194 while (startIndex < result.length) {
188 codeUnit == CARRIAGE_RETURN || 195 int codeUnit = result.codeUnitAt(startIndex);
189 _isWhitespace(codeUnit)) { 196 if (codeUnit == SPACE ||
190 startIndex++; 197 codeUnit == CARRIAGE_RETURN ||
191 } else { 198 _isWhitespace(codeUnit)) {
192 break; 199 startIndex++;
200 } else {
201 break;
202 }
203 }
204 if (startIndex == result.length) return "";
205 }
206
207 int endIndex = result.length;
208 // We know that there is at least one character that is non-whitespace.
209 // Therefore we don't need to verify that endIndex > startIndex.
210 int lastCode = result.codeUnitAt(endIndex - 1);
211 if (lastCode == NEL || lastCode == BOM) {
212 endIndex--;
213 while (true) {
214 int codeUnit = result.codeUnitAt(endIndex - 1);
215 if (codeUnit == SPACE ||
216 codeUnit == CARRIAGE_RETURN ||
217 _isWhitespace(codeUnit)) {
218 endIndex--;
219 } else {
220 break;
221 }
193 } 222 }
194 } 223 }
195 if (startIndex == this.length) return ""; 224 if (startIndex == 0 && endIndex == result.length) return result;
225 return JS('String', r'#.substring(#, #)', result, startIndex, endIndex);
226 }
196 227
197 int endIndex = this.length; 228 String repeat(int times, [String separator = ""]) {
198 // We know that there is at least one character that is non-whitespace. 229 if (times < 0) throw new RangeError.value(times);
199 // Therefore we don't need to verify that endIndex > startIndex. 230 if (times == 0) return "";
200 while (true) { 231 if (separator.isEmpty) {
201 int codeUnit = this.codeUnitAt(endIndex - 1); 232 return JS('String', "new Array(# + 1).join(#)", times, this);
202 if (codeUnit == SPACE || 233 } else {
203 codeUnit == CARRIAGE_RETURN || 234 var list = new JSArray.growable(times);
204 _isWhitespace(codeUnit)) { 235 for (int i = 0; i < times; i++) list[i] = this;
205 endIndex--; 236 return JS('String', "#.join(#)", list, separator);
206 } else {
207 break;
208 }
209 } 237 }
210 if (startIndex == 0 && endIndex == this.length) return this; 238 }
211 return JS('String', r'#.substring(#, #)', this, startIndex, endIndex); 239
240 String padLeft(int newSize, String padding, { int size }) {
241 if (size == null) {
242 size = this.length;
243 } else if (size < 0) {
244 throw new RangeError.value(size);
245 }
246 int delta = newSize - size;
247 if (delta <= 0) return this;
248 var list = new JSArray.growable(delta + 1);
249 list[delta] = this;
250 return JS("String", "#.join(#)", list, padding);
251 }
252
253 String padRight(int newSize, String padding, { int size }) {
254 if (size == null) {
255 size = this.length;
256 } else if (size < 0) {
257 throw new RangeError.value(size);
258 }
259 int delta = newSize - size;
260 if (delta <= 0) return this;
261 var list = new JSArray.growable(delta + 1);
262 list[0] = this;
263 return JS("String", "#.join(#)", list, padding);
212 } 264 }
213 265
214 List<int> get codeUnits => new _CodeUnits(this); 266 List<int> get codeUnits => new _CodeUnits(this);
215 267
216 Runes get runes => new Runes(this); 268 Runes get runes => new Runes(this);
217 269
218 int indexOf(Pattern pattern, [int start = 0]) { 270 int indexOf(Pattern pattern, [int start = 0]) {
219 checkNull(pattern); 271 checkNull(pattern);
220 if (start is! int) throw new ArgumentError(start); 272 if (start is! int) throw new ArgumentError(start);
221 if (start < 0 || start > this.length) { 273 if (start < 0 || start > this.length) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 */ 366 */
315 class _CodeUnits extends UnmodifiableListBase<int> { 367 class _CodeUnits extends UnmodifiableListBase<int> {
316 /** The string that this is the code units of. */ 368 /** The string that this is the code units of. */
317 String _string; 369 String _string;
318 370
319 _CodeUnits(this._string); 371 _CodeUnits(this._string);
320 372
321 int get length => _string.length; 373 int get length => _string.length;
322 int operator[](int i) => _string.codeUnitAt(i); 374 int operator[](int i) => _string.codeUnitAt(i);
323 } 375 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698