forked from square/luhnybin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.dart
71 lines (60 loc) · 1.04 KB
/
util.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#library('util');
inspect(v) {
var s;
if (v is String) {
s = '"$v"';
} else if (v is List) {
var sb = new StringBuffer();
var comma = false;
sb.add("[");
for (var i in v) {
if (comma) {
sb.add(', ');
} else {
comma = true;
}
sb.add(inspect(i));
}
sb.add("]");
s = sb.toString();
} else {
s = '$v';
}
return s;
}
p(v) {
print(inspect(v));
}
class Helpers {
static var compiledPatterns;
static regexp(v) {
if (compiledPatterns === null) {
compiledPatterns = {};
}
var re = compiledPatterns[v];
if (re === null) {
re = new RegExp(v);
compiledPatterns[v] = re;
}
return re;
}
static match(pattern, s) {
return regexp(pattern).firstMatch(s);
}
}
scan(s, pattern) {
var a = [];
var re = pattern is String ? Helpers.regexp(pattern) : pattern;
var mm = re.allMatches(s);
for (var m in mm) {
a.add(m[0]);
}
return a;
}
map(a, fn) {
var b = [];
for (var i in a) {
b.add(fn(i));
}
return b;
}