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 part of _interceptors; | 5 part of _interceptors; |
6 | 6 |
7 /** | 7 /** |
8 * The interceptor class for [String]. The compiler recognizes this | 8 * The interceptor class for [String]. The compiler recognizes this |
9 * class as an interceptor, and changes references to [:this:] to | 9 * class as an interceptor, and changes references to [:this:] to |
10 * actually use the receiver of the method, which is generated as an extra | 10 * actually use the receiver of the method, which is generated as an extra |
11 * argument added to each member. | 11 * argument added to each member. |
12 */ | 12 */ |
13 class JSString extends Interceptor implements String, JSIndexable { | 13 class JSString extends Interceptor implements String, JSIndexable { |
14 const JSString(); | 14 const JSString(); |
15 | 15 |
16 int codeUnitAt(int index) { | 16 int codeUnitAt(int index) { |
17 if (index is !int) throw new ArgumentError(index); | 17 if (index is !int) throw diagnoseIndexError(this, index); |
18 if (index < 0) throw new RangeError.value(index); | 18 if (index < 0) throw diagnoseIndexError(this, index); |
19 if (index >= length) throw new RangeError.value(index); | 19 if (index >= length) throw diagnoseIndexError(this, index); |
20 return JS('JSUInt31', r'#.charCodeAt(#)', this, index); | 20 return JS('JSUInt31', r'#.charCodeAt(#)', this, index); |
21 } | 21 } |
22 | 22 |
23 Iterable<Match> allMatches(String string, [int start = 0]) { | 23 Iterable<Match> allMatches(String string, [int start = 0]) { |
24 checkString(string); | 24 checkString(string); |
25 checkInt(start); | 25 checkInt(start); |
26 if (0 > start || start > string.length) { | 26 if (0 > start || start > string.length) { |
27 throw new RangeError.range(start, 0, string.length); | 27 throw new RangeError.range(start, 0, string.length); |
28 } | 28 } |
29 return allMatchesInStringUnchecked(this, string, start); | 29 return allMatchesInStringUnchecked(this, string, start); |
30 } | 30 } |
31 | 31 |
32 Match matchAsPrefix(String string, [int start = 0]) { | 32 Match matchAsPrefix(String string, [int start = 0]) { |
33 if (start < 0 || start > string.length) { | 33 if (start < 0 || start > string.length) { |
34 throw new RangeError.range(start, 0, string.length); | 34 throw new RangeError.range(start, 0, string.length); |
35 } | 35 } |
36 if (start + this.length > string.length) return null; | 36 if (start + this.length > string.length) return null; |
37 // TODO(lrn): See if this can be optimized. | 37 // TODO(lrn): See if this can be optimized. |
38 for (int i = 0; i < this.length; i++) { | 38 for (int i = 0; i < this.length; i++) { |
39 if (string.codeUnitAt(start + i) != this.codeUnitAt(i)) { | 39 if (string.codeUnitAt(start + i) != this.codeUnitAt(i)) { |
40 return null; | 40 return null; |
41 } | 41 } |
42 } | 42 } |
43 return new StringMatch(start, string, this); | 43 return new StringMatch(start, string, this); |
44 } | 44 } |
45 | 45 |
46 String operator +(String other) { | 46 String operator +(String other) { |
47 if (other is !String) throw new ArgumentError(other); | 47 if (other is !String) throw new ArgumentError.value(other); |
48 return JS('String', r'# + #', this, other); | 48 return JS('String', r'# + #', this, other); |
49 } | 49 } |
50 | 50 |
51 bool endsWith(String other) { | 51 bool endsWith(String other) { |
52 checkString(other); | 52 checkString(other); |
53 int otherLength = other.length; | 53 int otherLength = other.length; |
54 if (otherLength > length) return false; | 54 if (otherLength > length) return false; |
55 return other == substring(length - otherLength); | 55 return other == substring(length - otherLength); |
56 } | 56 } |
57 | 57 |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 462 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
463 hash = JS('int', '# ^ (# >> 11)', hash, hash); | 463 hash = JS('int', '# ^ (# >> 11)', hash, hash); |
464 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 464 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
465 } | 465 } |
466 | 466 |
467 Type get runtimeType => String; | 467 Type get runtimeType => String; |
468 | 468 |
469 int get length => JS('int', r'#.length', this); | 469 int get length => JS('int', r'#.length', this); |
470 | 470 |
471 String operator [](int index) { | 471 String operator [](int index) { |
472 if (index is !int) throw new ArgumentError(index); | 472 if (index is !int) throw diagnoseIndexError(this, index); |
473 if (index >= length || index < 0) throw new RangeError.value(index); | 473 if (index >= length || index < 0) throw diagnoseIndexError(this, index); |
474 return JS('String', '#[#]', this, index); | 474 return JS('String', '#[#]', this, index); |
475 } | 475 } |
476 } | 476 } |
OLD | NEW |