OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 // Misc benchmark-related utility functions. | |
6 | |
7 class BenchUtil { | |
8 static int get now() { | |
9 return new Date.now().value; | |
10 } | |
11 | |
12 static Map<String, Object> deserialize(String data) { | |
13 return JSON.parse(data); | |
14 } | |
15 | |
16 static String serialize(Object obj) { | |
17 return JSON.stringify(obj); | |
18 } | |
19 | |
20 // Shuffle a list randomly. | |
21 static void shuffle(List<Object> list) { | |
22 int len = list.length - 1; | |
23 for (int i = 0; i < len; i++) { | |
24 int index = (Math.random() * (len - i)).toInt() + i; | |
25 Object tmp = list[i]; | |
26 list[i] = list[index]; | |
27 list[index] = tmp; | |
28 } | |
29 } | |
30 | |
31 static String formatGolemData(String prefix, Map<String, num> results) { | |
32 List<String> elements = new List<String>(); | |
33 results.forEach((String name, num score) { | |
34 elements.add('"${prefix}/${name}":${score}'); | |
35 }); | |
36 return serialize(elements); | |
37 } | |
38 | |
39 static bool _inRange(int charCode, String start, String end) { | |
40 return start.charCodeAt(0) <= charCode && charCode <= end.charCodeAt(0); | |
41 } | |
42 | |
43 static final String DIGITS = '0123456789ABCDEF'; | |
44 static String _asDigit(int value) { | |
45 return DIGITS[value]; | |
46 } | |
47 | |
48 static String encodeUri(final String s) { | |
49 StringBuffer sb = new StringBuffer(); | |
50 for (int i = 0; i < s.length; i++) { | |
51 final int charCode = s.charCodeAt(i); | |
52 final bool noEscape = | |
53 _inRange(charCode, '0', '9') || | |
54 _inRange(charCode, 'a', 'z') || | |
55 _inRange(charCode, 'A', 'Z'); | |
56 if (noEscape) { | |
57 sb.add(s[i]); | |
58 } else { | |
59 sb.add('%'); | |
60 sb.add(_asDigit((charCode >> 4) & 0xF)); | |
61 sb.add(_asDigit(charCode & 0xF)); | |
62 } | |
63 } | |
64 return sb.toString(); | |
65 } | |
66 | |
67 // TODO: use corelib implementation. | |
68 static String replaceAll(String s, String pattern, | |
69 String replacement(Match match)) { | |
70 StringBuffer sb = new StringBuffer(); | |
71 | |
72 int pos = 0; | |
73 for (Match match in new RegExp(pattern).allMatches(s)) { | |
74 sb.add(s.substring(pos, match.start())); | |
75 sb.add(replacement(match)); | |
76 pos = match.end(); | |
77 } | |
78 sb.add(s.substring(pos)); | |
79 | |
80 return sb.toString(); | |
81 } | |
82 } | |
OLD | NEW |