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 /** | 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 25 matching lines...) Expand all Loading... |
36 static String _createFromCodePoints(List<int> codePoints) | 36 static String _createFromCodePoints(List<int> codePoints) |
37 native "StringBase_createFromCodePoints"; | 37 native "StringBase_createFromCodePoints"; |
38 | 38 |
39 String operator [](int index) native "String_charAt"; | 39 String operator [](int index) native "String_charAt"; |
40 | 40 |
41 int charCodeAt(int index) native "String_charCodeAt"; | 41 int charCodeAt(int index) native "String_charCodeAt"; |
42 | 42 |
43 int get length native "String_getLength"; | 43 int get length native "String_getLength"; |
44 | 44 |
45 bool get isEmpty { | 45 bool get isEmpty { |
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 (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 |
67 int compareTo(String other) { | 67 int compareTo(String other) { |
68 int thisLength = this.length; | 68 int thisLength = this.length; |
69 int otherLength = other.length; | 69 int otherLength = other.length; |
70 int len = (thisLength < otherLength) ? thisLength : otherLength; | 70 int len = (thisLength < otherLength) ? thisLength : otherLength; |
71 for (int i = 0; i < len; i++) { | 71 for (int i = 0; i < len; i++) { |
72 int thisCodePoint = this.charCodeAt(i); | 72 int thisCodePoint = this.charCodeAt(i); |
73 int otherCodePoint = other.charCodeAt(i); | 73 int otherCodePoint = other.charCodeAt(i); |
74 if (thisCodePoint < otherCodePoint) { | 74 if (thisCodePoint < otherCodePoint) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 134 } |
135 for (int index = start; index >= 0; index--) { | 135 for (int index = start; index >= 0; index--) { |
136 if (_substringMatches(index, other)) { | 136 if (_substringMatches(index, other)) { |
137 return index; | 137 return index; |
138 } | 138 } |
139 } | 139 } |
140 return -1; | 140 return -1; |
141 } | 141 } |
142 | 142 |
143 String substring(int startIndex, [int endIndex]) { | 143 String substring(int startIndex, [int endIndex]) { |
144 if (endIndex === null) endIndex = this.length; | 144 if (endIndex == null) endIndex = this.length; |
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 } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 return result; | 311 return result; |
312 } | 312 } |
313 | 313 |
314 String toUpperCase() native "String_toUpperCase"; | 314 String toUpperCase() native "String_toUpperCase"; |
315 | 315 |
316 String toLowerCase() native "String_toLowerCase"; | 316 String toLowerCase() native "String_toLowerCase"; |
317 | 317 |
318 // Implementations of Strings methods follow below. | 318 // Implementations of Strings methods follow below. |
319 static String join(List<String> strings, String separator) { | 319 static String join(List<String> strings, String separator) { |
320 final int length = strings.length; | 320 final int length = strings.length; |
321 if (length === 0) { | 321 if (length == 0) { |
322 return ""; | 322 return ""; |
323 } | 323 } |
324 | 324 |
325 List stringsList = strings; | 325 List stringsList = strings; |
326 if (separator.length != 0) { | 326 if (separator.length != 0) { |
327 stringsList = new List(2 * length - 1); | 327 stringsList = new List(2 * length - 1); |
328 stringsList[0] = strings[0]; | 328 stringsList[0] = strings[0]; |
329 int j = 1; | 329 int j = 1; |
330 for (int i = 1; i < length; i++) { | 330 for (int i = 1; i < length; i++) { |
331 stringsList[j++] = separator; | 331 stringsList[j++] = separator; |
(...skipping 26 matching lines...) Expand all Loading... |
358 factory _OneByteString._uninstantiable() { | 358 factory _OneByteString._uninstantiable() { |
359 throw new UnsupportedError( | 359 throw new UnsupportedError( |
360 "_OneByteString can only be allocated by the VM"); | 360 "_OneByteString can only be allocated by the VM"); |
361 } | 361 } |
362 | 362 |
363 // Checks for one-byte whitespaces only. | 363 // Checks for one-byte whitespaces only. |
364 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 364 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
365 // whitespaces for one byte strings. | 365 // whitespaces for one byte strings. |
366 bool _isWhitespace(int codePoint) { | 366 bool _isWhitespace(int codePoint) { |
367 return | 367 return |
368 (codePoint === 32) || // Space. | 368 (codePoint == 32) || // Space. |
369 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 369 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
370 } | 370 } |
371 | 371 |
372 } | 372 } |
373 | 373 |
374 | 374 |
375 class _TwoByteString extends _StringBase implements String { | 375 class _TwoByteString extends _StringBase implements String { |
376 factory _TwoByteString._uninstantiable() { | 376 factory _TwoByteString._uninstantiable() { |
377 throw new UnsupportedError( | 377 throw new UnsupportedError( |
378 "_TwoByteString can only be allocated by the VM"); | 378 "_TwoByteString can only be allocated by the VM"); |
379 } | 379 } |
380 | 380 |
381 // Checks for one-byte whitespaces only. | 381 // Checks for one-byte whitespaces only. |
382 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 382 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
383 // whitespaces. Add checking for multi-byte whitespace codepoints. | 383 // whitespaces. Add checking for multi-byte whitespace codepoints. |
384 bool _isWhitespace(int codePoint) { | 384 bool _isWhitespace(int codePoint) { |
385 return | 385 return |
386 (codePoint === 32) || // Space. | 386 (codePoint == 32) || // Space. |
387 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 387 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
388 } | 388 } |
389 } | 389 } |
390 | 390 |
391 | 391 |
392 class _FourByteString extends _StringBase implements String { | 392 class _FourByteString extends _StringBase implements String { |
393 factory _FourByteString._uninstantiable() { | 393 factory _FourByteString._uninstantiable() { |
394 throw new UnsupportedError( | 394 throw new UnsupportedError( |
395 "_FourByteString can only be allocated by the VM"); | 395 "_FourByteString can only be allocated by the VM"); |
396 } | 396 } |
397 | 397 |
398 // Checks for one-byte whitespaces only. | 398 // Checks for one-byte whitespaces only. |
399 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 399 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
400 // whitespaces. Add checking for multi-byte whitespace codepoints. | 400 // whitespaces. Add checking for multi-byte whitespace codepoints. |
401 bool _isWhitespace(int codePoint) { | 401 bool _isWhitespace(int codePoint) { |
402 return | 402 return |
403 (codePoint === 32) || // Space. | 403 (codePoint == 32) || // Space. |
404 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 404 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
405 } | 405 } |
406 } | 406 } |
407 | 407 |
408 | 408 |
409 class _ExternalOneByteString extends _StringBase implements String { | 409 class _ExternalOneByteString extends _StringBase implements String { |
410 factory _ExternalOneByteString._uninstantiable() { | 410 factory _ExternalOneByteString._uninstantiable() { |
411 throw new UnsupportedError( | 411 throw new UnsupportedError( |
412 "_ExternalOneByteString can only be allocated by the VM"); | 412 "_ExternalOneByteString can only be allocated by the VM"); |
413 } | 413 } |
414 | 414 |
415 // Checks for one-byte whitespaces only. | 415 // Checks for one-byte whitespaces only. |
416 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 416 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
417 // whitespaces for one byte strings. | 417 // whitespaces for one byte strings. |
418 bool _isWhitespace(int codePoint) { | 418 bool _isWhitespace(int codePoint) { |
419 return | 419 return |
420 (codePoint === 32) || // Space. | 420 (codePoint == 32) || // Space. |
421 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 421 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
422 } | 422 } |
423 } | 423 } |
424 | 424 |
425 | 425 |
426 class _ExternalTwoByteString extends _StringBase implements String { | 426 class _ExternalTwoByteString extends _StringBase implements String { |
427 factory ExternalTwoByteString._uninstantiable() { | 427 factory ExternalTwoByteString._uninstantiable() { |
428 throw new UnsupportedError( | 428 throw new UnsupportedError( |
429 "_ExternalTwoByteString can only be allocated by the VM"); | 429 "_ExternalTwoByteString can only be allocated by the VM"); |
430 } | 430 } |
431 | 431 |
432 // Checks for one-byte whitespaces only. | 432 // Checks for one-byte whitespaces only. |
433 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 433 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
434 // whitespaces. Add checking for multi-byte whitespace codepoints. | 434 // whitespaces. Add checking for multi-byte whitespace codepoints. |
435 bool _isWhitespace(int codePoint) { | 435 bool _isWhitespace(int codePoint) { |
436 return | 436 return |
437 (codePoint === 32) || // Space. | 437 (codePoint == 32) || // Space. |
438 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 438 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
439 } | 439 } |
440 } | 440 } |
441 | 441 |
442 | 442 |
443 class _ExternalFourByteString extends _StringBase implements String { | 443 class _ExternalFourByteString extends _StringBase implements String { |
444 factory _ExternalFourByteString._uninstantiable() { | 444 factory _ExternalFourByteString._uninstantiable() { |
445 throw new UnsupportedError( | 445 throw new UnsupportedError( |
446 "ExternalFourByteString can only be allocated by the VM"); | 446 "ExternalFourByteString can only be allocated by the VM"); |
447 } | 447 } |
448 | 448 |
449 // Checks for one-byte whitespaces only. | 449 // Checks for one-byte whitespaces only. |
450 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid | 450 // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid |
451 // whitespaces. Add checking for multi-byte whitespace codepoints. | 451 // whitespaces. Add checking for multi-byte whitespace codepoints. |
452 bool _isWhitespace(int codePoint) { | 452 bool _isWhitespace(int codePoint) { |
453 return | 453 return |
454 (codePoint === 32) || // Space. | 454 (codePoint == 32) || // Space. |
455 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. | 455 ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc. |
456 } | 456 } |
457 } | 457 } |
458 | 458 |
459 | 459 |
460 class _StringMatch implements Match { | 460 class _StringMatch implements Match { |
461 const _StringMatch(int this.start, | 461 const _StringMatch(int this.start, |
462 String this.str, | 462 String this.str, |
463 String this.pattern); | 463 String this.pattern); |
464 | 464 |
(...skipping 13 matching lines...) Expand all Loading... |
478 for (int g in groups) { | 478 for (int g in groups) { |
479 result.add(group(g)); | 479 result.add(group(g)); |
480 } | 480 } |
481 return result; | 481 return result; |
482 } | 482 } |
483 | 483 |
484 final int start; | 484 final int start; |
485 final String str; | 485 final String str; |
486 final String pattern; | 486 final String pattern; |
487 } | 487 } |
OLD | NEW |