OLD | NEW |
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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // behavior. | 75 // behavior. |
76 to = JS('String', r'#.replace(/\$/g, "$$$$")', to); | 76 to = JS('String', r'#.replace(/\$/g, "$$$$")', to); |
77 return JS('String', r'#.replace(#, #)', receiver, replacer, to); | 77 return JS('String', r'#.replace(#, #)', receiver, replacer, to); |
78 } | 78 } |
79 | 79 |
80 stringReplaceFirstRE(receiver, regexp, to, startIndex) { | 80 stringReplaceFirstRE(receiver, regexp, to, startIndex) { |
81 var match = regexp._execGlobal(receiver, startIndex); | 81 var match = regexp._execGlobal(receiver, startIndex); |
82 if (match == null) return receiver; | 82 if (match == null) return receiver; |
83 var start = match.start; | 83 var start = match.start; |
84 var end = match.end; | 84 var end = match.end; |
85 return "${receiver.substring(0,start)}$to${receiver.substring(end)}"; | 85 return stringReplaceRangeUnchecked(receiver, start, end, to); |
86 } | 86 } |
87 | 87 |
88 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; | 88 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; |
89 | 89 |
90 stringReplaceAllUnchecked(receiver, from, to) { | 90 stringReplaceAllUnchecked(receiver, from, to) { |
91 checkString(to); | 91 checkString(to); |
92 if (from is String) { | 92 if (from is String) { |
93 if (from == "") { | 93 if (from == "") { |
94 if (receiver == "") { | 94 if (receiver == "") { |
95 return to; | 95 return to; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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) { | 196 stringReplaceFirstUnchecked(receiver, from, to, int startIndex) { |
197 if (from is String) { | 197 if (from is String) { |
198 int 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 int end = index + from.length; |
201 '${receiver.substring(index + from.length)}'; | 201 return stringReplaceRangeUnchecked(receiver, index, end, to); |
202 } | 202 } |
203 if (from is JSSyntaxRegExp) { | 203 if (from is JSSyntaxRegExp) { |
204 return startIndex == 0 ? | 204 return startIndex == 0 ? |
205 stringReplaceJS(receiver, regExpGetNative(from), to) : | 205 stringReplaceJS(receiver, regExpGetNative(from), to) : |
206 stringReplaceFirstRE(receiver, from, to, startIndex); | 206 stringReplaceFirstRE(receiver, from, to, startIndex); |
207 } | 207 } |
208 checkNull(from); | 208 checkNull(from); |
209 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator; | 209 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator; |
210 if (!matches.moveNext()) return receiver; | 210 if (!matches.moveNext()) return receiver; |
211 Match match = matches.current; | 211 Match match = matches.current; |
212 return '${receiver.substring(0, match.start)}$to' | 212 return '${receiver.substring(0, match.start)}$to' |
213 '${receiver.substring(match.end)}'; | 213 '${receiver.substring(match.end)}'; |
214 } | 214 } |
215 | 215 |
216 stringReplaceFirstMappedUnchecked(receiver, from, replace, | 216 stringReplaceFirstMappedUnchecked(receiver, from, replace, |
217 int startIndex) { | 217 int startIndex) { |
218 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator; | 218 Iterator<Match> matches = from.allMatches(receiver, startIndex).iterator; |
219 if (!matches.moveNext()) return receiver; | 219 if (!matches.moveNext()) return receiver; |
220 Match match = matches.current; | 220 Match match = matches.current; |
221 String replacement = "${replace(match)}"; | 221 String replacement = "${replace(match)}"; |
222 return '${receiver.substring(0, match.start)}$replacement' | 222 return '${receiver.substring(0, match.start)}$replacement' |
223 '${receiver.substring(match.end)}'; | 223 '${receiver.substring(match.end)}'; |
224 } | 224 } |
225 | 225 |
226 stringJoinUnchecked(array, separator) { | 226 stringJoinUnchecked(array, separator) { |
227 return JS('String', r'#.join(#)', array, separator); | 227 return JS('String', r'#.join(#)', array, separator); |
228 } | 228 } |
| 229 |
| 230 String stringReplaceRangeUnchecked(String receiver, |
| 231 int start, int end, String replacement) { |
| 232 var prefix = JS('String', '#.substring(0, #)', receiver, start); |
| 233 var suffix = JS('String', '#.substring(#)', receiver, end); |
| 234 return "$prefix$replacement$suffix"; |
| 235 } |
OLD | NEW |