| 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().hasNext; |
| 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.hasNext) { |
| 206 Match match = iterator.next(); | 206 Match match = iterator.next(); |
| 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, String replacement) { | 213 String replaceAll(Pattern pattern, String 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"); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 } else { | 258 } else { |
| 259 startIndex = endIndex; | 259 startIndex = endIndex; |
| 260 } | 260 } |
| 261 } | 261 } |
| 262 return result; | 262 return result; |
| 263 } | 263 } |
| 264 | 264 |
| 265 List<String> split(Pattern pattern) { | 265 List<String> split(Pattern pattern) { |
| 266 int length = this.length; | 266 int length = this.length; |
| 267 Iterator iterator = pattern.allMatches(this).iterator(); | 267 Iterator iterator = pattern.allMatches(this).iterator(); |
| 268 if (length == 0 && iterator.hasNext()) { | 268 if (length == 0 && iterator.hasNext) { |
| 269 // A matched empty string input returns the empty list. | 269 // A matched empty string input returns the empty list. |
| 270 return <String>[]; | 270 return <String>[]; |
| 271 } | 271 } |
| 272 List<String> result = new List<String>(); | 272 List<String> result = new List<String>(); |
| 273 int startIndex = 0; | 273 int startIndex = 0; |
| 274 int previousIndex = 0; | 274 int previousIndex = 0; |
| 275 while (true) { | 275 while (true) { |
| 276 if (startIndex == length || !iterator.hasNext()) { | 276 if (startIndex == length || !iterator.hasNext) { |
| 277 result.add(this.substring(previousIndex, length)); | 277 result.add(this.substring(previousIndex, length)); |
| 278 break; | 278 break; |
| 279 } | 279 } |
| 280 Match match = iterator.next(); | 280 Match match = iterator.next(); |
| 281 if (match.start() == length) { | 281 if (match.start() == length) { |
| 282 result.add(this.substring(previousIndex, length)); | 282 result.add(this.substring(previousIndex, length)); |
| 283 break; | 283 break; |
| 284 } | 284 } |
| 285 int endIndex = match.end(); | 285 int endIndex = match.end(); |
| 286 if (startIndex == endIndex && endIndex == previousIndex) { | 286 if (startIndex == endIndex && endIndex == previousIndex) { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 for (int g in groups) { | 479 for (int g in groups) { |
| 480 result.add(group(g)); | 480 result.add(group(g)); |
| 481 } | 481 } |
| 482 return result; | 482 return result; |
| 483 } | 483 } |
| 484 | 484 |
| 485 final int _start; | 485 final int _start; |
| 486 final String str; | 486 final String str; |
| 487 final String pattern; | 487 final String pattern; |
| 488 } | 488 } |
| OLD | NEW |