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 implements String { | 13 class JSString implements String { |
14 const JSString(); | 14 const JSString(); |
15 | 15 |
16 int charCodeAt(index) => codeUnitAt(index); | |
17 | |
18 int codeUnitAt(int index) { | 16 int codeUnitAt(int index) { |
19 if (index is !num) throw new ArgumentError(index); | 17 if (index is !num) throw new ArgumentError(index); |
20 if (index < 0) throw new RangeError.value(index); | 18 if (index < 0) throw new RangeError.value(index); |
21 if (index >= length) throw new RangeError.value(index); | 19 if (index >= length) throw new RangeError.value(index); |
22 return JS('int', r'#.charCodeAt(#)', this, index); | 20 return JS('int', r'#.charCodeAt(#)', this, index); |
23 } | 21 } |
24 | 22 |
25 Iterable<Match> allMatches(String str) { | 23 Iterable<Match> allMatches(String str) { |
26 checkString(str); | 24 checkString(str); |
27 return allMatchesInStringUnchecked(this, str); | 25 return allMatchesInStringUnchecked(this, str); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 if (pattern is String) { | 62 if (pattern is String) { |
65 return JS('=List', r'#.split(#)', this, pattern); | 63 return JS('=List', r'#.split(#)', this, pattern); |
66 } else if (pattern is JSSyntaxRegExp) { | 64 } else if (pattern is JSSyntaxRegExp) { |
67 var re = regExpGetNative(pattern); | 65 var re = regExpGetNative(pattern); |
68 return JS('=List', r'#.split(#)', this, re); | 66 return JS('=List', r'#.split(#)', this, re); |
69 } else { | 67 } else { |
70 throw "String.split(Pattern) UNIMPLEMENTED"; | 68 throw "String.split(Pattern) UNIMPLEMENTED"; |
71 } | 69 } |
72 } | 70 } |
73 | 71 |
74 List<String> splitChars() { | |
75 return JS('=List', r'#.split("")', this); | |
76 } | |
77 | |
78 bool startsWith(String other) { | 72 bool startsWith(String other) { |
79 checkString(other); | 73 checkString(other); |
80 int otherLength = other.length; | 74 int otherLength = other.length; |
81 if (otherLength > length) return false; | 75 if (otherLength > length) return false; |
82 return JS('bool', r'# == #', other, | 76 return JS('bool', r'# == #', other, |
83 JS('String', r'#.substring(0, #)', this, otherLength)); | 77 JS('String', r'#.substring(0, #)', this, otherLength)); |
84 } | 78 } |
85 | 79 |
86 String substring(int startIndex, [int endIndex]) { | 80 String substring(int startIndex, [int endIndex]) { |
87 checkNum(startIndex); | 81 checkNum(startIndex); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 128 } |
135 | 129 |
136 String toUpperCase() { | 130 String toUpperCase() { |
137 return JS('String', r'#.toUpperCase()', this); | 131 return JS('String', r'#.toUpperCase()', this); |
138 } | 132 } |
139 | 133 |
140 String trim() { | 134 String trim() { |
141 return JS('String', r'#.trim()', this); | 135 return JS('String', r'#.trim()', this); |
142 } | 136 } |
143 | 137 |
144 List<int> get charCodes { | 138 List<int> get codeUnits => new CodeUnits(this); |
145 List<int> result = new List<int>.fixedLength(length); | |
146 for (int i = 0; i < length; i++) { | |
147 result[i] = JS('int', '#.charCodeAt(#)', this, i); | |
148 } | |
149 return result; | |
150 } | |
151 | |
152 Iterable<int> get codeUnits => new CodeUnits(this); | |
153 | 139 |
154 Runes get runes => new Runes(this); | 140 Runes get runes => new Runes(this); |
155 | 141 |
156 int indexOf(String other, [int start = 0]) { | 142 int indexOf(String other, [int start = 0]) { |
157 checkNull(other); | 143 checkNull(other); |
158 if (start is !int) throw new ArgumentError(start); | 144 if (start is !int) throw new ArgumentError(start); |
159 if (other is !String) throw new ArgumentError(other); | 145 if (other is !String) throw new ArgumentError(other); |
160 if (start < 0) return -1; | 146 if (start < 0) return -1; |
161 return JS('int', r'#.indexOf(#, #)', this, other, start); | 147 return JS('int', r'#.indexOf(#, #)', this, other, start); |
162 } | 148 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 Type get runtimeType => String; | 202 Type get runtimeType => String; |
217 | 203 |
218 int get length => JS('int', r'#.length', this); | 204 int get length => JS('int', r'#.length', this); |
219 | 205 |
220 String operator [](int index) { | 206 String operator [](int index) { |
221 if (index is !int) throw new ArgumentError(index); | 207 if (index is !int) throw new ArgumentError(index); |
222 if (index >= length || index < 0) throw new RangeError.value(index); | 208 if (index >= length || index < 0) throw new RangeError.value(index); |
223 return JS('String', '#[#]', this, index); | 209 return JS('String', '#[#]', this, index); |
224 } | 210 } |
225 } | 211 } |
OLD | NEW |