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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/string_helper.dart

Issue 920453002: Add String.replaceFirstMapped. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: With change to _interpolate methods. Created 5 years, 10 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 | « sdk/lib/_internal/compiler/js_lib/js_string.dart ('k') | sdk/lib/core/string.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 part of _js_helper; 5 part of _js_helper;
6 6
7 class StringMatch implements Match { 7 class StringMatch implements Match {
8 const StringMatch(int this.start, 8 const StringMatch(int this.start,
9 String this.input, 9 String this.input,
10 String this.pattern); 10 String this.pattern);
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 } 186 }
187 buffer.write(onNonMatch(receiver.substring(startIndex, position))); 187 buffer.write(onNonMatch(receiver.substring(startIndex, position)));
188 buffer.write(onMatch(new StringMatch(position, receiver, pattern))); 188 buffer.write(onMatch(new StringMatch(position, receiver, pattern)));
189 startIndex = position + patternLength; 189 startIndex = position + patternLength;
190 } 190 }
191 buffer.write(onNonMatch(receiver.substring(startIndex))); 191 buffer.write(onNonMatch(receiver.substring(startIndex)));
192 return buffer.toString(); 192 return buffer.toString();
193 } 193 }
194 194
195 195
196 stringReplaceFirstUnchecked(receiver, from, to, [int startIndex = 0]) { 196 stringReplaceFirstUnchecked(receiver, from, to, int startIndex) {
197 if (from is String) { 197 if (from is String) {
198 var index = receiver.indexOf(from, startIndex); 198 int index = receiver.indexOf(from, startIndex);
199 if (index < 0) return receiver; 199 if (index < 0) return receiver;
200 return '${receiver.substring(0, index)}$to' 200 return '${receiver.substring(0, index)}$to'
201 '${receiver.substring(index + from.length)}'; 201 '${receiver.substring(index + from.length)}';
sra1 2015/02/17 17:01:00 This common pattern (3x) makes me think that we sh
202 } else if (from is JSSyntaxRegExp) { 202 }
203 if (from is JSSyntaxRegExp) {
203 return startIndex == 0 ? 204 return startIndex == 0 ?
204 stringReplaceJS(receiver, regExpGetNative(from), to) : 205 stringReplaceJS(receiver, regExpGetNative(from), to) :
205 stringReplaceFirstRE(receiver, from, to, startIndex); 206 stringReplaceFirstRE(receiver, from, to, startIndex);
206 } else {
207 checkNull(from);
208 // TODO(floitsch): implement generic String.replace (with patterns).
209 throw "String.replace(Pattern) UNIMPLEMENTED";
210 } 207 }
208 checkNull(from);
209 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator;
210 if (!matches.moveNext()) return receiver;
211 Match match = matches.current;
212 return '${receiver.substring(0, match.start)}$to'
213 '${receiver.substring(match.end)}';
214 }
215
216 stringReplaceFirstMappedUnchecked(receiver, from, replace,
217 int startIndex) {
218 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator;
219 if (!matches.moveNext()) return receiver;
220 Match match = matches.current;
221 String replacement = "${replace(match)}";
222 return '${receiver.substring(0, match.start)}$replacement'
223 '${receiver.substring(match.end)}';
211 } 224 }
212 225
213 stringJoinUnchecked(array, separator) { 226 stringJoinUnchecked(array, separator) {
214 return JS('String', r'#.join(#)', array, separator); 227 return JS('String', r'#.join(#)', array, separator);
215 } 228 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/js_string.dart ('k') | sdk/lib/core/string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698