| 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 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 static String _createFromCodePoints(_ObjectArray<int> codePoints) | 36 static String _createFromCodePoints(_ObjectArray<int> codePoints) |
| 37 native "StringBase_createFromCodePoints"; | 37 native "StringBase_createFromCodePoints"; |
| 38 | 38 |
| 39 String operator [](int index) native "String_charAt"; | 39 String operator [](int index) native "String_charAt"; |
| 40 | 40 |
| 41 int charCodeAt(int index) native "String_charCodeAt"; | 41 int charCodeAt(int index) native "String_charCodeAt"; |
| 42 | 42 |
| 43 int get length native "String_getLength"; | 43 int get length native "String_getLength"; |
| 44 | 44 |
| 45 bool isEmpty() { | 45 bool get isEmpty { |
| 46 return this.length === 0; | 46 return this.length === 0; |
| 47 } | 47 } |
| 48 | 48 |
| 49 String concat(String other) native "String_concat"; | 49 String concat(String other) native "String_concat"; |
| 50 | 50 |
| 51 String toString() { | 51 String toString() { |
| 52 return this; | 52 return this; |
| 53 } | 53 } |
| 54 | 54 |
| 55 bool operator ==(Object other) { | 55 bool operator ==(Object other) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 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 _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 _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 (_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 (_substringMatches(index, other)) { | 136 if (_substringMatches(index, other)) { |
| 137 return index; | 137 return index; |
| 138 } | 138 } |
| 139 } | 139 } |
| (...skipping 339 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 |