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

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

Issue 11415234: Improve performance of String split (use unchecked substring). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years 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 | « no previous file | no next file » | 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 /** 5 /**
6 * [_StringBase] contains common methods used by concrete String 6 * [_StringBase] contains common methods used by concrete String
7 * implementations, e.g., _OneByteString. 7 * implementations, e.g., _OneByteString.
8 */ 8 */
9 class _StringBase { 9 class _StringBase {
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 return this.length == 0; 46 return this.length == 0;
47 } 47 }
48 48
49 String concat(String other) native "String_concat"; 49 String concat(String other) native "String_concat";
50 50
51 String toString() { 51 String toString() {
52 return this; 52 return this;
53 } 53 }
54 54
55 bool operator ==(Object other) { 55 bool operator ==(Object other) {
56 if (this === other) { 56 if (identical(this, other)) {
57 return true; 57 return true;
58 } 58 }
59 if ((other is !String) || 59 if ((other is !String) ||
60 (this.length != other.length)) { 60 (this.length != other.length)) {
61 // TODO(5413632): Compare hash codes when both are present. 61 // TODO(5413632): Compare hash codes when both are present.
62 return false; 62 return false;
63 } 63 }
64 return this.compareTo(other) == 0; 64 return this.compareTo(other) == 0;
65 } 65 }
66 66
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 145
146 if ((startIndex < 0) || (startIndex > this.length)) { 146 if ((startIndex < 0) || (startIndex > this.length)) {
147 throw new RangeError.value(startIndex); 147 throw new RangeError.value(startIndex);
148 } 148 }
149 if ((endIndex < 0) || (endIndex > this.length)) { 149 if ((endIndex < 0) || (endIndex > this.length)) {
150 throw new RangeError.value(endIndex); 150 throw new RangeError.value(endIndex);
151 } 151 }
152 if (startIndex > endIndex) { 152 if (startIndex > endIndex) {
153 throw new RangeError.value(startIndex); 153 throw new RangeError.value(startIndex);
154 } 154 }
155 return _substringUnchecked(startIndex, endIndex);
156 }
157
158 String _substringUnchecked(int startIndex, int endIndex) {
159 assert(endIndex != null);
160 assert((startIndex >= 0) && (startIndex <= this.length));
161 assert((endIndex >= 0) && (endIndex <= this.length));
162 assert(startIndex <= endIndex);
163
155 if (startIndex == endIndex) { 164 if (startIndex == endIndex) {
156 return ""; 165 return "";
157 } 166 }
158 if ((startIndex + 1) == endIndex) { 167 if ((startIndex + 1) == endIndex) {
159 return this[startIndex]; 168 return this[startIndex];
160 } 169 }
161 return _substringUnchecked(startIndex, endIndex); 170 return _substringUncheckedNative(startIndex, endIndex);
162 } 171 }
163 172
164 String _substringUnchecked(int startIndex, int endIndex) 173 String _substringUncheckedNative(int startIndex, int endIndex)
165 native "StringBase_substringUnchecked"; 174 native "StringBase_substringUnchecked";
166 175
167 String trim() { 176 String trim() {
168 final int len = this.length; 177 final int len = this.length;
169 int first = 0; 178 int first = 0;
170 for (; first < len; first++) { 179 for (; first < len; first++) {
171 if (!_isWhitespace(this.charCodeAt(first))) { 180 if (!_isWhitespace(this.charCodeAt(first))) {
172 break; 181 break;
173 } 182 }
174 } 183 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 Iterator iterator = pattern.allMatches(this).iterator(); 285 Iterator iterator = pattern.allMatches(this).iterator();
277 if (length == 0 && iterator.hasNext) { 286 if (length == 0 && iterator.hasNext) {
278 // A matched empty string input returns the empty list. 287 // A matched empty string input returns the empty list.
279 return <String>[]; 288 return <String>[];
280 } 289 }
281 List<String> result = new List<String>(); 290 List<String> result = new List<String>();
282 int startIndex = 0; 291 int startIndex = 0;
283 int previousIndex = 0; 292 int previousIndex = 0;
284 while (true) { 293 while (true) {
285 if (startIndex == length || !iterator.hasNext) { 294 if (startIndex == length || !iterator.hasNext) {
286 result.add(this.substring(previousIndex, length)); 295 result.add(this._substringUnchecked(previousIndex, length));
287 break; 296 break;
288 } 297 }
289 Match match = iterator.next(); 298 Match match = iterator.next();
290 if (match.start == length) { 299 if (match.start == length) {
291 result.add(this.substring(previousIndex, length)); 300 result.add(this._substringUnchecked(previousIndex, length));
292 break; 301 break;
293 } 302 }
294 int endIndex = match.end; 303 int endIndex = match.end;
295 if (startIndex == endIndex && endIndex == previousIndex) { 304 if (startIndex == endIndex && endIndex == previousIndex) {
296 ++startIndex; // empty match, advance and restart 305 ++startIndex; // empty match, advance and restart
297 continue; 306 continue;
298 } 307 }
299 result.add(this.substring(previousIndex, match.start)); 308 result.add(this._substringUnchecked(previousIndex, match.start));
300 startIndex = previousIndex = endIndex; 309 startIndex = previousIndex = endIndex;
301 } 310 }
302 return result; 311 return result;
303 } 312 }
304 313
305 List<String> splitChars() { 314 List<String> splitChars() {
306 int len = this.length; 315 int len = this.length;
307 final result = new List<String>(len); 316 final result = new List<String>(len);
308 for (int i = 0; i < len; i++) { 317 for (int i = 0; i < len; i++) {
309 result[i] = this[i]; 318 result[i] = this[i];
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 380
372 // Checks for one-byte whitespaces only. 381 // Checks for one-byte whitespaces only.
373 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid 382 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid
374 // whitespaces for one byte strings. 383 // whitespaces for one byte strings.
375 bool _isWhitespace(int codePoint) { 384 bool _isWhitespace(int codePoint) {
376 return 385 return
377 (codePoint == 32) || // Space. 386 (codePoint == 32) || // Space.
378 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. 387 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
379 } 388 }
380 389
381 String _substringUnchecked(int startIndex, int endIndex) 390 String _substringUncheckedNative(int startIndex, int endIndex)
382 native "OneByteString_substringUnchecked"; 391 native "OneByteString_substringUnchecked";
383 } 392 }
384 393
385 394
386 class _TwoByteString extends _StringBase implements String { 395 class _TwoByteString extends _StringBase implements String {
387 factory _TwoByteString._uninstantiable() { 396 factory _TwoByteString._uninstantiable() {
388 throw new UnsupportedError( 397 throw new UnsupportedError(
389 "_TwoByteString can only be allocated by the VM"); 398 "_TwoByteString can only be allocated by the VM");
390 } 399 }
391 400
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 for (int g in groups) { 498 for (int g in groups) {
490 result.add(group(g)); 499 result.add(group(g));
491 } 500 }
492 return result; 501 return result;
493 } 502 }
494 503
495 final int start; 504 final int start;
496 final String str; 505 final String str;
497 final String pattern; 506 final String pattern;
498 } 507 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698