Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: html/lib/src/utils.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « html/lib/src/treebuilder.dart ('k') | html/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /// Misc things that were useful when porting the code from Python.
2 library utils;
3
4 import 'constants.dart';
5
6 typedef bool Predicate();
7
8 class Pair<F, S> {
9 final F first;
10 final S second;
11
12 const Pair(this.first, this.second);
13
14 int get hashCode => 37 * first.hashCode + second.hashCode;
15 bool operator ==(other) => other.first == first && other.second == second;
16 }
17
18 int parseIntRadix(String str, [int radix = 10]) {
19 int val = 0;
20 for (int i = 0; i < str.length; i++) {
21 var digit = str.codeUnitAt(i);
22 if (digit >= LOWER_A) {
23 digit += 10 - LOWER_A;
24 } else if (digit >= UPPER_A) {
25 digit += 10 - UPPER_A;
26 } else {
27 digit -= ZERO;
28 }
29 val = val * radix + digit;
30 }
31 return val;
32 }
33
34 bool any(List<bool> iterable) => iterable.any((f) => f);
35
36 bool startsWithAny(String str, List<String> prefixes) {
37 for (var prefix in prefixes) {
38 if (str.startsWith(prefix)) {
39 return true;
40 }
41 }
42 return false;
43 }
44
45 // Like the python [:] operator.
46 List slice(List list, int start, [int end]) {
47 if (end == null) end = list.length;
48 if (end < 0) end += list.length;
49
50 // Ensure the indexes are in bounds.
51 if (end < start) end = start;
52 if (end > list.length) end = list.length;
53 return list.sublist(start, end);
54 }
55
56 bool allWhitespace(String str) {
57 for (int i = 0; i < str.length; i++) {
58 if (!isWhitespaceCC(str.codeUnitAt(i))) return false;
59 }
60 return true;
61 }
62
63 String padWithZeros(String str, int size) {
64 if (str.length == size) return str;
65 var result = new StringBuffer();
66 size -= str.length;
67 for (int i = 0; i < size; i++) result.write('0');
68 result.write(str);
69 return result.toString();
70 }
71
72 // TODO(jmesserly): this implementation is pretty wrong, but I need something
73 // quick until dartbug.com/1694 is fixed.
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
76 /// things needed for [errorMessages].
77 String formatStr(String format, Map data) {
78 if (data == null) return format;
79 data.forEach((key, value) {
80 var result = new StringBuffer();
81 var search = '%($key)';
82 int last = 0,
83 match;
84 while ((match = format.indexOf(search, last)) >= 0) {
85 result.write(format.substring(last, match));
86 match += search.length;
87
88 int digits = match;
89 while (isDigit(format[digits])) {
90 digits++;
91 }
92 int numberSize;
93 if (digits > match) {
94 numberSize = int.parse(format.substring(match, digits));
95 match = digits;
96 }
97
98 switch (format[match]) {
99 case 's':
100 result.write(value);
101 break;
102 case 'd':
103 var number = value.toString();
104 result.write(padWithZeros(number, numberSize));
105 break;
106 case 'x':
107 var number = value.toRadixString(16);
108 result.write(padWithZeros(number, numberSize));
109 break;
110 default:
111 throw "not implemented: formatStr does not support format "
112 "character ${format[match]}";
113 }
114
115 last = match + 1;
116 }
117
118 result.write(format.substring(last, format.length));
119 format = result.toString();
120 });
121
122 return format;
123 }
OLDNEW
« no previous file with comments | « html/lib/src/treebuilder.dart ('k') | html/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698