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

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

Issue 22375011: move html5lib code into dart svn repo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: change location of html5lib to pkg/third_party/html5lib Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /** Misc things that were useful when porting the code from Python. */
2 library utils;
3
4 import 'dart:collection';
5 import 'constants.dart';
6
7 typedef bool Predicate();
8
9 class Pair<F, S> {
10 final F first;
11 final S second;
12
13 const Pair(this.first, this.second);
14
15 int get hashCode => 37 * first.hashCode + second.hashCode;
16 bool operator ==(other) => other.first == first && other.second == second;
17 }
18
19 int parseIntRadix(String str, [int radix = 10]) {
20 int val = 0;
21 for (int i = 0; i < str.length; i++) {
22 var digit = str.codeUnitAt(i);
23 if (digit >= LOWER_A) {
24 digit += 10 - LOWER_A;
25 } else if (digit >= UPPER_A) {
26 digit += 10 - UPPER_A;
27 } else {
28 digit -= ZERO;
29 }
30 val = val * radix + digit;
31 }
32 return val;
33 }
34
35 bool any(List<bool> iterable) => iterable.any((f) => f);
36
37 bool startsWithAny(String str, List<String> prefixes) {
38 for (var prefix in prefixes) {
39 if (str.startsWith(prefix)) {
40 return true;
41 }
42 }
43 return false;
44 }
45
46 // Like the python [:] operator.
47 List slice(List list, int start, [int end]) {
48 if (end == null) end = list.length;
49 if (end < 0) end += list.length;
50
51 // Ensure the indexes are in bounds.
52 if (end < start) end = start;
53 if (end > list.length) end = list.length;
54 return list.sublist(start, end);
55 }
56
57 bool allWhitespace(String str) {
58 for (int i = 0; i < str.length; i++) {
59 if (!isWhitespaceCC(str.codeUnitAt(i))) return false;
60 }
61 return true;
62 }
63
64 String padWithZeros(String str, int size) {
65 if (str.length == size) return str;
66 var result = new StringBuffer();
67 size -= str.length;
68 for (int i = 0; i < size; i++) result.write('0');
69 result.write(str);
70 return result.toString();
71 }
72
73 // TODO(jmesserly): this implementation is pretty wrong, but I need something
74 // quick until dartbug.com/1694 is fixed.
75 /**
76 * Format a string like Python's % string format operator. Right now this only
77 * supports a [data] dictionary used with %s or %08x. Those were the only things
78 * needed for [errorMessages].
79 */
80 String formatStr(String format, Map data) {
81 if (data == null) return format;
82 data.forEach((key, value) {
83 var result = new StringBuffer();
84 var search = '%($key)';
85 int last = 0, match;
86 while ((match = format.indexOf(search, last)) >= 0) {
87 result.write(format.substring(last, match));
88 match += search.length;
89
90 int digits = match;
91 while (isDigit(format[digits])) {
92 digits++;
93 }
94 int numberSize;
95 if (digits > match) {
96 numberSize = int.parse(format.substring(match, digits));
97 match = digits;
98 }
99
100 switch (format[match]) {
101 case 's':
102 result.write(value);
103 break;
104 case 'd':
105 var number = value.toString();
106 result.write(padWithZeros(number, numberSize));
107 break;
108 case 'x':
109 var number = value.toRadixString(16);
110 result.write(padWithZeros(number, numberSize));
111 break;
112 default: throw "not implemented: formatStr does not support format "
113 "character ${format[match]}";
114 }
115
116 last = match + 1;
117 }
118
119 result.write(format.substring(last, format.length));
120 format = result.toString();
121 });
122
123 return format;
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698