OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class StringImplementation implements String native "String" { | 5 class StringImplementation implements String native "String" { |
6 factory StringImplementation.fromValues(Array<int> values) { | 6 factory StringImplementation.fromValues(List<int> values) { |
7 return _newFromValues(values); | 7 return _newFromValues(values); |
8 } | 8 } |
9 | 9 |
10 String operator[](int index) { | 10 String operator[](int index) { |
11 if (0 <= index && index < length) { | 11 if (0 <= index && index < length) { |
12 return _indexOperator(index); | 12 return _indexOperator(index); |
13 } | 13 } |
14 throw new IndexOutOfRangeException(index); | 14 throw new IndexOutOfRangeException(index); |
15 } | 15 } |
16 | 16 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return _replaceAll(from, to); | 121 return _replaceAll(from, to); |
122 } | 122 } |
123 } else if (from is JSSyntaxRegExp) { | 123 } else if (from is JSSyntaxRegExp) { |
124 return _replaceAll(from, to); | 124 return _replaceAll(from, to); |
125 } else { | 125 } else { |
126 // TODO(floitsch): implement generic String.replace (with patterns). | 126 // TODO(floitsch): implement generic String.replace (with patterns). |
127 throw "StringImplementation.replaceAll(Pattern) UNIMPLEMENTED"; | 127 throw "StringImplementation.replaceAll(Pattern) UNIMPLEMENTED"; |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 Array<String> split(Pattern pattern) { | 131 List<String> split(Pattern pattern) { |
132 if (pattern is String || pattern is JSSyntaxRegExp) { | 132 if (pattern is String || pattern is JSSyntaxRegExp) { |
133 return _split(pattern); | 133 return _split(pattern); |
134 } else { | 134 } else { |
135 throw "StringImplementation.split(Pattern) UNIMPLEMENTED"; | 135 throw "StringImplementation.split(Pattern) UNIMPLEMENTED"; |
136 } | 136 } |
137 } | 137 } |
138 | 138 |
139 Iterable<Match> allMatches(String str) { | 139 Iterable<Match> allMatches(String str) { |
140 List<Match> result = []; | 140 List<Match> result = []; |
141 if (this.isEmpty()) return result; | 141 if (this.isEmpty()) return result; |
142 int length = this.length; | 142 int length = this.length; |
143 | 143 |
144 int ix = 0; | 144 int ix = 0; |
145 while (ix < str.length) { | 145 while (ix < str.length) { |
146 int foundIx = str.indexOf(this, ix); | 146 int foundIx = str.indexOf(this, ix); |
147 if (foundIx < 0) break; | 147 if (foundIx < 0) break; |
148 // Call "toString" to coerce the "this" back to a primitive string. | 148 // Call "toString" to coerce the "this" back to a primitive string. |
149 result.add(new _StringMatch(foundIx, str, this.toString())); | 149 result.add(new _StringMatch(foundIx, str, this.toString())); |
150 ix = foundIx + length; | 150 ix = foundIx + length; |
151 } | 151 } |
152 return result; | 152 return result; |
153 } | 153 } |
154 | 154 |
155 Array<String> splitChars() { | 155 List<String> splitChars() { |
156 return _split(""); | 156 return _split(""); |
157 } | 157 } |
158 | 158 |
159 Array<int> charCodes() { | 159 List<int> charCodes() { |
160 int len = length; | 160 int len = length; |
161 Array<int> result = new Array<int>(len); | 161 List<int> result = new List<int>(len); |
162 for (int i = 0; i < len; i++) { | 162 for (int i = 0; i < len; i++) { |
163 // It is safe to call the private function (which doesn't do | 163 // It is safe to call the private function (which doesn't do |
164 // range-checks). | 164 // range-checks). |
165 result[i] = _charCodeAt(i); | 165 result[i] = _charCodeAt(i); |
166 } | 166 } |
167 return result; | 167 return result; |
168 } | 168 } |
169 | 169 |
170 String toLowerCase() native; | 170 String toLowerCase() native; |
171 String toUpperCase() native; | 171 String toUpperCase() native; |
172 | 172 |
173 int hashCode() native; | 173 int hashCode() native; |
174 | 174 |
175 // Note: we can't just return 'this', because we want the primitive string | 175 // Note: we can't just return 'this', because we want the primitive string |
176 // and not the wrapped String object. | 176 // and not the wrapped String object. |
177 String toString() native; | 177 String toString() native; |
178 | 178 |
179 int compareTo(String other) native; | 179 int compareTo(String other) native; |
180 | 180 |
181 static String _newFromValues(Array<int> values) native; | 181 static String _newFromValues(List<int> values) native; |
182 String _indexOperator(int index) native; | 182 String _indexOperator(int index) native; |
183 int _charCodeAt(int index) native; | 183 int _charCodeAt(int index) native; |
184 String _substringUnchecked(int startIndex, int endIndex) native; | 184 String _substringUnchecked(int startIndex, int endIndex) native; |
185 String _replace(Pattern from, String to) native; | 185 String _replace(Pattern from, String to) native; |
186 String _replaceAll(Pattern from, String to) native; | 186 String _replaceAll(Pattern from, String to) native; |
187 Array<String> _split(Pattern pattern) native; | 187 List<String> _split(Pattern pattern) native; |
188 | 188 |
189 get dynamic() { return toString(); } | 189 get dynamic() { return toString(); } |
190 } | 190 } |
191 | 191 |
192 class _StringJsUtil { | 192 class _StringJsUtil { |
193 static String toDartString(o) native { | 193 static String toDartString(o) native { |
194 if (o === null) return "null"; | 194 if (o === null) return "null"; |
195 return o.toString(); | 195 return o.toString(); |
196 } | 196 } |
197 } | 197 } |
198 | 198 |
199 class _StringMatch implements Match { | 199 class _StringMatch implements Match { |
200 const _StringMatch(int this._start, | 200 const _StringMatch(int this._start, |
201 String this.str, | 201 String this.str, |
202 String this.pattern); | 202 String this.pattern); |
203 | 203 |
204 int start() => _start; | 204 int start() => _start; |
205 int end() => _start + pattern.length; | 205 int end() => _start + pattern.length; |
206 String operator[](int g) => group(g); | 206 String operator[](int g) => group(g); |
207 int groupCount() => 0; | 207 int groupCount() => 0; |
208 | 208 |
209 String group(int group) { | 209 String group(int group) { |
210 if (group != 0) { | 210 if (group != 0) { |
211 throw new IndexOutOfRangeException(group); | 211 throw new IndexOutOfRangeException(group); |
212 } | 212 } |
213 return pattern; | 213 return pattern; |
214 } | 214 } |
215 | 215 |
216 Array<String> groups(Array<int> groups) { | 216 List<String> groups(List<int> groups) { |
217 Array<String> result = new Array<String>(); | 217 List<String> result = new List<String>(); |
218 for (int g in groups) { | 218 for (int g in groups) { |
219 result.add(group(g)); | 219 result.add(group(g)); |
220 } | 220 } |
221 return result; | 221 return result; |
222 } | 222 } |
223 | 223 |
224 final int _start; | 224 final int _start; |
225 final String str; | 225 final String str; |
226 final String pattern; | 226 final String pattern; |
227 } | 227 } |
OLD | NEW |