| 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 implementations, | 6 * [_StringBase] contains common methods used by concrete String implementations
, |
| 7 * e.g., OneByteString. | 7 * e.g., _OneByteString. |
| 8 */ | 8 */ |
| 9 class StringBase { | 9 class _StringBase { |
| 10 | 10 |
| 11 factory StringBase._uninstantiable() { | 11 factory _StringBase._uninstantiable() { |
| 12 throw const UnsupportedOperationException( | 12 throw const UnsupportedOperationException( |
| 13 "StringBase can't be instaniated"); | 13 "_StringBase can't be instaniated"); |
| 14 } | 14 } |
| 15 | 15 |
| 16 int hashCode() native "String_hashCode"; | 16 int hashCode() native "String_hashCode"; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * Create the most efficient string representation for specified | 19 * Create the most efficient string representation for specified |
| 20 * [codePoints]. | 20 * [codePoints]. |
| 21 */ | 21 */ |
| 22 static String createFromCharCodes(List<int> charCodes) { | 22 static String createFromCharCodes(List<int> charCodes) { |
| 23 _ObjectArray objectArray; | 23 _ObjectArray objectArray; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 76 } |
| 77 if (thisCodePoint > otherCodePoint) { | 77 if (thisCodePoint > otherCodePoint) { |
| 78 return 1; | 78 return 1; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 if (thisLength < otherLength) return -1; | 81 if (thisLength < otherLength) return -1; |
| 82 if (thisLength > otherLength) return 1; | 82 if (thisLength > otherLength) return 1; |
| 83 return 0; | 83 return 0; |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool substringMatches(int start, String other) { | 86 bool _substringMatches(int start, String other) { |
| 87 if (other.isEmpty()) return true; | 87 if (other.isEmpty()) return true; |
| 88 if ((start < 0) || (start >= this.length)) { | 88 if ((start < 0) || (start >= this.length)) { |
| 89 return false; | 89 return false; |
| 90 } | 90 } |
| 91 final int len = other.length; | 91 final int len = other.length; |
| 92 if ((start + len) > this.length) { | 92 if ((start + len) > this.length) { |
| 93 return false; | 93 return false; |
| 94 } | 94 } |
| 95 for (int i = 0; i < len; i++) { | 95 for (int i = 0; i < len; i++) { |
| 96 if (this.charCodeAt(i + start) != other.charCodeAt(i)) { | 96 if (this.charCodeAt(i + start) != other.charCodeAt(i)) { |
| 97 return false; | 97 return false; |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 return true; | 100 return true; |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool endsWith(String other) { | 103 bool endsWith(String other) { |
| 104 return this.substringMatches(this.length - other.length, other); | 104 return _substringMatches(this.length - other.length, other); |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool startsWith(String other) { | 107 bool startsWith(String other) { |
| 108 return this.substringMatches(0, other); | 108 return _substringMatches(0, other); |
| 109 } | 109 } |
| 110 | 110 |
| 111 int indexOf(String other, [int start = 0]) { | 111 int indexOf(String other, [int start = 0]) { |
| 112 if (other.isEmpty()) { | 112 if (other.isEmpty()) { |
| 113 return start < this.length ? start : this.length; | 113 return start < this.length ? start : this.length; |
| 114 } | 114 } |
| 115 if ((start < 0) || (start >= this.length)) { | 115 if ((start < 0) || (start >= this.length)) { |
| 116 return -1; | 116 return -1; |
| 117 } | 117 } |
| 118 int len = this.length - other.length + 1; | 118 int len = this.length - other.length + 1; |
| 119 for (int index = start; index < len; index++) { | 119 for (int index = start; index < len; index++) { |
| 120 if (this.substringMatches(index, other)) { | 120 if (_substringMatches(index, other)) { |
| 121 return index; | 121 return index; |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 return -1; | 124 return -1; |
| 125 } | 125 } |
| 126 | 126 |
| 127 int lastIndexOf(String other, [int start = null]) { | 127 int lastIndexOf(String other, [int start = null]) { |
| 128 if (start == null) start = length - 1; | 128 if (start == null) start = length - 1; |
| 129 if (other.isEmpty()) { | 129 if (other.isEmpty()) { |
| 130 return min(this.length, start); | 130 return min(this.length, start); |
| 131 } | 131 } |
| 132 if (start >= this.length) { | 132 if (start >= this.length) { |
| 133 start = this.length - 1; | 133 start = this.length - 1; |
| 134 } | 134 } |
| 135 for (int index = start; index >= 0; index--) { | 135 for (int index = start; index >= 0; index--) { |
| 136 if (this.substringMatches(index, other)) { | 136 if (_substringMatches(index, other)) { |
| 137 return index; | 137 return index; |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 return -1; | 140 return -1; |
| 141 } | 141 } |
| 142 | 142 |
| 143 String substring(int startIndex, [int endIndex]) { | 143 String substring(int startIndex, [int endIndex]) { |
| 144 if (endIndex === null) endIndex = this.length; | 144 if (endIndex === null) endIndex = this.length; |
| 145 | 145 |
| 146 if ((startIndex < 0) || (startIndex > this.length)) { | 146 if ((startIndex < 0) || (startIndex > this.length)) { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 return _concatAll(stringsArray); | 349 return _concatAll(stringsArray); |
| 350 } | 350 } |
| 351 | 351 |
| 352 static String _concatAll(_ObjectArray<String> strings) | 352 static String _concatAll(_ObjectArray<String> strings) |
| 353 native "Strings_concatAll"; | 353 native "Strings_concatAll"; |
| 354 } | 354 } |
| 355 | 355 |
| 356 | 356 |
| 357 class OneByteString extends StringBase implements String { | 357 class _OneByteString extends _StringBase implements String { |
| 358 factory OneByteString._uninstantiable() { | 358 factory _OneByteString._uninstantiable() { |
| 359 throw const UnsupportedOperationException( | 359 throw const UnsupportedOperationException( |
| 360 "OneByteString can only be allocated by the VM"); | 360 "_OneByteString can only be allocated by the VM"); |
| 361 } | 361 } |
| 362 | 362 |
| 363 // Checks for one-byte whitespaces only. | 363 // Checks for one-byte whitespaces only. |
| 364 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 364 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 365 // whitespaces for one byte strings. | 365 // whitespaces for one byte strings. |
| 366 bool _isWhitespace(int codePoint) { | 366 bool _isWhitespace(int codePoint) { |
| 367 return | 367 return |
| 368 (codePoint === 32) || // Space. | 368 (codePoint === 32) || // Space. |
| 369 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 369 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 370 } | 370 } |
| 371 | 371 |
| 372 } | 372 } |
| 373 | 373 |
| 374 | 374 |
| 375 class TwoByteString extends StringBase implements String { | 375 class _TwoByteString extends _StringBase implements String { |
| 376 factory TwoByteString._uninstantiable() { | 376 factory _TwoByteString._uninstantiable() { |
| 377 throw const UnsupportedOperationException( | 377 throw const UnsupportedOperationException( |
| 378 "TwoByteString can only be allocated by the VM"); | 378 "_TwoByteString can only be allocated by the VM"); |
| 379 } | 379 } |
| 380 | 380 |
| 381 // Checks for one-byte whitespaces only. | 381 // Checks for one-byte whitespaces only. |
| 382 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 382 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 383 // whitespaces. Add checking for multi-byte whitespace codepoints. | 383 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 384 bool _isWhitespace(int codePoint) { | 384 bool _isWhitespace(int codePoint) { |
| 385 return | 385 return |
| 386 (codePoint === 32) || // Space. | 386 (codePoint === 32) || // Space. |
| 387 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 387 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 | 390 |
| 391 | 391 |
| 392 class FourByteString extends StringBase implements String { | 392 class _FourByteString extends _StringBase implements String { |
| 393 factory FourByteString._uninstantiable() { | 393 factory _FourByteString._uninstantiable() { |
| 394 throw const UnsupportedOperationException( | 394 throw const UnsupportedOperationException( |
| 395 "FourByteString can only be allocated by the VM"); | 395 "_FourByteString can only be allocated by the VM"); |
| 396 } | 396 } |
| 397 | 397 |
| 398 // Checks for one-byte whitespaces only. | 398 // Checks for one-byte whitespaces only. |
| 399 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 399 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 400 // whitespaces. Add checking for multi-byte whitespace codepoints. | 400 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 401 bool _isWhitespace(int codePoint) { | 401 bool _isWhitespace(int codePoint) { |
| 402 return | 402 return |
| 403 (codePoint === 32) || // Space. | 403 (codePoint === 32) || // Space. |
| 404 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 404 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 405 } | 405 } |
| 406 } | 406 } |
| 407 | 407 |
| 408 | 408 |
| 409 class ExternalOneByteString extends StringBase implements String { | 409 class _ExternalOneByteString extends _StringBase implements String { |
| 410 factory ExternalOneByteString._uninstantiable() { | 410 factory _ExternalOneByteString._uninstantiable() { |
| 411 throw const UnsupportedOperationException( | 411 throw const UnsupportedOperationException( |
| 412 "ExternalOneByteString can only be allocated by the VM"); | 412 "_ExternalOneByteString can only be allocated by the VM"); |
| 413 } | 413 } |
| 414 | 414 |
| 415 // Checks for one-byte whitespaces only. | 415 // Checks for one-byte whitespaces only. |
| 416 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 416 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 417 // whitespaces for one byte strings. | 417 // whitespaces for one byte strings. |
| 418 bool _isWhitespace(int codePoint) { | 418 bool _isWhitespace(int codePoint) { |
| 419 return | 419 return |
| 420 (codePoint === 32) || // Space. | 420 (codePoint === 32) || // Space. |
| 421 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 421 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 422 } | 422 } |
| 423 } | 423 } |
| 424 | 424 |
| 425 | 425 |
| 426 class ExternalTwoByteString extends StringBase implements String { | 426 class _ExternalTwoByteString extends _StringBase implements String { |
| 427 factory ExternalTwoByteString._uninstantiable() { | 427 factory ExternalTwoByteString._uninstantiable() { |
| 428 throw const UnsupportedOperationException( | 428 throw const UnsupportedOperationException( |
| 429 "ExternalTwoByteString can only be allocated by the VM"); | 429 "_ExternalTwoByteString can only be allocated by the VM"); |
| 430 } | 430 } |
| 431 | 431 |
| 432 // Checks for one-byte whitespaces only. | 432 // Checks for one-byte whitespaces only. |
| 433 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 433 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 434 // whitespaces. Add checking for multi-byte whitespace codepoints. | 434 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 435 bool _isWhitespace(int codePoint) { | 435 bool _isWhitespace(int codePoint) { |
| 436 return | 436 return |
| 437 (codePoint === 32) || // Space. | 437 (codePoint === 32) || // Space. |
| 438 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 438 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 439 } | 439 } |
| 440 } | 440 } |
| 441 | 441 |
| 442 | 442 |
| 443 class ExternalFourByteString extends StringBase implements String { | 443 class _ExternalFourByteString extends _StringBase implements String { |
| 444 factory ExternalFourByteString._uninstantiable() { | 444 factory _ExternalFourByteString._uninstantiable() { |
| 445 throw const UnsupportedOperationException( | 445 throw const UnsupportedOperationException( |
| 446 "ExternalFourByteString can only be allocated by the VM"); | 446 "ExternalFourByteString can only be allocated by the VM"); |
| 447 } | 447 } |
| 448 | 448 |
| 449 // Checks for one-byte whitespaces only. | 449 // Checks for one-byte whitespaces only. |
| 450 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 450 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 451 // whitespaces. Add checking for multi-byte whitespace codepoints. | 451 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 452 bool _isWhitespace(int codePoint) { | 452 bool _isWhitespace(int codePoint) { |
| 453 return | 453 return |
| 454 (codePoint === 32) || // Space. | 454 (codePoint === 32) || // Space. |
| (...skipping 24 matching lines...) Expand all 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 |