| OLD | NEW |
| (Empty) |
| 1 /** Misc things that were useful when porting the code from Python. */ | |
| 2 #library('utils'); | |
| 3 | |
| 4 #import('dart:math'); | |
| 5 #import('package:unittest/unittest.dart'); | |
| 6 #import('constants.dart'); | |
| 7 | |
| 8 typedef bool Predicate(); | |
| 9 | |
| 10 class Pair<F extends Hashable, S extends Hashable> implements Hashable { | |
| 11 final F first; | |
| 12 final S second; | |
| 13 | |
| 14 const Pair(this.first, this.second); | |
| 15 | |
| 16 int hashCode() => 37 * first.hashCode() + second.hashCode(); | |
| 17 bool operator ==(other) => other.first == first && other.second == second; | |
| 18 } | |
| 19 | |
| 20 int parseIntRadix(String str, [int radix = 10]) { | |
| 21 int val = 0; | |
| 22 for (int i = 0; i < str.length; i++) { | |
| 23 var digit = str.charCodeAt(i); | |
| 24 if (digit >= LOWER_A) { | |
| 25 digit += 10 - LOWER_A; | |
| 26 } else if (digit >= UPPER_A) { | |
| 27 digit += 10 - UPPER_A; | |
| 28 } else { | |
| 29 digit -= ZERO; | |
| 30 } | |
| 31 val = val * radix + digit; | |
| 32 } | |
| 33 return val; | |
| 34 } | |
| 35 | |
| 36 bool any(List<bool> iterable) => iterable.some((f) => f); | |
| 37 | |
| 38 bool startsWithAny(String str, List<String> prefixes) { | |
| 39 for (var prefix in prefixes) { | |
| 40 if (str.startsWith(prefix)) { | |
| 41 return true; | |
| 42 } | |
| 43 } | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 String joinStr(List<String> strings) => Strings.join(strings, ''); | |
| 48 | |
| 49 // Like the python [:] operator. | |
| 50 List slice(List list, int start, [int end]) { | |
| 51 if (end == null) end = list.length; | |
| 52 if (end < 0) end += list.length; | |
| 53 | |
| 54 // Ensure the indexes are in bounds. | |
| 55 if (end < start) end = start; | |
| 56 if (end > list.length) end = list.length; | |
| 57 return list.getRange(start, end - start); | |
| 58 } | |
| 59 | |
| 60 removeAt(List list, int i) { | |
| 61 var result = list[i]; | |
| 62 list.removeRange(i, 1); | |
| 63 return result; | |
| 64 } | |
| 65 | |
| 66 bool removeFromList(List list, item) { | |
| 67 int i = list.indexOf(item); | |
| 68 if (i == -1) return false; | |
| 69 list.removeRange(i, 1); | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 /** Makes a dictionary, where the first key wins. */ | |
| 74 Map makeDict(List<List> items) { | |
| 75 var result = new Map(); | |
| 76 for (var item in items) { | |
| 77 expect(item.length, equals(2)); | |
| 78 result.putIfAbsent(item[0], () => item[1]); | |
| 79 } | |
| 80 return result; | |
| 81 } | |
| 82 | |
| 83 bool allWhitespace(String str) { | |
| 84 for (int i = 0; i < str.length; i++) { | |
| 85 if (!isWhitespaceCC(str.charCodeAt(i))) return false; | |
| 86 } | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 String padWithZeros(String str, int size) { | |
| 91 if (str.length == size) return str; | |
| 92 var result = new StringBuffer(); | |
| 93 size -= str.length; | |
| 94 for (int i = 0; i < size; i++) result.add('0'); | |
| 95 result.add(str); | |
| 96 return result.toString(); | |
| 97 } | |
| 98 | |
| 99 | |
| 100 /** | |
| 101 * Note: this is meant to match: | |
| 102 * <http://docs.python.org/library/xml.sax.utils.html#xml.sax.saxutils.escape> | |
| 103 * So we only escape `&` `<` and `>`, unlike Dart's htmlEscape function. | |
| 104 */ | |
| 105 String htmlEscapeMinimal(String text, [Map extraReplace]) { | |
| 106 // TODO(efortuna): A more efficient implementation. | |
| 107 text = text.replaceAll("&", "&") | |
| 108 .replaceAll("<", "<") | |
| 109 .replaceAll(">", ">"); | |
| 110 if (extraReplace != null) { | |
| 111 extraReplace.forEach((k, v) { text = text.replaceAll(k, v); }); | |
| 112 } | |
| 113 return text; | |
| 114 } | |
| 115 | |
| 116 | |
| 117 // TODO(jmesserly): this implementation is pretty wrong, but I need something | |
| 118 // quick until dartbug.com/1694 is fixed. | |
| 119 /** | |
| 120 * Format a string like Python's % string format operator. Right now this only | |
| 121 * supports a [data] dictionary used with %s or %08x. Those were the only things | |
| 122 * needed for [errorMessages]. | |
| 123 */ | |
| 124 String formatStr(String format, Map data) { | |
| 125 if (data == null) return format; | |
| 126 data.forEach((key, value) { | |
| 127 var result = new StringBuffer(); | |
| 128 var search = '%($key)'; | |
| 129 int last = 0, match; | |
| 130 while ((match = format.indexOf(search, last)) >= 0) { | |
| 131 result.add(format.substring(last, match)); | |
| 132 match += search.length; | |
| 133 | |
| 134 int digits = match; | |
| 135 while (isDigit(format[digits])) { | |
| 136 digits++; | |
| 137 } | |
| 138 int numberSize; | |
| 139 if (digits > match) { | |
| 140 numberSize = parseInt(format.substring(match, digits)); | |
| 141 match = digits; | |
| 142 } | |
| 143 | |
| 144 switch (format[match]) { | |
| 145 case 's': | |
| 146 result.add(value); | |
| 147 break; | |
| 148 case 'd': | |
| 149 var number = value.toString(); | |
| 150 result.add(padWithZeros(number, numberSize)); | |
| 151 break; | |
| 152 case 'x': | |
| 153 var number = value.toRadixString(16); | |
| 154 result.add(padWithZeros(number, numberSize)); | |
| 155 break; | |
| 156 default: throw "not implemented: formatStr does not support format " | |
| 157 "character ${format[match]}"; | |
| 158 } | |
| 159 | |
| 160 last = match + 1; | |
| 161 } | |
| 162 | |
| 163 result.add(format.substring(last, format.length)); | |
| 164 format = result.toString(); | |
| 165 }); | |
| 166 | |
| 167 return format; | |
| 168 } | |
| 169 | |
| 170 _ReverseIterable reversed(List list) => new _ReverseIterable(list); | |
| 171 | |
| 172 class _ReverseIterable implements Iterable, Iterator { | |
| 173 int _index; | |
| 174 List _list; | |
| 175 _ReverseIterable(this._list); | |
| 176 Iterator iterator() { | |
| 177 if (_index == null) { | |
| 178 _index = _list.length; | |
| 179 return this; | |
| 180 } else { | |
| 181 return new _ReverseIterable(_list).iterator(); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 bool hasNext() => _index > 0; | |
| 186 next() { | |
| 187 if (_index == 0) throw const NoMoreElementsException(); | |
| 188 return _list[--_index]; | |
| 189 } | |
| 190 } | |
| OLD | NEW |