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 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._clCid) && |
| 34 charCodes is! _ImmutableArray) { | 34 (ccid != _GrowableObjectArray._clCid) && |
| 35 (ccid != _ImmutableArray._clCid)) { | |
|
siva
2013/05/13 20:38:00
It might be more readable to have static methods i
srdjan
2013/05/14 17:46:31
I am open to change it, but I think this is more r
| |
| 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._clCid)) { |
|
siva
2013/05/13 20:38:00
Object.IsOneByteString(s);
srdjan
2013/05/14 17:46:31
Next CL as discussed offline.
| |
| 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._clCid) { |
| 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._clCid) { |
| 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 _clCid = "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._clCid) && (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 { | 633 class _ExternalFourByteString extends _StringBase implements String { |
|
Ivan Posva
2013/05/13 22:12:37
I don't think this needs to be here anymore. Four-
srdjan
2013/05/14 17:46:31
Removed.
| |
| 631 factory _ExternalFourByteString._uninstantiable() { | 634 factory _ExternalFourByteString._uninstantiable() { |
| 632 throw new UnsupportedError( | 635 throw new UnsupportedError( |
| 633 "ExternalFourByteString can only be allocated by the VM"); | 636 "ExternalFourByteString can only be allocated by the VM"); |
| 634 } | 637 } |
| 635 | 638 |
| 636 // Checks for one-byte whitespaces only. | 639 // Checks for one-byte whitespaces only. |
| 637 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 640 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
| 638 // whitespaces. Add checking for multi-byte whitespace codepoints. | 641 // whitespaces. Add checking for multi-byte whitespace codepoints. |
| 639 bool _isWhitespace(int codePoint) { | 642 bool _isWhitespace(int codePoint) { |
| 640 return | 643 return |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 679 class _CodeUnits extends Object with ListMixin<int>, | 682 class _CodeUnits extends Object with ListMixin<int>, |
| 680 UnmodifiableListMixin<int> { | 683 UnmodifiableListMixin<int> { |
| 681 /** The string that this is the code units of. */ | 684 /** The string that this is the code units of. */ |
| 682 String _string; | 685 String _string; |
| 683 | 686 |
| 684 _CodeUnits(this._string); | 687 _CodeUnits(this._string); |
| 685 | 688 |
| 686 int get length => _string.length; | 689 int get length => _string.length; |
| 687 int operator[](int i) => _string.codeUnitAt(i); | 690 int operator[](int i) => _string.codeUnitAt(i); |
| 688 } | 691 } |
| OLD | NEW |