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

Unified Diff: runtime/lib/string.dart

Issue 9689071: Implement regular expression support for split and replace{First,All}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: update corelib.stat comment for StringSplitRegExpTest skip Created 8 years, 9 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 | tests/co19/co19-runtime.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
}
- 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;
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));
« no previous file with comments | « no previous file | tests/co19/co19-runtime.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698