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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 79 |
80 stringReplaceAllUnchecked(receiver, from, to) { | 80 stringReplaceAllUnchecked(receiver, from, to) { |
81 checkString(to); | 81 checkString(to); |
82 if (from is String) { | 82 if (from is String) { |
83 if (from == "") { | 83 if (from == "") { |
84 if (receiver == "") { | 84 if (receiver == "") { |
85 return to; | 85 return to; |
86 } else { | 86 } else { |
87 StringBuffer result = new StringBuffer(); | 87 StringBuffer result = new StringBuffer(); |
88 int length = receiver.length; | 88 int length = receiver.length; |
89 result.add(to); | 89 result.write(to); |
90 for (int i = 0; i < length; i++) { | 90 for (int i = 0; i < length; i++) { |
91 result.add(receiver[i]); | 91 result.write(receiver[i]); |
92 result.add(to); | 92 result.write(to); |
93 } | 93 } |
94 return result.toString(); | 94 return result.toString(); |
95 } | 95 } |
96 } else { | 96 } else { |
97 var quoter = JS('', "new RegExp(#, 'g')", r'[-[\]{}()*+?.,\\^$|#\s]'); | 97 var quoter = JS('', "new RegExp(#, 'g')", r'[-[\]{}()*+?.,\\^$|#\s]'); |
98 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter); | 98 var quoted = JS('String', r'#.replace(#, "\\$&")', from, quoter); |
99 var replacer = JS('', "new RegExp(#, 'g')", quoted); | 99 var replacer = JS('', "new RegExp(#, 'g')", quoted); |
100 return stringReplaceJS(receiver, replacer, to); | 100 return stringReplaceJS(receiver, replacer, to); |
101 } | 101 } |
102 } else if (from is JSSyntaxRegExp) { | 102 } else if (from is JSSyntaxRegExp) { |
(...skipping 15 matching lines...) Expand all Loading... |
118 } | 118 } |
119 if (onMatch == null) onMatch = _matchString; | 119 if (onMatch == null) onMatch = _matchString; |
120 if (onNonMatch == null) onNonMatch = _stringIdentity; | 120 if (onNonMatch == null) onNonMatch = _stringIdentity; |
121 if (pattern is String) { | 121 if (pattern is String) { |
122 return stringReplaceAllStringFuncUnchecked(receiver, pattern, | 122 return stringReplaceAllStringFuncUnchecked(receiver, pattern, |
123 onMatch, onNonMatch); | 123 onMatch, onNonMatch); |
124 } | 124 } |
125 StringBuffer buffer = new StringBuffer(); | 125 StringBuffer buffer = new StringBuffer(); |
126 int startIndex = 0; | 126 int startIndex = 0; |
127 for (Match match in pattern.allMatches(receiver)) { | 127 for (Match match in pattern.allMatches(receiver)) { |
128 buffer.add(onNonMatch(receiver.substring(startIndex, match.start))); | 128 buffer.write(onNonMatch(receiver.substring(startIndex, match.start))); |
129 buffer.add(onMatch(match)); | 129 buffer.write(onMatch(match)); |
130 startIndex = match.end; | 130 startIndex = match.end; |
131 } | 131 } |
132 buffer.add(onNonMatch(receiver.substring(startIndex))); | 132 buffer.write(onNonMatch(receiver.substring(startIndex))); |
133 return buffer.toString(); | 133 return buffer.toString(); |
134 } | 134 } |
135 | 135 |
136 stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch) { | 136 stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch) { |
137 // Pattern is the empty string. | 137 // Pattern is the empty string. |
138 StringBuffer buffer = new StringBuffer(); | 138 StringBuffer buffer = new StringBuffer(); |
139 int length = receiver.length; | 139 int length = receiver.length; |
140 int i = 0; | 140 int i = 0; |
141 buffer.add(onNonMatch("")); | 141 buffer.write(onNonMatch("")); |
142 while (i < length) { | 142 while (i < length) { |
143 buffer.add(onMatch(new StringMatch(i, receiver, ""))); | 143 buffer.write(onMatch(new StringMatch(i, receiver, ""))); |
144 // Special case to avoid splitting a surrogate pair. | 144 // Special case to avoid splitting a surrogate pair. |
145 int code = receiver.codeUnitAt(i); | 145 int code = receiver.codeUnitAt(i); |
146 if ((code & ~0x3FF) == 0xD800 && length > i + 1) { | 146 if ((code & ~0x3FF) == 0xD800 && length > i + 1) { |
147 // Leading surrogate; | 147 // Leading surrogate; |
148 code = receiver.codeUnitAt(i + 1); | 148 code = receiver.codeUnitAt(i + 1); |
149 if ((code & ~0x3FF) == 0xDC00) { | 149 if ((code & ~0x3FF) == 0xDC00) { |
150 // Matching trailing surrogate. | 150 // Matching trailing surrogate. |
151 buffer.add(onNonMatch(receiver.substring(i, i + 2))); | 151 buffer.write(onNonMatch(receiver.substring(i, i + 2))); |
152 i += 2; | 152 i += 2; |
153 continue; | 153 continue; |
154 } | 154 } |
155 } | 155 } |
156 buffer.add(onNonMatch(receiver[i])); | 156 buffer.write(onNonMatch(receiver[i])); |
157 i++; | 157 i++; |
158 } | 158 } |
159 buffer.add(onMatch(new StringMatch(i, receiver, ""))); | 159 buffer.write(onMatch(new StringMatch(i, receiver, ""))); |
160 buffer.add(onNonMatch("")); | 160 buffer.write(onNonMatch("")); |
161 return buffer.toString(); | 161 return buffer.toString(); |
162 } | 162 } |
163 | 163 |
164 stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) { | 164 stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) { |
165 int patternLength = pattern.length; | 165 int patternLength = pattern.length; |
166 if (patternLength == 0) { | 166 if (patternLength == 0) { |
167 return stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch); | 167 return stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch); |
168 } | 168 } |
169 int length = receiver.length; | 169 int length = receiver.length; |
170 StringBuffer buffer = new StringBuffer(); | 170 StringBuffer buffer = new StringBuffer(); |
171 int startIndex = 0; | 171 int startIndex = 0; |
172 while (startIndex < length) { | 172 while (startIndex < length) { |
173 int position = receiver.indexOf(pattern, startIndex); | 173 int position = receiver.indexOf(pattern, startIndex); |
174 if (position == -1) { | 174 if (position == -1) { |
175 break; | 175 break; |
176 } | 176 } |
177 buffer.add(onNonMatch(receiver.substring(startIndex, position))); | 177 buffer.write(onNonMatch(receiver.substring(startIndex, position))); |
178 buffer.add(onMatch(new StringMatch(position, receiver, pattern))); | 178 buffer.write(onMatch(new StringMatch(position, receiver, pattern))); |
179 startIndex = position + patternLength; | 179 startIndex = position + patternLength; |
180 } | 180 } |
181 buffer.add(onNonMatch(receiver.substring(startIndex))); | 181 buffer.write(onNonMatch(receiver.substring(startIndex))); |
182 return buffer.toString(); | 182 return buffer.toString(); |
183 } | 183 } |
184 | 184 |
185 | 185 |
186 stringReplaceFirstUnchecked(receiver, from, to) { | 186 stringReplaceFirstUnchecked(receiver, from, to) { |
187 if (from is String) { | 187 if (from is String) { |
188 return stringReplaceJS(receiver, from, to); | 188 return stringReplaceJS(receiver, from, to); |
189 } else if (from is JSSyntaxRegExp) { | 189 } else if (from is JSSyntaxRegExp) { |
190 var re = regExpGetNative(from); | 190 var re = regExpGetNative(from); |
191 return stringReplaceJS(receiver, re, to); | 191 return stringReplaceJS(receiver, re, to); |
192 } else { | 192 } else { |
193 checkNull(from); | 193 checkNull(from); |
194 // TODO(floitsch): implement generic String.replace (with patterns). | 194 // TODO(floitsch): implement generic String.replace (with patterns). |
195 throw "String.replace(Pattern) UNIMPLEMENTED"; | 195 throw "String.replace(Pattern) UNIMPLEMENTED"; |
196 } | 196 } |
197 } | 197 } |
198 | 198 |
199 stringJoinUnchecked(array, separator) { | 199 stringJoinUnchecked(array, separator) { |
200 return JS('String', r'#.join(#)', array, separator); | 200 return JS('String', r'#.join(#)', array, separator); |
201 } | 201 } |
OLD | NEW |