Chromium Code Reviews| 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 patch class StringImplementation { | 5 patch class StringImplementation { |
| 6 /* patch */ static String _fromCharCodes(List<int> charCodes) { | 6 /* patch */ static String _fromCharCodes(List<int> charCodes) { |
| 7 return StringBase.createFromCharCodes(charCodes); | 7 return StringBase.createFromCharCodes(charCodes); |
| 8 } | 8 } |
| 9 | 9 |
| 10 /* patch */ static String join(List<String> strings, String separator) { | 10 /* patch */ static String join(List<String> strings, String separator) { |
| 11 return StringBase.join(strings, separator); | 11 return StringBase.join(strings, separator); |
| 12 } | 12 } |
| 13 | 13 |
| 14 /* patch */ static String concatAll(List<String> strings) { | 14 /* patch */ static String concatAll(List<String> strings) { |
| 15 return StringBase.concatAll(strings); | 15 return StringBase.concatAll(strings); |
| 16 } | 16 } |
| 17 } | 17 } |
| 18 | |
| 19 /** | |
| 20 * [StringBase] contains common methods used by concrete String implementations, | |
| 21 * e.g., OneByteString. | |
| 22 */ | |
| 23 class StringBase { | |
|
Mads Ager (google)
2012/08/27 12:55:03
If this is in a patch file, doesn't it have to be
| |
| 24 | |
| 25 factory StringBase._uninstantiable() { | |
| 26 throw const UnsupportedOperationException( | |
| 27 "StringBase can't be instaniated"); | |
| 28 } | |
| 29 | |
| 30 int hashCode() native "String_hashCode"; | |
| 31 | |
| 32 /** | |
| 33 * Create the most efficient string representation for specified | |
| 34 * [codePoints]. | |
| 35 */ | |
| 36 static String createFromCharCodes(List<int> charCodes) { | |
| 37 ObjectArray objectArray; | |
| 38 if (charCodes is ObjectArray) { | |
| 39 objectArray = charCodes; | |
| 40 } else { | |
| 41 int len = charCodes.length; | |
| 42 objectArray = new ObjectArray(len); | |
| 43 for (int i = 0; i < len; i++) { | |
| 44 objectArray[i] = charCodes[i]; | |
| 45 } | |
| 46 } | |
| 47 return _createFromCodePoints(objectArray); | |
| 48 } | |
| 49 | |
| 50 static String _createFromCodePoints(ObjectArray<int> codePoints) | |
| 51 native "StringBase_createFromCodePoints"; | |
| 52 | |
| 53 String operator [](int index) native "String_charAt"; | |
| 54 | |
| 55 int charCodeAt(int index) native "String_charCodeAt"; | |
| 56 | |
| 57 int get length() native "String_getLength"; | |
| 58 | |
| 59 bool isEmpty() { | |
| 60 return this.length === 0; | |
| 61 } | |
| 62 | |
| 63 String concat(String other) native "String_concat"; | |
| 64 | |
| 65 String toString() { | |
| 66 return this; | |
| 67 } | |
| 68 | |
| 69 bool operator ==(Object other) { | |
| 70 if (this === other) { | |
| 71 return true; | |
| 72 } | |
| 73 if ((other is !String) || | |
| 74 (this.length != other.length)) { | |
| 75 // TODO(5413632): Compare hash codes when both are present. | |
| 76 return false; | |
| 77 } | |
| 78 return this.compareTo(other) === 0; | |
| 79 } | |
| 80 | |
| 81 int compareTo(String other) { | |
| 82 int thisLength = this.length; | |
| 83 int otherLength = other.length; | |
| 84 int len = (thisLength < otherLength) ? thisLength : otherLength; | |
| 85 for (int i = 0; i < len; i++) { | |
| 86 int thisCodePoint = this.charCodeAt(i); | |
| 87 int otherCodePoint = other.charCodeAt(i); | |
| 88 if (thisCodePoint < otherCodePoint) { | |
| 89 return -1; | |
| 90 } | |
| 91 if (thisCodePoint > otherCodePoint) { | |
| 92 return 1; | |
| 93 } | |
| 94 } | |
| 95 if (thisLength < otherLength) return -1; | |
| 96 if (thisLength > otherLength) return 1; | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 bool substringMatches(int start, String other) { | |
| 101 if (other.isEmpty()) return true; | |
| 102 if ((start < 0) || (start >= this.length)) { | |
| 103 return false; | |
| 104 } | |
| 105 final int len = other.length; | |
| 106 if ((start + len) > this.length) { | |
| 107 return false; | |
| 108 } | |
| 109 for (int i = 0; i < len; i++) { | |
| 110 if (this.charCodeAt(i + start) != other.charCodeAt(i)) { | |
| 111 return false; | |
| 112 } | |
| 113 } | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool endsWith(String other) { | |
| 118 return this.substringMatches(this.length - other.length, other); | |
| 119 } | |
| 120 | |
| 121 bool startsWith(String other) { | |
| 122 return this.substringMatches(0, other); | |
| 123 } | |
| 124 | |
| 125 int indexOf(String other, [int start = 0]) { | |
| 126 if (other.isEmpty()) { | |
| 127 return start < this.length ? start : this.length; | |
| 128 } | |
| 129 if ((start < 0) || (start >= this.length)) { | |
| 130 return -1; | |
| 131 } | |
| 132 int len = this.length - other.length + 1; | |
| 133 for (int index = start; index < len; index++) { | |
| 134 if (this.substringMatches(index, other)) { | |
| 135 return index; | |
| 136 } | |
| 137 } | |
| 138 return -1; | |
| 139 } | |
| 140 | |
| 141 int lastIndexOf(String other, [int start = null]) { | |
| 142 if (start == null) start = length - 1; | |
| 143 if (other.isEmpty()) { | |
| 144 return min(this.length, start); | |
| 145 } | |
| 146 if (start >= this.length) { | |
| 147 start = this.length - 1; | |
| 148 } | |
| 149 for (int index = start; index >= 0; index--) { | |
| 150 if (this.substringMatches(index, other)) { | |
| 151 return index; | |
| 152 } | |
| 153 } | |
| 154 return -1; | |
| 155 } | |
| 156 | |
| 157 String substring(int startIndex, [int endIndex]) { | |
| 158 if (endIndex === null) endIndex = this.length; | |
| 159 | |
| 160 if ((startIndex < 0) || (startIndex > this.length)) { | |
| 161 throw new IndexOutOfRangeException(startIndex); | |
| 162 } | |
| 163 if ((endIndex < 0) || (endIndex > this.length)) { | |
| 164 throw new IndexOutOfRangeException(endIndex); | |
| 165 } | |
| 166 if (startIndex > endIndex) { | |
| 167 throw new IndexOutOfRangeException(startIndex); | |
| 168 } | |
| 169 return _substringUnchecked(startIndex, endIndex); | |
| 170 } | |
| 171 | |
| 172 String _substringUnchecked(int startIndex, int endIndex) | |
| 173 native "StringBase_substringUnchecked"; | |
| 174 | |
| 175 String trim() { | |
| 176 final int len = this.length; | |
| 177 int first = 0; | |
| 178 for (; first < len; first++) { | |
| 179 if (!_isWhitespace(this.charCodeAt(first))) { | |
| 180 break; | |
| 181 } | |
| 182 } | |
| 183 if (len == first) { | |
| 184 // String contains only whitespaces. | |
| 185 return ""; | |
| 186 } | |
| 187 int last = len - 1; | |
| 188 for (; last >= first; last--) { | |
| 189 if (!_isWhitespace(this.charCodeAt(last))) { | |
| 190 break; | |
| 191 } | |
| 192 } | |
| 193 if ((first == 0) && (last == (len - 1))) { | |
| 194 // Returns this string if it does not have leading or trailing | |
| 195 // whitespaces. | |
| 196 return this; | |
| 197 } else { | |
| 198 return _substringUnchecked(first, last + 1); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 bool contains(Pattern pattern, [int startIndex = 0]) { | |
| 203 if (pattern is String) { | |
| 204 return indexOf(pattern, startIndex) >= 0; | |
| 205 } | |
| 206 return pattern.allMatches(this.substring(startIndex)).iterator().hasNext(); | |
| 207 } | |
| 208 | |
| 209 String replaceFirst(Pattern pattern, String replacement) { | |
| 210 if (pattern is! Pattern) { | |
| 211 throw new IllegalArgumentException("${pattern} is not a Pattern"); | |
| 212 } | |
| 213 if (replacement is! String) { | |
| 214 throw new IllegalArgumentException("${replacement} is not a String"); | |
| 215 } | |
| 216 StringBuffer buffer = new StringBuffer(); | |
| 217 int startIndex = 0; | |
| 218 Iterator iterator = pattern.allMatches(this).iterator(); | |
| 219 if (iterator.hasNext()) { | |
| 220 Match match = iterator.next(); | |
| 221 buffer.add(this.substring(startIndex, match.start())).add(replacement); | |
| 222 startIndex = match.end(); | |
| 223 } | |
| 224 return buffer.add(this.substring(startIndex)).toString(); | |
| 225 } | |
| 226 | |
| 227 String replaceAll(Pattern pattern, String replacement) { | |
| 228 if (pattern is! Pattern) { | |
| 229 throw new IllegalArgumentException("${pattern} is not a Pattern"); | |
| 230 } | |
| 231 if (replacement is! String) { | |
| 232 throw new IllegalArgumentException("${replacement} is not a String"); | |
| 233 } | |
| 234 StringBuffer buffer = new StringBuffer(); | |
| 235 int startIndex = 0; | |
| 236 for (Match match in pattern.allMatches(this)) { | |
| 237 buffer.add(this.substring(startIndex, match.start())).add(replacement); | |
| 238 startIndex = match.end(); | |
| 239 } | |
| 240 return buffer.add(this.substring(startIndex)).toString(); | |
| 241 } | |
| 242 | |
| 243 /** | |
| 244 * Convert all objects in [values] to strings and concat them | |
| 245 * into a result string. | |
| 246 */ | |
| 247 static String _interpolate(List values) { | |
| 248 int numValues = values.length; | |
| 249 var stringList = new ObjectArray(numValues); | |
| 250 for (int i = 0; i < numValues; i++) { | |
| 251 stringList[i] = values[i].toString(); | |
| 252 } | |
| 253 return _concatAll(stringList); | |
| 254 } | |
| 255 | |
| 256 Iterable<Match> allMatches(String str) { | |
| 257 List<Match> result = new List<Match>(); | |
| 258 int length = str.length; | |
| 259 int patternLength = this.length; | |
| 260 int startIndex = 0; | |
| 261 while (true) { | |
| 262 int position = str.indexOf(this, startIndex); | |
| 263 if (position == -1) { | |
| 264 break; | |
| 265 } | |
| 266 result.add(new _StringMatch(position, str, this)); | |
| 267 int endIndex = position + patternLength; | |
| 268 if (endIndex == length) { | |
| 269 break; | |
| 270 } else if (position == endIndex) { | |
| 271 ++startIndex; // empty match, advance and restart | |
| 272 } else { | |
| 273 startIndex = endIndex; | |
| 274 } | |
| 275 } | |
| 276 return result; | |
| 277 } | |
| 278 | |
| 279 List<String> split(Pattern pattern) { | |
| 280 int length = this.length; | |
| 281 Iterator iterator = pattern.allMatches(this).iterator(); | |
| 282 if (length == 0 && iterator.hasNext()) { | |
| 283 // A matched empty string input returns the empty list. | |
| 284 return <String>[]; | |
| 285 } | |
| 286 List<String> result = new List<String>(); | |
| 287 int startIndex = 0; | |
| 288 int previousIndex = 0; | |
| 289 while (true) { | |
| 290 if (startIndex == length || !iterator.hasNext()) { | |
| 291 result.add(this.substring(previousIndex, length)); | |
| 292 break; | |
| 293 } | |
| 294 Match match = iterator.next(); | |
| 295 if (match.start() == length) { | |
| 296 result.add(this.substring(previousIndex, length)); | |
| 297 break; | |
| 298 } | |
| 299 int endIndex = match.end(); | |
| 300 if (startIndex == endIndex && endIndex == previousIndex) { | |
| 301 ++startIndex; // empty match, advance and restart | |
| 302 continue; | |
| 303 } | |
| 304 result.add(this.substring(previousIndex, match.start())); | |
| 305 startIndex = previousIndex = endIndex; | |
| 306 } | |
| 307 return result; | |
| 308 } | |
| 309 | |
| 310 List<String> splitChars() { | |
| 311 int len = this.length; | |
| 312 final result = new List<String>(len); | |
| 313 for (int i = 0; i < len; i++) { | |
| 314 result[i] = this[i]; | |
| 315 } | |
| 316 return result; | |
| 317 } | |
| 318 | |
| 319 List<int> charCodes() { | |
| 320 int len = this.length; | |
| 321 final result = new List<int>(len); | |
| 322 for (int i = 0; i < len; i++) { | |
| 323 result[i] = this.charCodeAt(i); | |
| 324 } | |
| 325 return result; | |
| 326 } | |
| 327 | |
| 328 String toUpperCase() native "String_toUpperCase"; | |
| 329 | |
| 330 String toLowerCase() native "String_toLowerCase"; | |
| 331 | |
| 332 // Implementations of Strings methods follow below. | |
| 333 static String join(List<String> strings, String separator) { | |
| 334 final int length = strings.length; | |
| 335 if (length === 0) { | |
| 336 return ""; | |
| 337 } | |
| 338 | |
| 339 List stringsList = strings; | |
| 340 if (separator.length != 0) { | |
| 341 stringsList = new List(2 * length - 1); | |
| 342 stringsList[0] = strings[0]; | |
| 343 int j = 1; | |
| 344 for (int i = 1; i < length; i++) { | |
| 345 stringsList[j++] = separator; | |
| 346 stringsList[j++] = strings[i]; | |
| 347 } | |
| 348 } | |
| 349 return concatAll(stringsList); | |
| 350 } | |
| 351 | |
| 352 static String concatAll(List<String> strings) { | |
| 353 ObjectArray stringsArray; | |
| 354 if (strings is ObjectArray) { | |
| 355 stringsArray = strings; | |
| 356 } else { | |
| 357 int len = strings.length; | |
| 358 stringsArray = new ObjectArray(len); | |
| 359 for (int i = 0; i < len; i++) { | |
| 360 stringsArray[i] = strings[i]; | |
| 361 } | |
| 362 } | |
| 363 return _concatAll(stringsArray); | |
| 364 } | |
| 365 | |
| 366 static String _concatAll(ObjectArray<String> strings) | |
| 367 native "Strings_concatAll"; | |
| 368 } | |
| 369 | |
| 370 | |
| 371 class OneByteString extends StringBase implements String { | |
| 372 factory OneByteString._uninstantiable() { | |
| 373 throw const UnsupportedOperationException( | |
| 374 "OneByteString can only be allocated by the VM"); | |
| 375 } | |
| 376 | |
| 377 // Checks for one-byte whitespaces only. | |
| 378 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 379 // whitespaces for one byte strings. | |
| 380 bool _isWhitespace(int codePoint) { | |
| 381 return | |
| 382 (codePoint === 32) || // Space. | |
| 383 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 384 } | |
| 385 | |
| 386 } | |
| 387 | |
| 388 | |
| 389 class TwoByteString extends StringBase implements String { | |
| 390 factory TwoByteString._uninstantiable() { | |
| 391 throw const UnsupportedOperationException( | |
| 392 "TwoByteString can only be allocated by the VM"); | |
| 393 } | |
| 394 | |
| 395 // Checks for one-byte whitespaces only. | |
| 396 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 397 // whitespaces. Add checking for multi-byte whitespace codepoints. | |
| 398 bool _isWhitespace(int codePoint) { | |
| 399 return | |
| 400 (codePoint === 32) || // Space. | |
| 401 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 402 } | |
| 403 } | |
| 404 | |
| 405 | |
| 406 class FourByteString extends StringBase implements String { | |
| 407 factory FourByteString._uninstantiable() { | |
| 408 throw const UnsupportedOperationException( | |
| 409 "FourByteString can only be allocated by the VM"); | |
| 410 } | |
| 411 | |
| 412 // Checks for one-byte whitespaces only. | |
| 413 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 414 // whitespaces. Add checking for multi-byte whitespace codepoints. | |
| 415 bool _isWhitespace(int codePoint) { | |
| 416 return | |
| 417 (codePoint === 32) || // Space. | |
| 418 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 | |
| 423 class ExternalOneByteString extends StringBase implements String { | |
| 424 factory ExternalOneByteString._uninstantiable() { | |
| 425 throw const UnsupportedOperationException( | |
| 426 "ExternalOneByteString can only be allocated by the VM"); | |
| 427 } | |
| 428 | |
| 429 // Checks for one-byte whitespaces only. | |
| 430 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 431 // whitespaces for one byte strings. | |
| 432 bool _isWhitespace(int codePoint) { | |
| 433 return | |
| 434 (codePoint === 32) || // Space. | |
| 435 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 436 } | |
| 437 } | |
| 438 | |
| 439 | |
| 440 class ExternalTwoByteString extends StringBase implements String { | |
| 441 factory ExternalTwoByteString._uninstantiable() { | |
| 442 throw const UnsupportedOperationException( | |
| 443 "ExternalTwoByteString can only be allocated by the VM"); | |
| 444 } | |
| 445 | |
| 446 // Checks for one-byte whitespaces only. | |
| 447 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 448 // whitespaces. Add checking for multi-byte whitespace codepoints. | |
| 449 bool _isWhitespace(int codePoint) { | |
| 450 return | |
| 451 (codePoint === 32) || // Space. | |
| 452 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 | |
| 457 class ExternalFourByteString extends StringBase implements String { | |
| 458 factory ExternalFourByteString._uninstantiable() { | |
| 459 throw const UnsupportedOperationException( | |
| 460 "ExternalFourByteString can only be allocated by the VM"); | |
| 461 } | |
| 462 | |
| 463 // Checks for one-byte whitespaces only. | |
| 464 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 465 // whitespaces. Add checking for multi-byte whitespace codepoints. | |
| 466 bool _isWhitespace(int codePoint) { | |
| 467 return | |
| 468 (codePoint === 32) || // Space. | |
| 469 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 470 } | |
| 471 } | |
| 472 | |
| 473 | |
| 474 class _StringMatch implements Match { | |
| 475 const _StringMatch(int this._start, | |
| 476 String this.str, | |
| 477 String this.pattern); | |
| 478 | |
| 479 int start() => _start; | |
| 480 int end() => _start + pattern.length; | |
| 481 String operator[](int g) => group(g); | |
| 482 int groupCount() => 0; | |
| 483 | |
| 484 String group(int group) { | |
| 485 if (group != 0) { | |
| 486 throw new IndexOutOfRangeException(group); | |
| 487 } | |
| 488 return pattern; | |
| 489 } | |
| 490 | |
| 491 List<String> groups(List<int> groups) { | |
| 492 List<String> result = new List<String>(); | |
| 493 for (int g in groups) { | |
| 494 result.add(group(g)); | |
| 495 } | |
| 496 return result; | |
| 497 } | |
| 498 | |
| 499 final int _start; | |
| 500 final String str; | |
| 501 final String pattern; | |
| 502 } | |
| OLD | NEW |