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; |
11 | 11 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 if (str.length == size) return str; | 64 if (str.length == size) return str; |
65 var result = new StringBuffer(); | 65 var result = new StringBuffer(); |
66 size -= str.length; | 66 size -= str.length; |
67 for (int i = 0; i < size; i++) result.write('0'); | 67 for (int i = 0; i < size; i++) result.write('0'); |
68 result.write(str); | 68 result.write(str); |
69 return result.toString(); | 69 return result.toString(); |
70 } | 70 } |
71 | 71 |
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 /** | 74 /// Format a string like Python's % string format operator. Right now this only |
75 * 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 |
76 * supports a [data] dictionary used with %s or %08x. Those were the only things | 76 /// things needed for [errorMessages]. |
77 * needed for [errorMessages]. | |
78 */ | |
79 String formatStr(String format, Map data) { | 77 String formatStr(String format, Map data) { |
80 if (data == null) return format; | 78 if (data == null) return format; |
81 data.forEach((key, value) { | 79 data.forEach((key, value) { |
82 var result = new StringBuffer(); | 80 var result = new StringBuffer(); |
83 var search = '%($key)'; | 81 var search = '%($key)'; |
84 int last = 0, match; | 82 int last = 0, match; |
85 while ((match = format.indexOf(search, last)) >= 0) { | 83 while ((match = format.indexOf(search, last)) >= 0) { |
86 result.write(format.substring(last, match)); | 84 result.write(format.substring(last, match)); |
87 match += search.length; | 85 match += search.length; |
88 | 86 |
(...skipping 25 matching lines...) Expand all Loading... |
114 | 112 |
115 last = match + 1; | 113 last = match + 1; |
116 } | 114 } |
117 | 115 |
118 result.write(format.substring(last, format.length)); | 116 result.write(format.substring(last, format.length)); |
119 format = result.toString(); | 117 format = result.toString(); |
120 }); | 118 }); |
121 | 119 |
122 return format; | 120 return format; |
123 } | 121 } |
OLD | NEW |