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

Unified Diff: runtime/lib/string_patch.dart

Issue 462463003: Add optional startIndex to String.replaceFirst (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Little fixes Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/string_patch.dart
diff --git a/runtime/lib/string_patch.dart b/runtime/lib/string_patch.dart
index f750fc31f153281a011307d53d6e7ee6481ed193..3ac78a8fd9404faa5724cd771666eb61053110d5 100644
--- a/runtime/lib/string_patch.dart
+++ b/runtime/lib/string_patch.dart
@@ -414,23 +414,29 @@ class _StringBase {
return pattern.allMatches(this.substring(startIndex)).isNotEmpty;
}
- String replaceFirst(Pattern pattern, String replacement) {
+ String replaceFirst(Pattern pattern,
+ String replacement,
+ [int startIndex = 0]) {
if (pattern is! Pattern) {
throw new ArgumentError("${pattern} is not a Pattern");
}
if (replacement is! String) {
throw new ArgumentError("${replacement} is not a String");
}
- StringBuffer buffer = new StringBuffer();
- int startIndex = 0;
- Iterator iterator = pattern.allMatches(this).iterator;
- if (iterator.moveNext()) {
- Match match = iterator.current;
- buffer..write(this.substring(startIndex, match.start))
- ..write(replacement);
- startIndex = match.end;
+ if (startIndex is! int) {
+ throw new ArgumentError("${startIndex} is not an int");
}
- return (buffer..write(this.substring(startIndex))).toString();
+ if ((startIndex < 0) || (startIndex > this.length)) {
+ throw new RangeError.range(startIndex, 0, this.length);
+ }
+ Iterator iterator =
+ startIndex == 0 ? pattern.allMatches(this).iterator
+ : pattern.allMatches(this, startIndex).iterator;
+ if (!iterator.moveNext()) return this;
+ Match match = iterator.current;
+ return "${this.substring(0, match.start)}"
+ "$replacement"
+ "${this.substring(match.end)}";
}
String replaceAll(Pattern pattern, String replacement) {
« 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