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

Side by Side Diff: pkg/third_party/html5lib/lib/src/utils.dart

Issue 814113004: Pull args, intl, logging, shelf, and source_maps out of the SDK. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Also csslib. Created 6 years 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 | Annotate | Revision Log
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, match;
83 while ((match = format.indexOf(search, last)) >= 0) {
84 result.write(format.substring(last, match));
85 match += search.length;
86
87 int digits = match;
88 while (isDigit(format[digits])) {
89 digits++;
90 }
91 int numberSize;
92 if (digits > match) {
93 numberSize = int.parse(format.substring(match, digits));
94 match = digits;
95 }
96
97 switch (format[match]) {
98 case 's':
99 result.write(value);
100 break;
101 case 'd':
102 var number = value.toString();
103 result.write(padWithZeros(number, numberSize));
104 break;
105 case 'x':
106 var number = value.toRadixString(16);
107 result.write(padWithZeros(number, numberSize));
108 break;
109 default: throw "not implemented: formatStr does not support format "
110 "character ${format[match]}";
111 }
112
113 last = match + 1;
114 }
115
116 result.write(format.substring(last, format.length));
117 format = result.toString();
118 });
119
120 return format;
121 }
OLDNEW
« no previous file with comments | « pkg/third_party/html5lib/lib/src/treebuilder.dart ('k') | pkg/third_party/html5lib/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698