Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(613)

Side by Side Diff: runtime/lib/string_patch.dart

Issue 551823002: Optimize _GrowableArray._join and _StringBase._interpolate. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed operator+ change. Added more tests. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | runtime/vm/method_recognizer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 [int start = 0, int end]) { 7 [int start = 0, int end]) {
8 return _StringBase.createFromCharCodes(charCodes, start, end); 8 return _StringBase.createFromCharCodes(charCodes, start, end);
9 } 9 }
10 10
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 final s = o.toString(); 571 final s = o.toString();
572 if (s is! String) { 572 if (s is! String) {
573 throw new ArgumentError(o); 573 throw new ArgumentError(o);
574 } 574 }
575 return s; 575 return s;
576 } 576 }
577 577
578 /** 578 /**
579 * Convert all objects in [values] to strings and concat them 579 * Convert all objects in [values] to strings and concat them
580 * into a result string. 580 * into a result string.
581 * Modifies the input list if it contains non-`String` values.
581 */ 582 */
582 static String _interpolate(List<String> values) { 583 static String _interpolate(final List values) {
583 final numValues = values.length; 584 final numValues = values.length;
584 _List stringList = new List<String>(numValues);
585 bool isOneByteString = true;
586 int totalLength = 0; 585 int totalLength = 0;
587 for (int i = 0; i < numValues; i++) { 586 int i = 0;
588 var s = values[i].toString(); 587 while (i < numValues) {
589 if (isOneByteString && (ClassID.getID(s) == ClassID.cidOneByteString)) { 588 final e = values[i];
589 final s = e.toString();
590 values[i] = s;
591 if (ClassID.getID(s) == ClassID.cidOneByteString) {
590 totalLength += s.length; 592 totalLength += s.length;
593 i++;
594 } else if (s is! String) {
595 throw new ArgumentError(s);
591 } else { 596 } else {
592 isOneByteString = false; 597 // Handle remaining elements without checking for one-byte-ness.
593 if (s is! String) { 598 while (++i < numValues) {
594 throw new ArgumentError(s); 599 final e = values[i];
600 final s = e.toString();
601 values[i] = s;
602 if (s is! String) {
603 throw new ArgumentError(s);
604 }
595 } 605 }
606 return _concatRangeNative(values, 0, numValues);
596 } 607 }
597 stringList[i] = s;
598 } 608 }
599 if (isOneByteString) { 609 // All strings were one-byte strings.
600 return _OneByteString._concatAll(stringList, totalLength); 610 return _OneByteString._concatAll(values, totalLength);
601 }
602 return _concatRangeNative(stringList, 0, stringList.length);
603 } 611 }
604 612
605 Iterable<Match> allMatches(String string, [int start = 0]) { 613 Iterable<Match> allMatches(String string, [int start = 0]) {
606 List<Match> result = new List<Match>(); 614 List<Match> result = new List<Match>();
607 int length = string.length; 615 int length = string.length;
608 int patternLength = this.length; 616 int patternLength = this.length;
609 int startIndex = start; 617 int startIndex = start;
610 while (true) { 618 while (true) {
611 int position = string.indexOf(this, startIndex); 619 int position = string.indexOf(this, startIndex);
612 if (position == -1) { 620 if (position == -1) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 return super.split(pattern); 739 return super.split(pattern);
732 } 740 }
733 741
734 // All element of 'strings' must be OneByteStrings. 742 // All element of 'strings' must be OneByteStrings.
735 static _concatAll(List<String> strings, int totalLength) { 743 static _concatAll(List<String> strings, int totalLength) {
736 // TODO(srdjan): Improve code below and raise or eliminate the limit. 744 // TODO(srdjan): Improve code below and raise or eliminate the limit.
737 if (totalLength > 128) { 745 if (totalLength > 128) {
738 // Native is quicker. 746 // Native is quicker.
739 return _StringBase._concatRangeNative(strings, 0, strings.length); 747 return _StringBase._concatRangeNative(strings, 0, strings.length);
740 } 748 }
741 var res = _OneByteString._allocate(totalLength); 749 final res = _OneByteString._allocate(totalLength);
742 final stringsLength = strings.length; 750 final stringsLength = strings.length;
743 int rIx = 0; 751 int rIx = 0;
744 for (int i = 0; i < stringsLength; i++) { 752 for (int i = 0; i < stringsLength; i++) {
745 _OneByteString e = strings[i]; 753 final _OneByteString e = strings[i];
746 final eLength = e.length; 754 final eLength = e.length;
747 for (int s = 0; s < eLength; s++) { 755 for (int s = 0; s < eLength; s++) {
748 res._setAt(rIx++, e.codeUnitAt(s)); 756 res._setAt(rIx++, e.codeUnitAt(s));
749 } 757 }
750 } 758 }
751 return res; 759 return res;
752 } 760 }
753 761
754 int indexOf(Pattern pattern, [int start = 0]) { 762 int indexOf(Pattern pattern, [int start = 0]) {
755 // Specialize for single character pattern. 763 // Specialize for single character pattern.
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 class _CodeUnits extends Object with ListMixin<int>, 1075 class _CodeUnits extends Object with ListMixin<int>,
1068 UnmodifiableListMixin<int> { 1076 UnmodifiableListMixin<int> {
1069 /** The string that this is the code units of. */ 1077 /** The string that this is the code units of. */
1070 String _string; 1078 String _string;
1071 1079
1072 _CodeUnits(this._string); 1080 _CodeUnits(this._string);
1073 1081
1074 int get length => _string.length; 1082 int get length => _string.length;
1075 int operator[](int i) => _string.codeUnitAt(i); 1083 int operator[](int i) => _string.codeUnitAt(i);
1076 } 1084 }
OLDNEW
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | runtime/vm/method_recognizer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698