| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 String substringUnchecked_(int startIndex, int endIndex) { | 153 String substringUnchecked_(int startIndex, int endIndex) { |
| 154 int len = endIndex - startIndex; | 154 int len = endIndex - startIndex; |
| 155 List<int> charCodes = new List<int>(len); | 155 List<int> charCodes = new List<int>(len); |
| 156 for (int i = 0; i < len; i++) { | 156 for (int i = 0; i < len; i++) { |
| 157 charCodes[i] = this.charCodeAt(startIndex + i); | 157 charCodes[i] = this.charCodeAt(startIndex + i); |
| 158 } | 158 } |
| 159 return StringBase.createFromCharCodes(charCodes); | 159 return StringBase.createFromCharCodes(charCodes); |
| 160 } | 160 } |
| 161 | 161 |
| 162 String trim() { | 162 String trim() { |
| 163 int len = this.length; | 163 final int len = this.length; |
| 164 int first = 0; | 164 int first = 0; |
| 165 for (; first < len; first++) { | 165 for (; first < len; first++) { |
| 166 if (!_isWhitespace(this.charCodeAt(first))) { | 166 if (!_isWhitespace(this.charCodeAt(first))) { |
| 167 break; | 167 break; |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 if (len == first) { | 170 if (len == first) { |
| 171 // String contains only whitespaces. | 171 // String contains only whitespaces. |
| 172 return ""; | 172 return ""; |
| 173 } | 173 } |
| 174 int last = len - 1; | 174 int last = len - 1; |
| 175 for (int i = last; last >= first; last--) { | 175 for (; last >= first; last--) { |
| 176 if (!_isWhitespace(this.charCodeAt(last))) { | 176 if (!_isWhitespace(this.charCodeAt(last))) { |
| 177 break; | 177 break; |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 return substringUnchecked_(first, last + 1); | 180 if ((first == 0) && (last == (len - 1))) { |
| 181 // Returns this string if it does not have leading or trailing |
| 182 // whitespaces. |
| 183 return this; |
| 184 } else { |
| 185 return substringUnchecked_(first, last + 1); |
| 186 } |
| 181 } | 187 } |
| 182 | 188 |
| 183 bool contains(Pattern other, [int startIndex = 0]) { | 189 bool contains(Pattern other, [int startIndex = 0]) { |
| 184 if (other is String) { | 190 if (other is String) { |
| 185 return indexOf(other, startIndex) >= 0; | 191 return indexOf(other, startIndex) >= 0; |
| 186 } | 192 } |
| 187 return other.allMatches(this.substring(startIndex)).iterator().hasNext(); | 193 return other.allMatches(this.substring(startIndex)).iterator().hasNext(); |
| 188 } | 194 } |
| 189 | 195 |
| 190 String replaceFirst(Pattern from, String to) { | 196 String replaceFirst(Pattern from, String to) { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 for (int g in groups) { | 442 for (int g in groups) { |
| 437 result.add(group(g)); | 443 result.add(group(g)); |
| 438 } | 444 } |
| 439 return result; | 445 return result; |
| 440 } | 446 } |
| 441 | 447 |
| 442 final int _start; | 448 final int _start; |
| 443 final String str; | 449 final String str; |
| 444 final String pattern; | 450 final String pattern; |
| 445 } | 451 } |
| OLD | NEW |