OLD | NEW |
1 /// Misc things that were useful when porting the code from Python. | 1 /// Misc things that were useful when porting the code from Python. |
2 library utils; | 2 library utils; |
3 | 3 |
4 import 'constants.dart'; | 4 import 'constants.dart'; |
5 | 5 |
6 typedef bool Predicate(); | 6 typedef bool Predicate(); |
7 | 7 |
8 class Pair<F, S> { | 8 class Pair<F, S> { |
9 final F first; | 9 final F first; |
10 final S second; | 10 final S second; |
(...skipping 25 matching lines...) Expand all Loading... |
36 bool startsWithAny(String str, List<String> prefixes) { | 36 bool startsWithAny(String str, List<String> prefixes) { |
37 for (var prefix in prefixes) { | 37 for (var prefix in prefixes) { |
38 if (str.startsWith(prefix)) { | 38 if (str.startsWith(prefix)) { |
39 return true; | 39 return true; |
40 } | 40 } |
41 } | 41 } |
42 return false; | 42 return false; |
43 } | 43 } |
44 | 44 |
45 // Like the python [:] operator. | 45 // Like the python [:] operator. |
46 List/*<T>*/ slice/*<T>*/(List/*<T>*/ list, int start, [int end]) { | 46 List<T> slice<T>(List<T> list, int start, [int end]) { |
47 if (end == null) end = list.length; | 47 if (end == null) end = list.length; |
48 if (end < 0) end += list.length; | 48 if (end < 0) end += list.length; |
49 | 49 |
50 // Ensure the indexes are in bounds. | 50 // Ensure the indexes are in bounds. |
51 if (end < start) end = start; | 51 if (end < start) end = start; |
52 if (end > list.length) end = list.length; | 52 if (end > list.length) end = list.length; |
53 return list.sublist(start, end); | 53 return list.sublist(start, end); |
54 } | 54 } |
55 | 55 |
56 bool allWhitespace(String str) { | 56 bool allWhitespace(String str) { |
(...skipping 15 matching lines...) Expand all Loading... |
72 // TODO(jmesserly): this implementation is pretty wrong, but I need something | 72 // TODO(jmesserly): this implementation is pretty wrong, but I need something |
73 // quick until dartbug.com/1694 is fixed. | 73 // quick until dartbug.com/1694 is fixed. |
74 /// Format a string like Python's % string format operator. Right now this only | 74 /// Format a string like Python's % string format operator. Right now this only |
75 /// supports a [data] dictionary used with %s or %08x. Those were the only | 75 /// supports a [data] dictionary used with %s or %08x. Those were the only |
76 /// things needed for [errorMessages]. | 76 /// things needed for [errorMessages]. |
77 String formatStr(String format, Map data) { | 77 String formatStr(String format, Map data) { |
78 if (data == null) return format; | 78 if (data == null) return format; |
79 data.forEach((key, value) { | 79 data.forEach((key, value) { |
80 var result = new StringBuffer(); | 80 var result = new StringBuffer(); |
81 var search = '%($key)'; | 81 var search = '%($key)'; |
82 int last = 0, | 82 int last = 0, match; |
83 match; | |
84 while ((match = format.indexOf(search, last)) >= 0) { | 83 while ((match = format.indexOf(search, last)) >= 0) { |
85 result.write(format.substring(last, match)); | 84 result.write(format.substring(last, match)); |
86 match += search.length; | 85 match += search.length; |
87 | 86 |
88 int digits = match; | 87 int digits = match; |
89 while (isDigit(format[digits])) { | 88 while (isDigit(format[digits])) { |
90 digits++; | 89 digits++; |
91 } | 90 } |
92 int numberSize; | 91 int numberSize; |
93 if (digits > match) { | 92 if (digits > match) { |
(...skipping 20 matching lines...) Expand all Loading... |
114 | 113 |
115 last = match + 1; | 114 last = match + 1; |
116 } | 115 } |
117 | 116 |
118 result.write(format.substring(last, format.length)); | 117 result.write(format.substring(last, format.length)); |
119 format = result.toString(); | 118 format = result.toString(); |
120 }); | 119 }); |
121 | 120 |
122 return format; | 121 return format; |
123 } | 122 } |
OLD | NEW |