-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi32.sj
101 lines (83 loc) · 1.63 KB
/
i32.sj
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
u32_maxValue : 4294967295u
i32_maxValue : -1i - 2147483647i
i32_minValue : 2147483647i
i32_max(a : 'i32, b : 'i32)'i32 {
if a < b { b } else { a }
}
i32_min(a : 'i32, b : 'i32)'i32 {
if a < b { a } else { b }
}
i32_random()'i32 {
x := 0
--c--
x = rand();
--c--
x
}
i32_asString(val : 'i32, base : 10) {
count := 0
data := nullptr
--c--
data = (int*)malloc(sizeof(int) + sizeof(char) * 256) + 1;
int* refcount = (int*)data - 1;
*refcount = 1;
char *tmp = (char*)data + 128;
char *tp = (char*)data + 128;
int i;
unsigned v;
int sign = (base == 10 && val < 0);
if (sign)
v = -val;
else
v = (unsigned)val;
while (v || tp == tmp)
{
i = v % base;
v /= base; // v/=base uses less CPU clocks than v=v/base does
if (i < 10)
*tp++ = i + '0';
else
*tp++ = i + 'a' - 10;
}
int len = tp - tmp;
char* sp = (char*)data;
if (sign)
{
*sp++ = '-';
len++;
}
while (tp > tmp)
*sp++ = *--tp;
count = len;
--c--
string(count := count, data := array!char(dataSize := 256, count := count, data := data))
}
i32_compare(l : 'i32, r : 'i32) {
if l == r {
0
} else if l < r {
-1
} else {
1
}
}
i32_hash(val : 'i32)'u32 {
val as u32
}
i32_isEqual(l : 'i32, r : 'i32)'bool {
l == r
}
string_asI32(text : 'string)'i32 {
x := 0
--c--
char* e;
int v = (int)strtol((char*)text->data.data, &e, 10);
if (*e != '\0') {
x = 0;
}
else {
x = v;
}
--c--
x
}