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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/co19/co19-runtime.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 implementations, 6 * [StringBase] contains common methods used by concrete String implementations,
7 * e.g., OneByteString. 7 * e.g., OneByteString.
8 */ 8 */
9 class StringBase { 9 class StringBase {
10 10
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 191 }
192 } 192 }
193 193
194 bool contains(Pattern other, [int startIndex = 0]) { 194 bool contains(Pattern other, [int startIndex = 0]) {
195 if (other is String) { 195 if (other is String) {
196 return indexOf(other, startIndex) >= 0; 196 return indexOf(other, startIndex) >= 0;
197 } 197 }
198 return other.allMatches(this.substring(startIndex)).iterator().hasNext(); 198 return other.allMatches(this.substring(startIndex)).iterator().hasNext();
199 } 199 }
200 200
201 String replaceFirst(Pattern from, String to) { 201 String replaceFirst(Pattern pattern, String to) {
202 if (from is RegExp) { 202 if (pattern is RegExp) {
203 throw "Unimplemented String.replace with RegExp"; 203 StringBuffer buffer = new StringBuffer();
204 int startIndex = 0;
205 Match match = pattern.firstMatch(this);
206 if (match != null) {
207 buffer.add(this.substring(startIndex, match.start())).add(to);
208 startIndex = match.end();
209 }
210 return buffer.add(this.substring(startIndex)).toString();
204 } 211 }
205 int pos = this.indexOf(from, 0); 212 int pos = this.indexOf(pattern, 0);
206 if (pos < 0) { 213 if (pos < 0) {
207 return this; 214 return this;
208 } 215 }
209 String s1 = this.substring(0, pos); 216 String s1 = this.substring(0, pos);
210 String s2 = this.substring(pos + from.length, this.length); 217 String s2 = this.substring(pos + pattern.length, this.length);
211 return s1.concat(to.concat(s2)); 218 return s1.concat(to.concat(s2));
212 } 219 }
213 220
214 String replaceAll(Pattern from_, String to) { 221 String replaceAll(Pattern pattern, String to) {
215 if (from_ is RegExp) { 222 if (pattern is RegExp) {
216 throw "Unimplemented String.replaceAll with RegExp"; 223 StringBuffer buffer = new StringBuffer();
224 int startIndex = 0;
225 for (Match match in pattern.allMatches(this)) {
226 buffer.add(this.substring(startIndex, match.start())).add(to);
227 startIndex = match.end();
228 }
229 return buffer.add(this.substring(startIndex)).toString();
217 } 230 }
218 String from = from_; 231 String from = pattern;
219 int fromLength = from.length; 232 int fromLength = from.length;
220 int toLength = to.length; 233 int toLength = to.length;
221 int thisLength = this.length; 234 int thisLength = this.length;
222 235
223 StringBuffer result = new StringBuffer(""); 236 StringBuffer result = new StringBuffer("");
224 // Special case the empty string replacement where [to] is 237 // Special case the empty string replacement where [to] is
225 // inserted in between each character. 238 // inserted in between each character.
226 if (fromLength === 0) { 239 if (fromLength === 0) {
227 result.add(to); 240 result.add(to);
228 for (int i = 0; i < thisLength; i++) { 241 for (int i = 0; i < thisLength; i++) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 while (ix < str.length) { 307 while (ix < str.length) {
295 int foundIx = str.indexOf(this, ix); 308 int foundIx = str.indexOf(this, ix);
296 if (foundIx < 0) break; 309 if (foundIx < 0) break;
297 result.add(new _StringMatch(foundIx, str, this)); 310 result.add(new _StringMatch(foundIx, str, this));
298 ix = foundIx + length; 311 ix = foundIx + length;
299 } 312 }
300 return result; 313 return result;
301 } 314 }
302 315
303 List<String> split(Pattern pattern) { 316 List<String> split(Pattern pattern) {
317 List<String> result = new List<String>();
304 if (pattern is RegExp) { 318 if (pattern is RegExp) {
305 throw "Unimplemented split with RegExp"; 319 int startIndex = 0;
320 for (Match match in pattern.allMatches(this)) {
321 result.add(this.substring(startIndex, match.start()));
322 startIndex = match.end();
323 }
324 result.add(this.substring(startIndex));
325 return result;
306 } 326 }
307 List<String> result = new List<String>();
308 if (pattern.isEmpty()) { 327 if (pattern.isEmpty()) {
309 for (int i = 0; i < this.length; i++) { 328 for (int i = 0; i < this.length; i++) {
310 result.add(this.substring(i, i+1)); 329 result.add(this.substring(i, i+1));
311 } 330 }
312 return result; 331 return result;
313 } 332 }
314 int ix = 0; 333 int ix = 0;
315 while (ix < this.length) { 334 while (ix < this.length) {
316 int foundIx = this.indexOf(pattern, ix); 335 int foundIx = this.indexOf(pattern, ix);
317 if (foundIx < 0) { 336 if (foundIx < 0) {
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 for (int g in groups) { 533 for (int g in groups) {
515 result.add(group(g)); 534 result.add(group(g));
516 } 535 }
517 return result; 536 return result;
518 } 537 }
519 538
520 final int _start; 539 final int _start;
521 final String str; 540 final String str;
522 final String pattern; 541 final String pattern;
523 } 542 }
OLDNEW
« 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