OLD | NEW |
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 /** | 5 /** |
6 * [_StringBase] contains common methods used by concrete String | 6 * [_StringBase] contains common methods used by concrete String |
7 * implementations, e.g., _OneByteString. | 7 * implementations, e.g., _OneByteString. |
8 */ | 8 */ |
9 class _StringBase { | 9 class _StringBase { |
10 | 10 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 throw new ArgumentError("${pattern} is not a Pattern"); | 197 throw new ArgumentError("${pattern} is not a Pattern"); |
198 } | 198 } |
199 if (replacement is! String) { | 199 if (replacement is! String) { |
200 throw new ArgumentError("${replacement} is not a String"); | 200 throw new ArgumentError("${replacement} is not a String"); |
201 } | 201 } |
202 StringBuffer buffer = new StringBuffer(); | 202 StringBuffer buffer = new StringBuffer(); |
203 int startIndex = 0; | 203 int startIndex = 0; |
204 Iterator iterator = pattern.allMatches(this).iterator(); | 204 Iterator iterator = pattern.allMatches(this).iterator(); |
205 if (iterator.hasNext) { | 205 if (iterator.hasNext) { |
206 Match match = iterator.next(); | 206 Match match = iterator.next(); |
207 buffer.add(this.substring(startIndex, match.start())).add(replacement); | 207 buffer.add(this.substring(startIndex, match.start)).add(replacement); |
208 startIndex = match.end(); | 208 startIndex = match.end; |
209 } | 209 } |
210 return buffer.add(this.substring(startIndex)).toString(); | 210 return buffer.add(this.substring(startIndex)).toString(); |
211 } | 211 } |
212 | 212 |
213 String replaceAll(Pattern pattern, String replacement) { | 213 String replaceAll(Pattern pattern, String replacement) { |
214 if (pattern is! Pattern) { | 214 if (pattern is! Pattern) { |
215 throw new ArgumentError("${pattern} is not a Pattern"); | 215 throw new ArgumentError("${pattern} is not a Pattern"); |
216 } | 216 } |
217 if (replacement is! String) { | 217 if (replacement is! String) { |
218 throw new ArgumentError("${replacement} is not a String"); | 218 throw new ArgumentError("${replacement} is not a String"); |
219 } | 219 } |
220 StringBuffer buffer = new StringBuffer(); | 220 StringBuffer buffer = new StringBuffer(); |
221 int startIndex = 0; | 221 int startIndex = 0; |
222 for (Match match in pattern.allMatches(this)) { | 222 for (Match match in pattern.allMatches(this)) { |
223 buffer.add(this.substring(startIndex, match.start())).add(replacement); | 223 buffer.add(this.substring(startIndex, match.start)).add(replacement); |
224 startIndex = match.end(); | 224 startIndex = match.end; |
225 } | 225 } |
226 return buffer.add(this.substring(startIndex)).toString(); | 226 return buffer.add(this.substring(startIndex)).toString(); |
227 } | 227 } |
228 | 228 |
229 /** | 229 /** |
230 * Convert all objects in [values] to strings and concat them | 230 * Convert all objects in [values] to strings and concat them |
231 * into a result string. | 231 * into a result string. |
232 */ | 232 */ |
233 static String _interpolate(List values) { | 233 static String _interpolate(List values) { |
234 int numValues = values.length; | 234 int numValues = values.length; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 } | 271 } |
272 List<String> result = new List<String>(); | 272 List<String> result = new List<String>(); |
273 int startIndex = 0; | 273 int startIndex = 0; |
274 int previousIndex = 0; | 274 int previousIndex = 0; |
275 while (true) { | 275 while (true) { |
276 if (startIndex == length || !iterator.hasNext) { | 276 if (startIndex == length || !iterator.hasNext) { |
277 result.add(this.substring(previousIndex, length)); | 277 result.add(this.substring(previousIndex, length)); |
278 break; | 278 break; |
279 } | 279 } |
280 Match match = iterator.next(); | 280 Match match = iterator.next(); |
281 if (match.start() == length) { | 281 if (match.start == length) { |
282 result.add(this.substring(previousIndex, length)); | 282 result.add(this.substring(previousIndex, length)); |
283 break; | 283 break; |
284 } | 284 } |
285 int endIndex = match.end(); | 285 int endIndex = match.end; |
286 if (startIndex == endIndex && endIndex == previousIndex) { | 286 if (startIndex == endIndex && endIndex == previousIndex) { |
287 ++startIndex; // empty match, advance and restart | 287 ++startIndex; // empty match, advance and restart |
288 continue; | 288 continue; |
289 } | 289 } |
290 result.add(this.substring(previousIndex, match.start())); | 290 result.add(this.substring(previousIndex, match.start)); |
291 startIndex = previousIndex = endIndex; | 291 startIndex = previousIndex = endIndex; |
292 } | 292 } |
293 return result; | 293 return result; |
294 } | 294 } |
295 | 295 |
296 List<String> splitChars() { | 296 List<String> splitChars() { |
297 int len = this.length; | 297 int len = this.length; |
298 final result = new List<String>(len); | 298 final result = new List<String>(len); |
299 for (int i = 0; i < len; i++) { | 299 for (int i = 0; i < len; i++) { |
300 result[i] = this[i]; | 300 result[i] = this[i]; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 // whitespaces. Add checking for multi-byte whitespace codepoints. | 451 // whitespaces. Add checking for multi-byte whitespace codepoints. |
452 bool _isWhitespace(int codePoint) { | 452 bool _isWhitespace(int codePoint) { |
453 return | 453 return |
454 (codePoint === 32) || // Space. | 454 (codePoint === 32) || // Space. |
455 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 455 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
456 } | 456 } |
457 } | 457 } |
458 | 458 |
459 | 459 |
460 class _StringMatch implements Match { | 460 class _StringMatch implements Match { |
461 const _StringMatch(int this._start, | 461 const _StringMatch(int this.start, |
462 String this.str, | 462 String this.str, |
463 String this.pattern); | 463 String this.pattern); |
464 | 464 |
465 int start() => _start; | 465 int get end => start + pattern.length; |
466 int end() => _start + pattern.length; | |
467 String operator[](int g) => group(g); | 466 String operator[](int g) => group(g); |
468 int groupCount() => 0; | 467 int get groupCount => 0; |
469 | 468 |
470 String group(int group) { | 469 String group(int group) { |
471 if (group != 0) { | 470 if (group != 0) { |
472 throw new IndexOutOfRangeException(group); | 471 throw new IndexOutOfRangeException(group); |
473 } | 472 } |
474 return pattern; | 473 return pattern; |
475 } | 474 } |
476 | 475 |
477 List<String> groups(List<int> groups) { | 476 List<String> groups(List<int> groups) { |
478 List<String> result = new List<String>(); | 477 List<String> result = new List<String>(); |
479 for (int g in groups) { | 478 for (int g in groups) { |
480 result.add(group(g)); | 479 result.add(group(g)); |
481 } | 480 } |
482 return result; | 481 return result; |
483 } | 482 } |
484 | 483 |
485 final int _start; | 484 final int start; |
486 final String str; | 485 final String str; |
487 final String pattern; | 486 final String pattern; |
488 } | 487 } |
OLD | NEW |