| 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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 throw new ArgumentError("${pattern} is not a Pattern"); | 247 throw new ArgumentError("${pattern} is not a Pattern"); |
| 248 } | 248 } |
| 249 if (replacement is! String) { | 249 if (replacement is! String) { |
| 250 throw new ArgumentError("${replacement} is not a String"); | 250 throw new ArgumentError("${replacement} is not a String"); |
| 251 } | 251 } |
| 252 StringBuffer buffer = new StringBuffer(); | 252 StringBuffer buffer = new StringBuffer(); |
| 253 int startIndex = 0; | 253 int startIndex = 0; |
| 254 Iterator iterator = pattern.allMatches(this).iterator; | 254 Iterator iterator = pattern.allMatches(this).iterator; |
| 255 if (iterator.moveNext()) { | 255 if (iterator.moveNext()) { |
| 256 Match match = iterator.current; | 256 Match match = iterator.current; |
| 257 buffer..add(this.substring(startIndex, match.start)) | 257 buffer..write(this.substring(startIndex, match.start)) |
| 258 ..add(replacement); | 258 ..write(replacement); |
| 259 startIndex = match.end; | 259 startIndex = match.end; |
| 260 } | 260 } |
| 261 return (buffer..add(this.substring(startIndex))).toString(); | 261 return (buffer..write(this.substring(startIndex))).toString(); |
| 262 } | 262 } |
| 263 | 263 |
| 264 String replaceAll(Pattern pattern, String replacement) { | 264 String replaceAll(Pattern pattern, String replacement) { |
| 265 if (pattern is! Pattern) { | 265 if (pattern is! Pattern) { |
| 266 throw new ArgumentError("${pattern} is not a Pattern"); | 266 throw new ArgumentError("${pattern} is not a Pattern"); |
| 267 } | 267 } |
| 268 if (replacement is! String) { | 268 if (replacement is! String) { |
| 269 throw new ArgumentError( | 269 throw new ArgumentError( |
| 270 "${replacement} is not a String or Match->String function"); | 270 "${replacement} is not a String or Match->String function"); |
| 271 } | 271 } |
| 272 StringBuffer buffer = new StringBuffer(); | 272 StringBuffer buffer = new StringBuffer(); |
| 273 int startIndex = 0; | 273 int startIndex = 0; |
| 274 for (Match match in pattern.allMatches(this)) { | 274 for (Match match in pattern.allMatches(this)) { |
| 275 buffer..add(this.substring(startIndex, match.start)) | 275 buffer..write(this.substring(startIndex, match.start)) |
| 276 ..add(replacement); | 276 ..write(replacement); |
| 277 startIndex = match.end; | 277 startIndex = match.end; |
| 278 } | 278 } |
| 279 return (buffer..add(this.substring(startIndex))).toString(); | 279 return (buffer..write(this.substring(startIndex))).toString(); |
| 280 } | 280 } |
| 281 | 281 |
| 282 String replaceAllMapped(Pattern pattern, String replace(Match match)) { | 282 String replaceAllMapped(Pattern pattern, String replace(Match match)) { |
| 283 return splitMapJoin(pattern, onMatch: replace); | 283 return splitMapJoin(pattern, onMatch: replace); |
| 284 } | 284 } |
| 285 | 285 |
| 286 static String _matchString(Match match) => match[0]; | 286 static String _matchString(Match match) => match[0]; |
| 287 static String _stringIdentity(String string) => string; | 287 static String _stringIdentity(String string) => string; |
| 288 | 288 |
| 289 String _splitMapJoinEmptyString(String onMatch(Match match), | 289 String _splitMapJoinEmptyString(String onMatch(Match match), |
| 290 String onNonMatch(String nonMatch)) { | 290 String onNonMatch(String nonMatch)) { |
| 291 // Pattern is the empty string. | 291 // Pattern is the empty string. |
| 292 StringBuffer buffer = new StringBuffer(); | 292 StringBuffer buffer = new StringBuffer(); |
| 293 int length = this.length; | 293 int length = this.length; |
| 294 int i = 0; | 294 int i = 0; |
| 295 buffer.add(onNonMatch("")); | 295 buffer.write(onNonMatch("")); |
| 296 while (i < length) { | 296 while (i < length) { |
| 297 buffer.add(onMatch(new _StringMatch(i, this, ""))); | 297 buffer.write(onMatch(new _StringMatch(i, this, ""))); |
| 298 // Special case to avoid splitting a surrogate pair. | 298 // Special case to avoid splitting a surrogate pair. |
| 299 int code = this.codeUnitAt(i); | 299 int code = this.codeUnitAt(i); |
| 300 if ((code & ~0x3FF) == 0xD800 && length > i + 1) { | 300 if ((code & ~0x3FF) == 0xD800 && length > i + 1) { |
| 301 // Leading surrogate; | 301 // Leading surrogate; |
| 302 code = this.codeUnitAt(i + 1); | 302 code = this.codeUnitAt(i + 1); |
| 303 if ((code & ~0x3FF) == 0xDC00) { | 303 if ((code & ~0x3FF) == 0xDC00) { |
| 304 // Matching trailing surrogate. | 304 // Matching trailing surrogate. |
| 305 buffer.add(onNonMatch(this.substring(i, i + 2))); | 305 buffer.write(onNonMatch(this.substring(i, i + 2))); |
| 306 i += 2; | 306 i += 2; |
| 307 continue; | 307 continue; |
| 308 } | 308 } |
| 309 } | 309 } |
| 310 buffer.add(onNonMatch(this[i])); | 310 buffer.write(onNonMatch(this[i])); |
| 311 i++; | 311 i++; |
| 312 } | 312 } |
| 313 buffer.add(onMatch(new _StringMatch(i, this, ""))); | 313 buffer.write(onMatch(new _StringMatch(i, this, ""))); |
| 314 buffer.add(onNonMatch("")); | 314 buffer.write(onNonMatch("")); |
| 315 return buffer.toString(); | 315 return buffer.toString(); |
| 316 } | 316 } |
| 317 | 317 |
| 318 String splitMapJoin(Pattern pattern, | 318 String splitMapJoin(Pattern pattern, |
| 319 {String onMatch(Match match), | 319 {String onMatch(Match match), |
| 320 String onNonMatch(String nonMatch)}) { | 320 String onNonMatch(String nonMatch)}) { |
| 321 if (pattern is! Pattern) { | 321 if (pattern is! Pattern) { |
| 322 throw new ArgumentError("${pattern} is not a Pattern"); | 322 throw new ArgumentError("${pattern} is not a Pattern"); |
| 323 } | 323 } |
| 324 if (onMatch == null) onMatch = _matchString; | 324 if (onMatch == null) onMatch = _matchString; |
| 325 if (onNonMatch == null) onNonMatch = _stringIdentity; | 325 if (onNonMatch == null) onNonMatch = _stringIdentity; |
| 326 if (pattern is String) { | 326 if (pattern is String) { |
| 327 String stringPattern = pattern; | 327 String stringPattern = pattern; |
| 328 if (stringPattern.isEmpty) { | 328 if (stringPattern.isEmpty) { |
| 329 return _splitMapJoinEmptyString(onMatch, onNonMatch); | 329 return _splitMapJoinEmptyString(onMatch, onNonMatch); |
| 330 } | 330 } |
| 331 } | 331 } |
| 332 StringBuffer buffer = new StringBuffer(); | 332 StringBuffer buffer = new StringBuffer(); |
| 333 int startIndex = 0; | 333 int startIndex = 0; |
| 334 for (Match match in pattern.allMatches(this)) { | 334 for (Match match in pattern.allMatches(this)) { |
| 335 buffer.add(onNonMatch(this.substring(startIndex, match.start))); | 335 buffer.write(onNonMatch(this.substring(startIndex, match.start))); |
| 336 buffer.add(onMatch(match).toString()); | 336 buffer.write(onMatch(match).toString()); |
| 337 startIndex = match.end; | 337 startIndex = match.end; |
| 338 } | 338 } |
| 339 buffer.add(onNonMatch(this.substring(startIndex))); | 339 buffer.write(onNonMatch(this.substring(startIndex))); |
| 340 return buffer.toString(); | 340 return buffer.toString(); |
| 341 } | 341 } |
| 342 | 342 |
| 343 | 343 |
| 344 /** | 344 /** |
| 345 * Convert all objects in [values] to strings and concat them | 345 * Convert all objects in [values] to strings and concat them |
| 346 * into a result string. | 346 * into a result string. |
| 347 */ | 347 */ |
| 348 static String _interpolate(List values) { | 348 static String _interpolate(List values) { |
| 349 int numValues = values.length; | 349 int numValues = values.length; |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 for (int g in groups) { | 604 for (int g in groups) { |
| 605 result.add(group(g)); | 605 result.add(group(g)); |
| 606 } | 606 } |
| 607 return result; | 607 return result; |
| 608 } | 608 } |
| 609 | 609 |
| 610 final int start; | 610 final int start; |
| 611 final String str; | 611 final String str; |
| 612 final String pattern; | 612 final String pattern; |
| 613 } | 613 } |
| OLD | NEW |