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

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: 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') | sdk/lib/_internal/lib/string_helper.dart » ('J')
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 11f549338be8cc80828f2aa37059c0845315c51a..1835f789caec343f124826454900b86ef7043320 100644
--- a/runtime/lib/string_patch.dart
+++ b/runtime/lib/string_patch.dart
@@ -414,23 +414,27 @@ class _StringBase {
return pattern.allMatches(this.substring(startIndex)).isNotEmpty;
}
- String replaceFirst(Pattern pattern, String replacement) {
+ String replaceFirst(Pattern pattern, String replacement, [int startIndex = 0]) {
Lasse Reichstein Nielsen 2014/08/13 08:35:09 long line.
srawlins 2014/08/14 00:42:44 Done.
if (pattern is! Pattern) {
throw new ArgumentError("${pattern} is not a Pattern");
}
if (replacement is! String) {
throw new ArgumentError("${replacement} is not a String");
}
+ if ((startIndex < 0) || (startIndex > this.length)) {
+ throw new RangeError.value(startIndex);
Lasse Reichstein Nielsen 2014/08/13 08:35:09 You can use throw new RangeError.range(startInde
srawlins 2014/08/14 00:42:44 Done.
+ }
StringBuffer buffer = new StringBuffer();
Bill Hesse 2014/08/11 07:47:47 This could be made more efficient by moving the St
srawlins 2014/08/14 00:42:44 Done.
- int startIndex = 0;
- Iterator iterator = pattern.allMatches(this).iterator;
+ buffer.write(this.substring(0, startIndex));
+ String remaining = this.substring(startIndex);
Lasse Reichstein Nielsen 2014/08/11 08:13:32 Creating a substring, and then using allMatches, i
Lasse Reichstein Nielsen 2014/08/11 09:37:11 This implementation is actually wrong: A "^" must
Lasse Reichstein Nielsen 2014/08/13 08:35:09 And now we DO have start-index on allMatches, so t
srawlins 2014/08/14 00:42:44 Done. Thanks very much for this cleanup!
srawlins 2014/08/14 00:42:44 Good catch. This implementation now depends on pas
srawlins 2014/08/14 00:42:44 Thanks for the patch to Pattern.allMatches! This i
+ Iterator iterator = pattern.allMatches(remaining).iterator;
if (iterator.moveNext()) {
Match match = iterator.current;
- buffer..write(this.substring(startIndex, match.start))
+ buffer..write(remaining.substring(0, match.start))
..write(replacement);
startIndex = match.end;
}
- return (buffer..write(this.substring(startIndex))).toString();
+ return (buffer..write(remaining.substring(startIndex))).toString();
}
String replaceAll(Pattern pattern, String replacement) {
« no previous file with comments | « no previous file | sdk/lib/_internal/lib/js_string.dart » ('j') | sdk/lib/_internal/lib/string_helper.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698