| 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 String { | 5 patch class String { |
| 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { | 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { |
| 7 return _StringBase.createFromCharCodes(charCodes); | 7 return _StringBase.createFromCharCodes(charCodes); |
| 8 } | 8 } |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 int get hashCode native "String_getHashCode"; | 23 int get hashCode native "String_getHashCode"; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Create the most efficient string representation for specified | 26 * Create the most efficient string representation for specified |
| 27 * [codePoints]. | 27 * [codePoints]. |
| 28 */ | 28 */ |
| 29 static String createFromCharCodes(Iterable<int> charCodes) { | 29 static String createFromCharCodes(Iterable<int> charCodes) { |
| 30 if (charCodes != null) { | 30 if (charCodes != null) { |
| 31 // TODO(srdjan): Also skip copying of typed arrays. | 31 // TODO(srdjan): Also skip copying of typed arrays. |
| 32 if (charCodes is! _ObjectArray && | 32 final ccid = charCodes._cid; |
| 33 charCodes is! _GrowableObjectArray && | 33 if ((ccid != _ObjectArray._classId) && |
| 34 charCodes is! _ImmutableArray) { | 34 (ccid != _GrowableObjectArray._classId) && |
| 35 (ccid != _ImmutableArray._classId)) { |
| 35 charCodes = new List<int>.from(charCodes, growable: false); | 36 charCodes = new List<int>.from(charCodes, growable: false); |
| 36 } | 37 } |
| 37 | 38 |
| 38 bool isOneByteString = true; | 39 bool isOneByteString = true; |
| 39 for (int i = 0; i < charCodes.length; i++) { | 40 for (int i = 0; i < charCodes.length; i++) { |
| 40 int e = charCodes[i]; | 41 int e = charCodes[i]; |
| 41 if (e is! int) throw new ArgumentError(e); | 42 if (e is! _Smi) throw new ArgumentError(e); |
| 42 // Is e Latin1? | 43 // Is e Latin1? |
| 43 if ((e < 0) || (e > 0xFF)) { | 44 if ((e < 0) || (e > 0xFF)) { |
| 44 isOneByteString = false; | 45 isOneByteString = false; |
| 45 break; | 46 break; |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 if (isOneByteString) { | 49 if (isOneByteString) { |
| 49 var s = _OneByteString._allocate(charCodes.length); | 50 var s = _OneByteString._allocate(charCodes.length); |
| 50 for (int i = 0; i < charCodes.length; i++) { | 51 for (int i = 0; i < charCodes.length; i++) { |
| 51 s._setAt(i, charCodes[i]); | 52 s._setAt(i, charCodes[i]); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 * Convert all objects in [values] to strings and concat them | 336 * Convert all objects in [values] to strings and concat them |
| 336 * into a result string. | 337 * into a result string. |
| 337 */ | 338 */ |
| 338 static String _interpolate(List values) { | 339 static String _interpolate(List values) { |
| 339 final int numValues = values.length; | 340 final int numValues = values.length; |
| 340 _ObjectArray stringList = new List(numValues); | 341 _ObjectArray stringList = new List(numValues); |
| 341 bool isOneByteString = true; | 342 bool isOneByteString = true; |
| 342 int totalLength = 0; | 343 int totalLength = 0; |
| 343 for (int i = 0; i < numValues; i++) { | 344 for (int i = 0; i < numValues; i++) { |
| 344 var s = values[i].toString(); | 345 var s = values[i].toString(); |
| 345 if (isOneByteString && (s is _OneByteString)) { | 346 if (isOneByteString && (s._cid == _OneByteString._classId)) { |
| 346 totalLength += s.length; | 347 totalLength += s.length; |
| 347 } else { | 348 } else { |
| 348 isOneByteString = false; | 349 isOneByteString = false; |
| 349 } | 350 } |
| 350 stringList[i] = s; | 351 stringList[i] = s; |
| 351 } | 352 } |
| 352 if (isOneByteString) { | 353 if (isOneByteString) { |
| 353 return _OneByteString._concatAll(stringList, totalLength); | 354 return _OneByteString._concatAll(stringList, totalLength); |
| 354 } | 355 } |
| 355 return _concatAllNative(stringList); | 356 return _concatAllNative(stringList); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 stringsList.add(string); | 442 stringsList.add(string); |
| 442 } | 443 } |
| 443 return concatAll(stringsList); | 444 return concatAll(stringsList); |
| 444 } | 445 } |
| 445 | 446 |
| 446 static String concatAll(Iterable<String> strings) { | 447 static String concatAll(Iterable<String> strings) { |
| 447 _ObjectArray stringsArray; | 448 _ObjectArray stringsArray; |
| 448 final len = strings.length; | 449 final len = strings.length; |
| 449 bool isOneByteString = true; | 450 bool isOneByteString = true; |
| 450 int totalLength = 0; | 451 int totalLength = 0; |
| 451 if (strings is _ObjectArray) { | 452 if (strings._cid == _ObjectArray._clCId) { |
| 452 stringsArray = strings; | 453 stringsArray = strings; |
| 453 for (int i = 0; i < len; i++) { | 454 for (int i = 0; i < len; i++) { |
| 454 var string = strings[i]; | 455 var string = strings[i]; |
| 455 if (string is _OneByteString) { | 456 if (string._cid == _OneByteString._classId) { |
| 456 totalLength += string.length; | 457 totalLength += string.length; |
| 457 } else { | 458 } else { |
| 458 isOneByteString = false; | 459 isOneByteString = false; |
| 459 if (string is! String) throw new ArgumentError(string); | 460 if (string is! String) throw new ArgumentError(string); |
| 460 } | 461 } |
| 461 } | 462 } |
| 462 } else { | 463 } else { |
| 463 // Copy into an _ObjectArray. | 464 // Copy into an _ObjectArray. |
| 464 stringsArray = new _ObjectArray(len); | 465 stringsArray = new _ObjectArray(len); |
| 465 int i = 0; | 466 int i = 0; |
| 466 for (int i = 0; i < len; i++) { | 467 for (int i = 0; i < len; i++) { |
| 467 var string = strings[i]; | 468 var string = strings[i]; |
| 468 if (string is _OneByteString) { | 469 if (string._cid == _OneByteString.clCid) { |
| 469 totalLength += s.length; | 470 totalLength += s.length; |
| 470 } else { | 471 } else { |
| 471 isOneByteString = false; | 472 isOneByteString = false; |
| 472 if (string is! String) throw new ArgumentError(string); | 473 if (string is! String) throw new ArgumentError(string); |
| 473 } | 474 } |
| 474 stringsArray[i++] = string; | 475 stringsArray[i++] = string; |
| 475 } | 476 } |
| 476 } | 477 } |
| 477 if (isOneByteString) { | 478 if (isOneByteString) { |
| 478 return _OneByteString._concatAll(stringsArray, totalLength); | 479 return _OneByteString._concatAll(stringsArray, totalLength); |
| 479 } | 480 } |
| 480 return _concatAllNative(stringsArray); | 481 return _concatAllNative(stringsArray); |
| 481 } | 482 } |
| 482 | 483 |
| 483 static String _concatAll(_ObjectArray<String> strings) { | 484 static String _concatAll(_ObjectArray<String> strings) { |
| 484 int totalLength = 0; | 485 int totalLength = 0; |
| 485 final stringsLength = strings.length; | 486 final stringsLength = strings.length; |
| 486 for (int i = 0; i < stringsLength; i++) { | 487 for (int i = 0; i < stringsLength; i++) { |
| 487 var e = strings[i]; | 488 var e = strings[i]; |
| 488 if (e is! _OneByteString) { | 489 if (e._cid != _OneByteString._classId) { |
| 489 return _concatAllNative(strings); | 490 return _concatAllNative(strings); |
| 490 } | 491 } |
| 491 totalLength += e.length; | 492 totalLength += e.length; |
| 492 } | 493 } |
| 493 return _OneByteString._concatAll(strings, totalLength); | 494 return _OneByteString._concatAll(strings, totalLength); |
| 494 } | 495 } |
| 495 | 496 |
| 496 // Call this method if not all list elements are OneByteString-s. | 497 // Call this method if not all list elements are OneByteString-s. |
| 497 static String _concatAllNative(_ObjectArray<String> strings) | 498 static String _concatAllNative(_ObjectArray<String> strings) |
| 498 native "Strings_concatAll"; | 499 native "Strings_concatAll"; |
| 499 } | 500 } |
| 500 | 501 |
| 501 | 502 |
| 502 class _OneByteString extends _StringBase implements String { | 503 class _OneByteString extends _StringBase implements String { |
| 504 static final int _classId = "A"._cid; |
| 505 |
| 503 factory _OneByteString._uninstantiable() { | 506 factory _OneByteString._uninstantiable() { |
| 504 throw new UnsupportedError( | 507 throw new UnsupportedError( |
| 505 "_OneByteString can only be allocated by the VM"); | 508 "_OneByteString can only be allocated by the VM"); |
| 506 } | 509 } |
| 507 | 510 |
| 508 int get hashCode native "String_getHashCode"; | 511 int get hashCode native "String_getHashCode"; |
| 509 | 512 |
| 510 // Checks for one-byte whitespaces only. | 513 // Checks for one-byte whitespaces only. |
| 511 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 514 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 512 // whitespaces for one byte strings. | 515 // whitespaces for one byte strings. |
| 513 bool _isWhitespace(int codePoint) { | 516 bool _isWhitespace(int codePoint) { |
| 514 return | 517 return |
| 515 (codePoint == 32) || // Space. | 518 (codePoint == 32) || // Space. |
| 516 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 519 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 517 } | 520 } |
| 518 | 521 |
| 519 String _substringUncheckedNative(int startIndex, int endIndex) | 522 String _substringUncheckedNative(int startIndex, int endIndex) |
| 520 native "OneByteString_substringUnchecked"; | 523 native "OneByteString_substringUnchecked"; |
| 521 | 524 |
| 522 List<String> _splitWithCharCode(int charCode) | 525 List<String> _splitWithCharCode(int charCode) |
| 523 native "OneByteString_splitWithCharCode"; | 526 native "OneByteString_splitWithCharCode"; |
| 524 | 527 |
| 525 List<String> split(Pattern pattern) { | 528 List<String> split(Pattern pattern) { |
| 526 if ((pattern is _OneByteString) && (pattern.length == 1)) { | 529 if ((pattern._cid == _OneByteString._classId) && (pattern.length == 1)) { |
| 527 return _splitWithCharCode(pattern.codeUnitAt(0)); | 530 return _splitWithCharCode(pattern.codeUnitAt(0)); |
| 528 } | 531 } |
| 529 return super.split(pattern); | 532 return super.split(pattern); |
| 530 } | 533 } |
| 531 | 534 |
| 532 // All element of 'strings' must be OneByteStrings. | 535 // All element of 'strings' must be OneByteStrings. |
| 533 static _concatAll(_ObjectArray<String> strings, int totalLength) { | 536 static _concatAll(_ObjectArray<String> strings, int totalLength) { |
| 534 // TODO(srdjan): Improve code below and raise or eliminate the limit. | 537 // TODO(srdjan): Improve code below and raise or eliminate the limit. |
| 535 if (totalLength > 128) { | 538 if (totalLength > 128) { |
| 536 // Native is quicker. | 539 // Native is quicker. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 623 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 621 // whitespaces. Add checking for multi-byte whitespace codepoints. | 624 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 622 bool _isWhitespace(int codePoint) { | 625 bool _isWhitespace(int codePoint) { |
| 623 return | 626 return |
| 624 (codePoint == 32) || // Space. | 627 (codePoint == 32) || // Space. |
| 625 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 628 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
| 626 } | 629 } |
| 627 } | 630 } |
| 628 | 631 |
| 629 | 632 |
| 630 class _ExternalFourByteString extends _StringBase implements String { | |
| 631 factory _ExternalFourByteString._uninstantiable() { | |
| 632 throw new UnsupportedError( | |
| 633 "ExternalFourByteString can only be allocated by the VM"); | |
| 634 } | |
| 635 | |
| 636 // Checks for one-byte whitespaces only. | |
| 637 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | |
| 638 // whitespaces. Add checking for multi-byte whitespace codepoints. | |
| 639 bool _isWhitespace(int codePoint) { | |
| 640 return | |
| 641 (codePoint == 32) || // Space. | |
| 642 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | |
| 643 } | |
| 644 } | |
| 645 | |
| 646 | |
| 647 class _StringMatch implements Match { | 633 class _StringMatch implements Match { |
| 648 const _StringMatch(int this.start, | 634 const _StringMatch(int this.start, |
| 649 String this.str, | 635 String this.str, |
| 650 String this.pattern); | 636 String this.pattern); |
| 651 | 637 |
| 652 int get end => start + pattern.length; | 638 int get end => start + pattern.length; |
| 653 String operator[](int g) => group(g); | 639 String operator[](int g) => group(g); |
| 654 int get groupCount => 0; | 640 int get groupCount => 0; |
| 655 | 641 |
| 656 String group(int group) { | 642 String group(int group) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 679 class _CodeUnits extends Object with ListMixin<int>, | 665 class _CodeUnits extends Object with ListMixin<int>, |
| 680 UnmodifiableListMixin<int> { | 666 UnmodifiableListMixin<int> { |
| 681 /** The string that this is the code units of. */ | 667 /** The string that this is the code units of. */ |
| 682 String _string; | 668 String _string; |
| 683 | 669 |
| 684 _CodeUnits(this._string); | 670 _CodeUnits(this._string); |
| 685 | 671 |
| 686 int get length => _string.length; | 672 int get length => _string.length; |
| 687 int operator[](int i) => _string.codeUnitAt(i); | 673 int operator[](int i) => _string.codeUnitAt(i); |
| 688 } | 674 } |
| OLD | NEW |