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

Side by Side Diff: runtime/lib/string_base.dart

Issue 11312144: Allow function as replacement argument to String.replaceAll. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 8 years, 1 month 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 | sdk/lib/_internal/compiler/implementation/lib/interceptors.dart » ('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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 6 * [_StringBase] contains common methods used by concrete String
7 * implementations, e.g., _OneByteString. 7 * implementations, e.g., _OneByteString.
8 */ 8 */
9 class _StringBase { 9 class _StringBase {
10 10
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 int startIndex = 0; 203 int startIndex = 0;
204 Iterator iterator = pattern.allMatches(this).iterator(); 204 Iterator iterator = pattern.allMatches(this).iterator();
205 if (iterator.hasNext) { 205 if (iterator.hasNext) {
206 Match match = iterator.next(); 206 Match match = iterator.next();
207 buffer.add(this.substring(startIndex, match.start)).add(replacement); 207 buffer.add(this.substring(startIndex, match.start)).add(replacement);
208 startIndex = match.end; 208 startIndex = match.end;
209 } 209 }
210 return buffer.add(this.substring(startIndex)).toString(); 210 return buffer.add(this.substring(startIndex)).toString();
211 } 211 }
212 212
213 String replaceAll(Pattern pattern, String replacement) { 213 String replaceAll(Pattern pattern, var replacement) {
214 if (pattern is! Pattern) { 214 if (pattern is! Pattern) {
215 throw new ArgumentError("${pattern} is not a Pattern"); 215 throw new ArgumentError("${pattern} is not a Pattern");
216 } 216 }
217 if (replacement is Function) {
218 StringBuffer buffer = new StringBuffer();
219 int startIndex = 0;
220 for (Match match in pattern.allMatches(this)) {
221 if (match.start > startIndex) {
222 buffer.add(this.substring(startIndex, match.start));
223 }
224 buffer.add(replacement(match).toString());
225 startIndex = match.end;
226 }
227 if (startIndex < this.length) {
228 buffer.add(this.substring(startIndex));
229 }
230 return buffer.toString();
231 }
217 if (replacement is! String) { 232 if (replacement is! String) {
218 throw new ArgumentError("${replacement} is not a String"); 233 throw new ArgumentError(
234 "${replacement} is not a String or Match->String function");
219 } 235 }
220 StringBuffer buffer = new StringBuffer(); 236 StringBuffer buffer = new StringBuffer();
221 int startIndex = 0; 237 int startIndex = 0;
222 for (Match match in pattern.allMatches(this)) { 238 for (Match match in pattern.allMatches(this)) {
223 buffer.add(this.substring(startIndex, match.start)).add(replacement); 239 buffer.add(this.substring(startIndex, match.start)).add(replacement);
224 startIndex = match.end; 240 startIndex = match.end;
225 } 241 }
226 return buffer.add(this.substring(startIndex)).toString(); 242 return buffer.add(this.substring(startIndex)).toString();
227 } 243 }
228 244
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 for (int g in groups) { 494 for (int g in groups) {
479 result.add(group(g)); 495 result.add(group(g));
480 } 496 }
481 return result; 497 return result;
482 } 498 }
483 499
484 final int start; 500 final int start;
485 final String str; 501 final String str;
486 final String pattern; 502 final String pattern;
487 } 503 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/interceptors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698