Index: runtime/lib/string.dart |
diff --git a/runtime/lib/string.dart b/runtime/lib/string.dart |
index 66f026b491d1e14267fa7dd6160b7c91ebc6775f..f40ba005d1c8ea7bfd7cc5e0381e2ff2fbe77ada 100644 |
--- a/runtime/lib/string.dart |
+++ b/runtime/lib/string.dart |
@@ -198,24 +198,37 @@ class StringBase { |
return other.allMatches(this.substring(startIndex)).iterator().hasNext(); |
} |
- String replaceFirst(Pattern from, String to) { |
- if (from is RegExp) { |
- throw "Unimplemented String.replace with RegExp"; |
+ String replaceFirst(Pattern pattern, String to) { |
+ if (pattern is RegExp) { |
+ StringBuffer buffer = new StringBuffer(); |
+ int startIndex = 0; |
+ Match match = pattern.firstMatch(this); |
+ if (match != null) { |
+ buffer.add(this.substring(startIndex, match.start())).add(to); |
+ startIndex = match.end(); |
+ } |
+ return buffer.add(this.substring(startIndex)).toString(); |
} |
Ivan Posva
2012/03/14 05:05:37
The assumption here is that there are only two imp
cshapiro
2012/03/14 20:18:00
Yes, I am perpetuating the assumption. It is my b
|
- int pos = this.indexOf(from, 0); |
+ int pos = this.indexOf(pattern, 0); |
if (pos < 0) { |
return this; |
} |
String s1 = this.substring(0, pos); |
- String s2 = this.substring(pos + from.length, this.length); |
+ String s2 = this.substring(pos + pattern.length, this.length); |
return s1.concat(to.concat(s2)); |
} |
- String replaceAll(Pattern from_, String to) { |
- if (from_ is RegExp) { |
- throw "Unimplemented String.replaceAll with RegExp"; |
+ String replaceAll(Pattern pattern, String to) { |
+ if (pattern is RegExp) { |
+ StringBuffer buffer = new StringBuffer(); |
+ int startIndex = 0; |
+ for (Match match in pattern.allMatches(this)) { |
+ buffer.add(this.substring(startIndex, match.start())).add(to); |
+ startIndex = match.end(); |
+ } |
+ return buffer.add(this.substring(startIndex)).toString(); |
} |
- String from = from_; |
+ String from = pattern; |
Ivan Posva
2012/03/14 05:05:37
ditto
cshapiro
2012/03/14 20:18:00
Agreed.
|
int fromLength = from.length; |
int toLength = to.length; |
int thisLength = this.length; |
@@ -301,10 +314,16 @@ class StringBase { |
} |
List<String> split(Pattern pattern) { |
+ List<String> result = new List<String>(); |
if (pattern is RegExp) { |
- throw "Unimplemented split with RegExp"; |
+ int startIndex = 0; |
+ for (Match match in pattern.allMatches(this)) { |
+ result.add(this.substring(startIndex, match.start())); |
+ startIndex = match.end(); |
+ } |
+ result.add(this.substring(startIndex)); |
+ return result; |
} |
- List<String> result = new List<String>(); |
if (pattern.isEmpty()) { |
for (int i = 0; i < this.length; i++) { |
result.add(this.substring(i, i+1)); |