OLD | NEW |
| (Empty) |
1 library java.core; | |
2 | |
3 const int LONG_MAX_VALUE = 0x7fffffffffffffff; | |
4 | |
5 final Stopwatch nanoTimeStopwatch = new Stopwatch(); | |
6 | |
7 /** | |
8 * Inserts the given arguments into [pattern]. | |
9 * | |
10 * format('Hello, {0}!', 'John') = 'Hello, John!' | |
11 * format('{0} are you {1}ing?', 'How', 'do') = 'How are you doing?' | |
12 * format('{0} are you {1}ing?', 'What', 'read') = 'What are you reading?' | |
13 */ | |
14 String format(String pattern, | |
15 [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7]) { | |
16 return formatList(pattern, [arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7]); | |
17 } | |
18 | |
19 /** | |
20 * Inserts the given [args] into [pattern]. | |
21 * | |
22 * format('Hello, {0}!', ['John']) = 'Hello, John!' | |
23 * format('{0} are you {1}ing?', ['How', 'do']) = 'How are you doing?' | |
24 * format('{0} are you {1}ing?', ['What', 'read']) = 'What are you reading?' | |
25 */ | |
26 String formatList(String pattern, List<Object> arguments) { | |
27 if (arguments == null || arguments.isEmpty) { | |
28 assert(!pattern.contains(new RegExp(r'\{(\d+)\}'))); | |
29 return pattern; | |
30 } | |
31 return pattern.replaceAllMapped(new RegExp(r'\{(\d+)\}'), (match) { | |
32 String indexStr = match.group(1); | |
33 int index = int.parse(indexStr); | |
34 Object arg = arguments[index]; | |
35 assert(arg != null); | |
36 return arg != null ? arg.toString() : null; | |
37 }); | |
38 } | |
39 | |
40 bool javaCollectionContainsAll(Iterable list, Iterable c) { | |
41 return c.fold(true, (bool prev, e) => prev && list.contains(e)); | |
42 } | |
43 | |
44 javaListSet(List list, int index, newValue) { | |
45 var oldValue = list[index]; | |
46 list[index] = newValue; | |
47 return oldValue; | |
48 } | |
49 | |
50 bool javaSetEquals(Set a, Set b) { | |
51 return a.containsAll(b) && b.containsAll(a); | |
52 } | |
53 | |
54 bool javaStringEqualsIgnoreCase(String a, String b) { | |
55 return a.toLowerCase() == b.toLowerCase(); | |
56 } | |
57 | |
58 bool javaStringRegionMatches( | |
59 String t, int toffset, String o, int ooffset, int len) { | |
60 if (toffset < 0) return false; | |
61 if (ooffset < 0) return false; | |
62 var tend = toffset + len; | |
63 var oend = ooffset + len; | |
64 if (tend > t.length) return false; | |
65 if (oend > o.length) return false; | |
66 return t.substring(toffset, tend) == o.substring(ooffset, oend); | |
67 } | |
68 | |
69 /// Parses given string to [Uri], throws [URISyntaxException] if invalid. | |
70 Uri parseUriWithException(String str) { | |
71 Uri uri; | |
72 try { | |
73 uri = Uri.parse(str); | |
74 } on FormatException catch (e) { | |
75 throw new URISyntaxException(e.toString()); | |
76 } | |
77 if (uri.path.isEmpty) { | |
78 throw new URISyntaxException('empty path'); | |
79 } | |
80 return uri; | |
81 } | |
82 | |
83 /** | |
84 * Very limited printf implementation, supports only %s and %d. | |
85 */ | |
86 String _printf(String fmt, List args) { | |
87 StringBuffer sb = new StringBuffer(); | |
88 bool markFound = false; | |
89 int argIndex = 0; | |
90 for (int i = 0; i < fmt.length; i++) { | |
91 int c = fmt.codeUnitAt(i); | |
92 if (c == 0x25) { | |
93 if (markFound) { | |
94 sb.writeCharCode(c); | |
95 markFound = false; | |
96 } else { | |
97 markFound = true; | |
98 } | |
99 continue; | |
100 } | |
101 if (markFound) { | |
102 markFound = false; | |
103 // %d | |
104 if (c == 0x64) { | |
105 sb.write(args[argIndex++]); | |
106 continue; | |
107 } | |
108 // %s | |
109 if (c == 0x73) { | |
110 sb.write(args[argIndex++]); | |
111 continue; | |
112 } | |
113 // unknown | |
114 throw new IllegalArgumentException( | |
115 '[$fmt][$i] = 0x${c.toRadixString(16)}'); | |
116 } else { | |
117 sb.writeCharCode(c); | |
118 } | |
119 } | |
120 return sb.toString(); | |
121 } | |
122 | |
123 class Character { | |
124 static const int MAX_VALUE = 0xffff; | |
125 static const int MAX_CODE_POINT = 0x10ffff; | |
126 static const int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000; | |
127 static const int MIN_LOW_SURROGATE = 0xDC00; | |
128 static const int MIN_HIGH_SURROGATE = 0xD800; | |
129 static int digit(int codePoint, int radix) { | |
130 if (radix != 16) { | |
131 throw new ArgumentError("only radix == 16 is supported"); | |
132 } | |
133 if (0x30 <= codePoint && codePoint <= 0x39) { | |
134 return codePoint - 0x30; | |
135 } | |
136 if (0x41 <= codePoint && codePoint <= 0x46) { | |
137 return 0xA + (codePoint - 0x41); | |
138 } | |
139 if (0x61 <= codePoint && codePoint <= 0x66) { | |
140 return 0xA + (codePoint - 0x61); | |
141 } | |
142 return -1; | |
143 } | |
144 static bool isDigit(int c) { | |
145 return c >= 0x30 && c <= 0x39; | |
146 } | |
147 static bool isLetter(int c) { | |
148 return c >= 0x41 && c <= 0x5A || c >= 0x61 && c <= 0x7A; | |
149 } | |
150 static bool isLetterOrDigit(int c) { | |
151 return isLetter(c) || isDigit(c); | |
152 } | |
153 static bool isLowerCase(int c) { | |
154 return c >= 0x61 && c <= 0x7A; | |
155 } | |
156 static bool isUpperCase(int c) { | |
157 return c >= 0x41 && c <= 0x5A; | |
158 } | |
159 static bool isWhitespace(int c) { | |
160 return c == 0x09 || c == 0x20 || c == 0x0A || c == 0x0D; | |
161 } | |
162 static String toChars(int codePoint) { | |
163 if (codePoint < 0 || codePoint > MAX_CODE_POINT) { | |
164 throw new IllegalArgumentException(); | |
165 } | |
166 if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) { | |
167 return new String.fromCharCode(codePoint); | |
168 } | |
169 int offset = codePoint - MIN_SUPPLEMENTARY_CODE_POINT; | |
170 int c0 = ((offset & 0x7FFFFFFF) >> 10) + MIN_HIGH_SURROGATE; | |
171 int c1 = (offset & 0x3ff) + MIN_LOW_SURROGATE; | |
172 return new String.fromCharCodes([c0, c1]); | |
173 } | |
174 static int toLowerCase(int c) { | |
175 if (c >= 0x41 && c <= 0x5A) { | |
176 return 0x61 + (c - 0x41); | |
177 } | |
178 return c; | |
179 } | |
180 static int toUpperCase(int c) { | |
181 if (c >= 0x61 && c <= 0x7A) { | |
182 return 0x41 + (c - 0x61); | |
183 } | |
184 return c; | |
185 } | |
186 } | |
187 | |
188 abstract class Enum<E extends Enum> implements Comparable<E> { | |
189 /// The name of this enum constant, as declared in the enum declaration. | |
190 final String name; | |
191 /// The position in the enum declaration. | |
192 final int ordinal; | |
193 const Enum(this.name, this.ordinal); | |
194 int get hashCode => ordinal; | |
195 int compareTo(E other) => ordinal - other.ordinal; | |
196 String toString() => name; | |
197 } | |
198 | |
199 class IllegalArgumentException extends JavaException { | |
200 IllegalArgumentException([message = "", cause = null]) | |
201 : super(message, cause); | |
202 } | |
203 | |
204 class IllegalStateException extends JavaException { | |
205 IllegalStateException([message = ""]) : super(message); | |
206 } | |
207 | |
208 class JavaArrays { | |
209 static bool equals(List a, List b) { | |
210 if (identical(a, b)) { | |
211 return true; | |
212 } | |
213 if (a.length != b.length) { | |
214 return false; | |
215 } | |
216 var len = a.length; | |
217 for (int i = 0; i < len; i++) { | |
218 if (a[i] != b[i]) { | |
219 return false; | |
220 } | |
221 } | |
222 return true; | |
223 } | |
224 static int makeHashCode(List a) { | |
225 if (a == null) { | |
226 return 0; | |
227 } | |
228 int result = 1; | |
229 for (var element in a) { | |
230 result = 31 * result + (element == null ? 0 : element.hashCode); | |
231 } | |
232 return result; | |
233 } | |
234 } | |
235 | |
236 class JavaException implements Exception { | |
237 final String message; | |
238 final Exception cause; | |
239 JavaException([this.message = "", this.cause = null]); | |
240 JavaException.withCause(this.cause) : message = null; | |
241 String toString() => "$runtimeType: $message $cause"; | |
242 } | |
243 | |
244 class JavaIOException extends JavaException { | |
245 JavaIOException([message = "", cause = null]) : super(message, cause); | |
246 } | |
247 | |
248 class JavaPatternMatcher { | |
249 Iterator<Match> _matches; | |
250 Match _match; | |
251 JavaPatternMatcher(RegExp re, String input) { | |
252 _matches = re.allMatches(input).iterator; | |
253 } | |
254 int end() => _match.end; | |
255 bool find() { | |
256 if (!_matches.moveNext()) { | |
257 return false; | |
258 } | |
259 _match = _matches.current; | |
260 return true; | |
261 } | |
262 String group(int i) => _match[i]; | |
263 bool matches() => find(); | |
264 int start() => _match.start; | |
265 } | |
266 | |
267 class JavaString { | |
268 static int indexOf(String target, String str, int fromIndex) { | |
269 if (fromIndex > target.length) return -1; | |
270 if (fromIndex < 0) fromIndex = 0; | |
271 return target.indexOf(str, fromIndex); | |
272 } | |
273 static int lastIndexOf(String target, String str, int fromIndex) { | |
274 if (fromIndex > target.length) return -1; | |
275 if (fromIndex < 0) fromIndex = 0; | |
276 return target.lastIndexOf(str, fromIndex); | |
277 } | |
278 static bool startsWithBefore(String s, String other, int start) { | |
279 return s.indexOf(other, start) != -1; | |
280 } | |
281 } | |
282 | |
283 class JavaSystem { | |
284 @deprecated | |
285 static void arraycopy( | |
286 List src, int srcPos, List dest, int destPos, int length) { | |
287 for (int i = 0; i < length; i++) { | |
288 dest[destPos + i] = src[srcPos + i]; | |
289 } | |
290 } | |
291 | |
292 static int currentTimeMillis() { | |
293 return (new DateTime.now()).millisecondsSinceEpoch; | |
294 } | |
295 | |
296 static int nanoTime() { | |
297 if (!nanoTimeStopwatch.isRunning) { | |
298 nanoTimeStopwatch.start(); | |
299 } | |
300 return nanoTimeStopwatch.elapsedMicroseconds * 1000; | |
301 } | |
302 } | |
303 | |
304 class MissingFormatArgumentException implements Exception { | |
305 final String s; | |
306 | |
307 MissingFormatArgumentException(this.s); | |
308 | |
309 String toString() => "MissingFormatArgumentException: $s"; | |
310 } | |
311 | |
312 class NoSuchElementException extends JavaException { | |
313 String toString() => "NoSuchElementException"; | |
314 } | |
315 | |
316 class NotImplementedException extends JavaException { | |
317 NotImplementedException(message) : super(message); | |
318 } | |
319 | |
320 class NumberFormatException extends JavaException { | |
321 String toString() => "NumberFormatException"; | |
322 } | |
323 | |
324 class PrintStringWriter extends PrintWriter { | |
325 final StringBuffer _sb = new StringBuffer(); | |
326 | |
327 void print(x) { | |
328 _sb.write(x); | |
329 } | |
330 | |
331 String toString() => _sb.toString(); | |
332 } | |
333 | |
334 abstract class PrintWriter { | |
335 void newLine() { | |
336 this.print('\n'); | |
337 } | |
338 | |
339 void print(x); | |
340 | |
341 void printf(String fmt, List args) { | |
342 this.print(_printf(fmt, args)); | |
343 } | |
344 | |
345 void println(String s) { | |
346 this.print(s); | |
347 this.newLine(); | |
348 } | |
349 } | |
350 | |
351 class RuntimeException extends JavaException { | |
352 RuntimeException({String message: "", Exception cause: null}) | |
353 : super(message, cause); | |
354 } | |
355 | |
356 class StringIndexOutOfBoundsException extends JavaException { | |
357 StringIndexOutOfBoundsException(int index) : super('$index'); | |
358 } | |
359 | |
360 class StringUtils { | |
361 static String capitalize(String str) { | |
362 if (isEmpty(str)) { | |
363 return str; | |
364 } | |
365 return str.substring(0, 1).toUpperCase() + str.substring(1); | |
366 } | |
367 | |
368 static bool equals(String cs1, String cs2) { | |
369 if (cs1 == cs2) { | |
370 return true; | |
371 } | |
372 if (cs1 == null || cs2 == null) { | |
373 return false; | |
374 } | |
375 return cs1 == cs2; | |
376 } | |
377 | |
378 static bool isEmpty(String str) { | |
379 return str == null || str.isEmpty; | |
380 } | |
381 | |
382 static String join(Iterable iter, | |
383 [String separator = ' ', int start = 0, int end = -1]) { | |
384 if (start != 0) { | |
385 iter = iter.skip(start); | |
386 } | |
387 if (end != -1) { | |
388 iter = iter.take(end - start); | |
389 } | |
390 return iter.join(separator); | |
391 } | |
392 | |
393 static void printf(StringBuffer buffer, String fmt, List args) { | |
394 buffer.write(_printf(fmt, args)); | |
395 } | |
396 | |
397 static String remove(String str, String remove) { | |
398 if (isEmpty(str) || isEmpty(remove)) { | |
399 return str; | |
400 } | |
401 return str.replaceAll(remove, ''); | |
402 } | |
403 | |
404 static String removeStart(String str, String remove) { | |
405 if (isEmpty(str) || isEmpty(remove)) { | |
406 return str; | |
407 } | |
408 if (str.startsWith(remove)) { | |
409 return str.substring(remove.length); | |
410 } | |
411 return str; | |
412 } | |
413 | |
414 static String repeat(String s, int n) { | |
415 StringBuffer sb = new StringBuffer(); | |
416 for (int i = 0; i < n; i++) { | |
417 sb.write(s); | |
418 } | |
419 return sb.toString(); | |
420 } | |
421 | |
422 static List<String> split(String s, [String pattern = ' ']) { | |
423 return s.split(pattern); | |
424 } | |
425 | |
426 static List<String> splitByWholeSeparatorPreserveAllTokens( | |
427 String s, String pattern) { | |
428 return s.split(pattern); | |
429 } | |
430 } | |
431 | |
432 class UnsupportedOperationException extends JavaException { | |
433 UnsupportedOperationException([message = ""]) : super(message); | |
434 } | |
435 | |
436 class URISyntaxException implements Exception { | |
437 final String message; | |
438 URISyntaxException(this.message); | |
439 String toString() => "URISyntaxException: $message"; | |
440 } | |
OLD | NEW |