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