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 #library('dart:_interceptors'); | 5 library _interceptors; |
6 | 6 |
7 #import('dart:collection'); | 7 import 'dart:collection'; |
8 | 8 |
9 /** | 9 part 'js_string.dart'; |
10 * The interceptor class for [String]. The compiler recognizes this | |
11 * class as an interceptor, and changes references to [:this:] to | |
12 * actually use the receiver of the method, which is generated as an extra | |
13 * argument added to each member. | |
14 */ | |
15 class JSString implements String { | |
16 const JSString(); | |
17 | |
18 int charCodeAt(index) { | |
19 if (index is !num) throw new ArgumentError(index); | |
20 if (index < 0) throw new RangeError.value(index); | |
21 if (index >= length) throw new RangeError.value(index); | |
22 return JS('int', r'#.charCodeAt(#)', this, index); | |
23 } | |
24 | |
25 Iterable<Match> allMatches(String str) { | |
26 checkString(str); | |
27 return allMatchesInStringUnchecked(this, str); | |
28 } | |
29 | |
30 String concat(String other) { | |
31 if (other is !String) throw new ArgumentError(other); | |
32 return JS('String', r'# + #', this, other); | |
33 } | |
34 | |
35 bool endsWith(String other) { | |
36 checkString(other); | |
37 int otherLength = other.length; | |
38 if (otherLength > length) return false; | |
39 return other == substring(length - otherLength); | |
40 } | |
41 | |
42 String replaceAll(Pattern from, String to) { | |
43 checkString(to); | |
44 return stringReplaceAllUnchecked(this, from, to); | |
45 } | |
46 | |
47 String replaceFirst(Pattern from, String to) { | |
48 checkString(to); | |
49 return stringReplaceFirstUnchecked(this, from, to); | |
50 } | |
51 | |
52 List<String> split(Pattern pattern) { | |
53 checkNull(pattern); | |
54 return stringSplitUnchecked(this, pattern); | |
55 } | |
56 | |
57 List<String> splitChars() { | |
58 return JS('List', r'#.split("")', this); | |
59 } | |
60 | |
61 bool startsWith(String other) { | |
62 checkString(other); | |
63 int otherLength = other.length; | |
64 if (otherLength > length) return false; | |
65 return JS('bool', r'# == #', other, | |
66 JS('String', r'#.substring(0, #)', this, otherLength)); | |
67 } | |
68 | |
69 String substring(int startIndex, [int endIndex]) { | |
70 checkNum(startIndex); | |
71 if (endIndex == null) endIndex = length; | |
72 checkNum(endIndex); | |
73 if (startIndex < 0 ) throw new RangeError.value(startIndex); | |
74 if (startIndex > endIndex) throw new RangeError.value(startIndex); | |
75 if (endIndex > length) throw new RangeError.value(endIndex); | |
76 return JS('String', r'#.substring(#, #)', this, startIndex, endIndex); | |
77 } | |
78 | |
79 String toLowerCase() { | |
80 return JS('String', r'#.toLowerCase()', this); | |
81 } | |
82 | |
83 String toUpperCase() { | |
84 return JS('String', r'#.toUpperCase()', this); | |
85 } | |
86 | |
87 String trim() { | |
88 return JS('String', r'#.trim()', this); | |
89 } | |
90 | |
91 List<int> get charCodes { | |
92 List<int> result = new List<int>(length); | |
93 for (int i = 0; i < length; i++) { | |
94 result[i] = charCodeAt(i); | |
95 } | |
96 return result; | |
97 } | |
98 } | |
99 | 10 |
100 /** | 11 /** |
101 * The interceptor class for all non-primitive objects. All its | 12 * The interceptor class for all non-primitive objects. All its |
102 * members are synthethized by the compiler's emitter. | 13 * members are synthethized by the compiler's emitter. |
103 */ | 14 */ |
104 class ObjectInterceptor { | 15 class ObjectInterceptor { |
105 const ObjectInterceptor(); | 16 const ObjectInterceptor(); |
106 } | 17 } |
107 | 18 |
108 /** | 19 /** |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 } else if (isJsArray(receiver)) { | 581 } else if (isJsArray(receiver)) { |
671 return getOrCreateCachedRuntimeType('List'); | 582 return getOrCreateCachedRuntimeType('List'); |
672 } else { | 583 } else { |
673 return UNINTERCEPTED(receiver.runtimeType); | 584 return UNINTERCEPTED(receiver.runtimeType); |
674 } | 585 } |
675 } | 586 } |
676 | 587 |
677 // TODO(lrn): These getters should be generated automatically for all | 588 // TODO(lrn): These getters should be generated automatically for all |
678 // intercepted methods. | 589 // intercepted methods. |
679 get$toString(receiver) => () => toString(receiver); | 590 get$toString(receiver) => () => toString(receiver); |
OLD | NEW |