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.str, | 9 String this.str, |
10 String this.pattern); | 10 String this.pattern); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 | 71 |
72 stringReplaceJS(receiver, replacer, to) { | 72 stringReplaceJS(receiver, replacer, to) { |
73 // The JavaScript String.replace method recognizes replacement | 73 // The JavaScript String.replace method recognizes replacement |
74 // patterns in the replacement string. Dart does not have that | 74 // patterns in the replacement string. Dart does not have that |
75 // behavior. | 75 // behavior. |
76 to = JS('String', r'#.replace("$", "$$$$")', to); | 76 to = JS('String', r'#.replace("$", "$$$$")', to); |
77 return JS('String', r'#.replace(#, #)', receiver, replacer, to); | 77 return JS('String', r'#.replace(#, #)', receiver, replacer, to); |
78 } | 78 } |
79 | 79 |
| 80 const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]'; |
| 81 |
80 stringReplaceAllUnchecked(receiver, from, to) { | 82 stringReplaceAllUnchecked(receiver, from, to) { |
81 checkString(to); | 83 checkString(to); |
82 if (from is String) { | 84 if (from is String) { |
83 if (from == "") { | 85 if (from == "") { |
84 if (receiver == "") { | 86 if (receiver == "") { |
85 return to; | 87 return to; |
86 } else { | 88 } else { |
87 StringBuffer result = new StringBuffer(); | 89 StringBuffer result = new StringBuffer(); |
88 int length = receiver.length; | 90 int length = receiver.length; |
89 result.write(to); | 91 result.write(to); |
90 for (int i = 0; i < length; i++) { | 92 for (int i = 0; i < length; i++) { |
91 result.write(receiver[i]); | 93 result.write(receiver[i]); |
92 result.write(to); | 94 result.write(to); |
93 } | 95 } |
94 return result.toString(); | 96 return result.toString(); |
95 } | 97 } |
96 } else { | 98 } else { |
97 var quoter = JS('', "new RegExp(#, 'g')", r'[[\]{}()*+?.\\^$|]'); | 99 var quoter = JS('', "new RegExp(#, 'g')", ESCAPE_REGEXP); |
98 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter); | 100 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter); |
99 var replacer = JS('', "new RegExp(#, 'g')", quoted); | 101 var replacer = JS('', "new RegExp(#, 'g')", quoted); |
100 return stringReplaceJS(receiver, replacer, to); | 102 return stringReplaceJS(receiver, replacer, to); |
101 } | 103 } |
102 } else if (from is JSSyntaxRegExp) { | 104 } else if (from is JSSyntaxRegExp) { |
103 var re = regExpGetGlobalNative(from); | 105 var re = regExpGetGlobalNative(from); |
104 return stringReplaceJS(receiver, re, to); | 106 return stringReplaceJS(receiver, re, to); |
105 } else { | 107 } else { |
106 checkNull(from); | 108 checkNull(from); |
107 // TODO(floitsch): implement generic String.replace (with patterns). | 109 // TODO(floitsch): implement generic String.replace (with patterns). |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } else { | 194 } else { |
193 checkNull(from); | 195 checkNull(from); |
194 // TODO(floitsch): implement generic String.replace (with patterns). | 196 // TODO(floitsch): implement generic String.replace (with patterns). |
195 throw "String.replace(Pattern) UNIMPLEMENTED"; | 197 throw "String.replace(Pattern) UNIMPLEMENTED"; |
196 } | 198 } |
197 } | 199 } |
198 | 200 |
199 stringJoinUnchecked(array, separator) { | 201 stringJoinUnchecked(array, separator) { |
200 return JS('String', r'#.join(#)', array, separator); | 202 return JS('String', r'#.join(#)', array, separator); |
201 } | 203 } |
OLD | NEW |