| 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 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 if ((startIndex < 0) || (startIndex > this.length)) { | 146 if ((startIndex < 0) || (startIndex > this.length)) { |
| 147 throw new IndexOutOfRangeException(startIndex); | 147 throw new IndexOutOfRangeException(startIndex); |
| 148 } | 148 } |
| 149 if ((endIndex < 0) || (endIndex > this.length)) { | 149 if ((endIndex < 0) || (endIndex > this.length)) { |
| 150 throw new IndexOutOfRangeException(endIndex); | 150 throw new IndexOutOfRangeException(endIndex); |
| 151 } | 151 } |
| 152 if (startIndex > endIndex) { | 152 if (startIndex > endIndex) { |
| 153 throw new IndexOutOfRangeException(startIndex); | 153 throw new IndexOutOfRangeException(startIndex); |
| 154 } | 154 } |
| 155 return substringUnchecked_(startIndex, endIndex); | 155 return _substringUnchecked(startIndex, endIndex); |
| 156 } | 156 } |
| 157 | 157 |
| 158 String substringUnchecked_(int startIndex, int endIndex) { | 158 String _substringUnchecked(int startIndex, int endIndex) |
| 159 int len = endIndex - startIndex; | 159 native "StringBase_substringUnchecked"; |
| 160 List<int> charCodes = new List<int>(len); | |
| 161 for (int i = 0; i < len; i++) { | |
| 162 charCodes[i] = this.charCodeAt(startIndex + i); | |
| 163 } | |
| 164 return StringBase.createFromCharCodes(charCodes); | |
| 165 } | |
| 166 | 160 |
| 167 String trim() { | 161 String trim() { |
| 168 final int len = this.length; | 162 final int len = this.length; |
| 169 int first = 0; | 163 int first = 0; |
| 170 for (; first < len; first++) { | 164 for (; first < len; first++) { |
| 171 if (!_isWhitespace(this.charCodeAt(first))) { | 165 if (!_isWhitespace(this.charCodeAt(first))) { |
| 172 break; | 166 break; |
| 173 } | 167 } |
| 174 } | 168 } |
| 175 if (len == first) { | 169 if (len == first) { |
| 176 // String contains only whitespaces. | 170 // String contains only whitespaces. |
| 177 return ""; | 171 return ""; |
| 178 } | 172 } |
| 179 int last = len - 1; | 173 int last = len - 1; |
| 180 for (; last >= first; last--) { | 174 for (; last >= first; last--) { |
| 181 if (!_isWhitespace(this.charCodeAt(last))) { | 175 if (!_isWhitespace(this.charCodeAt(last))) { |
| 182 break; | 176 break; |
| 183 } | 177 } |
| 184 } | 178 } |
| 185 if ((first == 0) && (last == (len - 1))) { | 179 if ((first == 0) && (last == (len - 1))) { |
| 186 // Returns this string if it does not have leading or trailing | 180 // Returns this string if it does not have leading or trailing |
| 187 // whitespaces. | 181 // whitespaces. |
| 188 return this; | 182 return this; |
| 189 } else { | 183 } else { |
| 190 return substringUnchecked_(first, last + 1); | 184 return _substringUnchecked(first, last + 1); |
| 191 } | 185 } |
| 192 } | 186 } |
| 193 | 187 |
| 194 bool contains(Pattern pattern, [int startIndex = 0]) { | 188 bool contains(Pattern pattern, [int startIndex = 0]) { |
| 195 if (pattern is String) { | 189 if (pattern is String) { |
| 196 return indexOf(pattern, startIndex) >= 0; | 190 return indexOf(pattern, startIndex) >= 0; |
| 197 } | 191 } |
| 198 return pattern.allMatches(this.substring(startIndex)).iterator().hasNext(); | 192 return pattern.allMatches(this.substring(startIndex)).iterator().hasNext(); |
| 199 } | 193 } |
| 200 | 194 |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 for (int g in groups) { | 491 for (int g in groups) { |
| 498 result.add(group(g)); | 492 result.add(group(g)); |
| 499 } | 493 } |
| 500 return result; | 494 return result; |
| 501 } | 495 } |
| 502 | 496 |
| 503 final int _start; | 497 final int _start; |
| 504 final String str; | 498 final String str; |
| 505 final String pattern; | 499 final String pattern; |
| 506 } | 500 } |
| OLD | NEW |