| 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 /** | 5 /** |
| 6 * [_StringBase] contains common methods used by concrete String | 6 * [_StringBase] contains common methods used by concrete String |
| 7 * implementations, e.g., _OneByteString. | 7 * implementations, e.g., _OneByteString. |
| 8 */ | 8 */ |
| 9 class _StringBase { | 9 class _StringBase { |
| 10 | 10 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 return this; | 182 return this; |
| 183 } else { | 183 } else { |
| 184 return _substringUnchecked(first, last + 1); | 184 return _substringUnchecked(first, last + 1); |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 bool contains(Pattern pattern, [int startIndex = 0]) { | 188 bool contains(Pattern pattern, [int startIndex = 0]) { |
| 189 if (pattern is String) { | 189 if (pattern is String) { |
| 190 return indexOf(pattern, startIndex) >= 0; | 190 return indexOf(pattern, startIndex) >= 0; |
| 191 } | 191 } |
| 192 return pattern.allMatches(this.substring(startIndex)).iterator().hasNext; | 192 return pattern.allMatches(this.substring(startIndex)).iterator.moveNext(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 String replaceFirst(Pattern pattern, String replacement) { | 195 String replaceFirst(Pattern pattern, String replacement) { |
| 196 if (pattern is! Pattern) { | 196 if (pattern is! Pattern) { |
| 197 throw new ArgumentError("${pattern} is not a Pattern"); | 197 throw new ArgumentError("${pattern} is not a Pattern"); |
| 198 } | 198 } |
| 199 if (replacement is! String) { | 199 if (replacement is! String) { |
| 200 throw new ArgumentError("${replacement} is not a String"); | 200 throw new ArgumentError("${replacement} is not a String"); |
| 201 } | 201 } |
| 202 StringBuffer buffer = new StringBuffer(); | 202 StringBuffer buffer = new StringBuffer(); |
| 203 int startIndex = 0; | 203 int startIndex = 0; |
| 204 Iterator iterator = pattern.allMatches(this).iterator(); | 204 Iterator iterator = pattern.allMatches(this).iterator; |
| 205 if (iterator.hasNext) { | 205 if (iterator.moveNext()) { |
| 206 Match match = iterator.next(); | 206 Match match = iterator.current; |
| 207 buffer.add(this.substring(startIndex, match.start)).add(replacement); | 207 buffer.add(this.substring(startIndex, match.start)).add(replacement); |
| 208 startIndex = match.end; | 208 startIndex = match.end; |
| 209 } | 209 } |
| 210 return buffer.add(this.substring(startIndex)).toString(); | 210 return buffer.add(this.substring(startIndex)).toString(); |
| 211 } | 211 } |
| 212 | 212 |
| 213 String replaceAll(Pattern pattern, var replacement) { | 213 String replaceAll(Pattern pattern, var replacement) { |
| 214 if (pattern is! Pattern) { | 214 if (pattern is! Pattern) { |
| 215 throw new ArgumentError("${pattern} is not a Pattern"); | 215 throw new ArgumentError("${pattern} is not a Pattern"); |
| 216 } | 216 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 ++startIndex; // empty match, advance and restart | 273 ++startIndex; // empty match, advance and restart |
| 274 } else { | 274 } else { |
| 275 startIndex = endIndex; | 275 startIndex = endIndex; |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 return result; | 278 return result; |
| 279 } | 279 } |
| 280 | 280 |
| 281 List<String> split(Pattern pattern) { | 281 List<String> split(Pattern pattern) { |
| 282 int length = this.length; | 282 int length = this.length; |
| 283 Iterator iterator = pattern.allMatches(this).iterator(); | 283 Iterator iterator = pattern.allMatches(this).iterator; |
| 284 if (length == 0 && iterator.hasNext) { | 284 if (length == 0 && iterator.moveNext()) { |
| 285 // A matched empty string input returns the empty list. | 285 // A matched empty string input returns the empty list. |
| 286 return <String>[]; | 286 return <String>[]; |
| 287 } | 287 } |
| 288 List<String> result = new List<String>(); | 288 List<String> result = new List<String>(); |
| 289 int startIndex = 0; | 289 int startIndex = 0; |
| 290 int previousIndex = 0; | 290 int previousIndex = 0; |
| 291 while (true) { | 291 while (true) { |
| 292 if (startIndex == length || !iterator.hasNext) { | 292 if (startIndex == length || !iterator.moveNext()) { |
| 293 result.add(this.substring(previousIndex, length)); | 293 result.add(this.substring(previousIndex, length)); |
| 294 break; | 294 break; |
| 295 } | 295 } |
| 296 Match match = iterator.next(); | 296 Match match = iterator.current; |
| 297 if (match.start == length) { | 297 if (match.start == length) { |
| 298 result.add(this.substring(previousIndex, length)); | 298 result.add(this.substring(previousIndex, length)); |
| 299 break; | 299 break; |
| 300 } | 300 } |
| 301 int endIndex = match.end; | 301 int endIndex = match.end; |
| 302 if (startIndex == endIndex && endIndex == previousIndex) { | 302 if (startIndex == endIndex && endIndex == previousIndex) { |
| 303 ++startIndex; // empty match, advance and restart | 303 ++startIndex; // empty match, advance and restart |
| 304 continue; | 304 continue; |
| 305 } | 305 } |
| 306 result.add(this.substring(previousIndex, match.start)); | 306 result.add(this.substring(previousIndex, match.start)); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 for (int g in groups) { | 494 for (int g in groups) { |
| 495 result.add(group(g)); | 495 result.add(group(g)); |
| 496 } | 496 } |
| 497 return result; | 497 return result; |
| 498 } | 498 } |
| 499 | 499 |
| 500 final int start; | 500 final int start; |
| 501 final String str; | 501 final String str; |
| 502 final String pattern; | 502 final String pattern; |
| 503 } | 503 } |
| OLD | NEW |