-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.hx
114 lines (105 loc) · 2.07 KB
/
Utils.hx
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Utils {
public static function map<T,U> (f : U -> T, l : Array<U>) : Array<T>{
var rv : Array<T> = new Array<T>();
for (it in l) {
rv.push(f(it));
}
return rv;
}
public static function filter<T> (f : T -> Bool, l : Array<T>)
: Array<T> {
var l_ = new Array<T>();
for (it in l) {
if(f(it)) {
l_.push(it);
}
}
return l_;
}
public static function halt() {
while(true) {}
}
public static function parseBool (digit : String) : Bool {
switch(digit) {
case "0":
return false;
case "1":
return true;
}
throw("bad bool");
}
static function parseHexDigit (digit : String) : Int {
switch(digit) {
case "0":
return 0;
case "1":
return 1;
case "2":
return 2;
case "3":
return 3;
case "4":
return 4;
case "5":
return 5;
case "6":
return 6;
case "7":
return 7;
case "8":
return 8;
case "9":
return 9;
case "a", "A":
return 10;
case "b", "B":
return 11;
case "c", "C":
return 12;
case "d", "D":
return 13;
case "e", "E":
return 14;
case "f", "F":
return 15;
}
throw("Bad hex digit");
}
public static function parseHex (hex : String) : Int {
var rv : Int = 0;
while (hex.length > 0) {
var digit = hex.substr(0, 1);
hex = hex.substr(1);
rv *= 16;
rv += parseHexDigit(digit);
}
return rv;
}
/*
public static function stackTrace (as : Array<String>) : String {
var s = new String();
for (i in as) {
s.append(i);
}
return s;
}
public static function myHandler(msg : String, stack : Array<String>) {
trace("We done fucked up: " + msg);
trace(stackTrace(stack));
}
*/
//public static function dump(o : Dynamic) {
// for (i in o) {
// }
//}
}
enum Option<T> {
none;
some(t:T);
}
class Ref<T> {
public var val : T;
public function new(t : T) {
val = t;
}
}