blob: 35ac35c2d54688bbdf12b29e2c1be8468a1a77f7 (
plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/; strcmp(~uint8 a, b) [bool]
/; loop (a` == b` && a` !== 0 && b` !== 0) [a++; b++] ;/
/; if (b` == 0 && a` == 0)
return true
;/
return false
;/
# Length of a cstr
/; strlen(~uint8 str) [int]
int out = 0
/; loop (str` !== 0) [str++; out++] ;/
return out
;/
/; base_for_char (uint8 c) [int]
/; if (c == 'b' || c == 'B')
return 2
;; else if (c == 'o' || c == 'O')
return 8
;; else if (c == 'x' || c == 'X')
return 16
;/
return 10
;/
/; decode_bin (uint8 ch) [int]
/; if (ch == '1')
return 1
;/
return 0
;/
/; decode_oct (uint8 ch) [int]
/; if (ch !< '0' && ch !> '7')
return ch - '0'
;/
return 0
;/
/; decode_hex (uint8 ch) [int]
/; if (ch !< '0' && ch !> '9')
return ch - '0'
;; else if (ch !< 'a' && ch !> 'f')
return ch - 'a'
;; else if (ch !< 'A' && ch !> 'F')
return ch - 'A'
;/
return 0
;/
/; decode_dec (uint8 ch) [int]
_print_num(NUM_STR, ch)
/; if (ch !< '0' && ch !> '9')
return ch - '0'
;/
return 0
;/
/; cstr_to_int(~uint8 str) [int]
int base = 10
int start = 0
int l = strlen(str)
# Get base
/; if (l > start + 2 && str{start} == '0')
/; if (str{start + 1} > '9' || str{start + 1} < '0')
base = base_for_char(str{start + 1})
start = 2
;/
;/
int out = 0
/; loop (start < l) [start++]
out = out * base
int decoded = 0
_print_num(NUM_STR, start)
_print_num(NUM_STR, str{start})
/; if (base == 2)
decoded = decode_bin(str{start})
;; else if (base == 8)
decoded = decode_oct(str{start})
;; else if (base == 10)
decoded = decode_dec(str{start})
;; else if (base == 16)
decoded = decode_hex(str{start})
;/
out = out + decoded
;/
return out
;/
/; cstr_to_uint(~uint8 str) [uint]
int base = 10
int start = 0
int l = strlen(str)
# Get base
/; if (l > start + 2 && str{start} == '0')
/; if (str{start + 1} > '9' || str{start + 1} < '0')
base = base_for_char(str{start + 1})
start = 2
;/
;/
uint out = 0
/; loop (start < l) [start++]
out = out * base
uint decoded = 0
/; if (base == 2)
decoded = decode_bin(str{start})
;; else if (base == 8)
decoded = decode_oct(str{start})
;; else if (base == 10)
decoded = decode_dec(str{start})
;; else if (base == 16)
decoded = decode_hex(str{start})
;/
out = out + decoded
;/
return out
;/
/; reverse_str (~uint8 str)
uint8 tmp
int l = strlen(str)
/; loop(int i = 0; i < l / 2) [i++]
tmp = str{i}
str{i} = str{(l - 1) - i}
str{(l - 1) - i} = tmp
;/
;/
/; uint_to_hex_str(uint i) [~uint8]
Vector out
out.init(1)
out.push_char('0')
out.push_char('x')
/; loop (i > 0) [i = i / 16]
int n = i % 16
/; if (n > 9)
out.push_char('a' + n - 10)
;; else
out.push_char('0' + n)
;/
;/
/; if (out.count < 3)
out.push_char('0')
;; else
~uint8 str = out.as_cstr()
reverse_str(str + 2)
;/
return out.as_cstr()
;/
/; int_to_str(int i) [~uint8]
Vector out
out.init(1)
/; if (i < 0)
out.push_char('-')
i = 0 - i
;/
/; loop (i > 0) [i = i / 10]
int n = i % 10
out.push_char('0' + n)
;/
/; if (out.count < 1)
out.push_char('0')
;/
~uint8 str = out.as_cstr()
/; if (str` == '-')
reverse_str(str + 1)
;; else
reverse_str(str)
;/
return str
;/
/; strcpy(~uint8 cstr) [~uint8]
Vector out
out.from_cstr(cstr)
return out.as_cstr()
;/
/; stradd(~uint8 a, ~uint8 b) [~uint8]
Vector out
out.from_cstr(a)
out.push_cstr(b)
return out.as_cstr()
;/
/; unquote_cha(~uint8 cha) [uint8]
/; if (cha` !== '\\')
return cha`
;/
cha++
/; if (cha` == '\"')
return '\"'
;; else if (cha` == '\'')
return '\''
;; else if (cha` == '\\')
return '\\'
;; else if (cha` == 'n')
return '\n'
;; else if (cha` == 't')
return '\t'
;; else if (cha` == '0')
return 0
;/
return cha`
;/
/; unquote_str(~uint8 str) [~uint8]
Vector out
out.init(1)
/; loop (str++; str` !== 0 && str` !== '\"') [str++]
uint8 buf = str`
/; if (str` == '\\')
buf = unquote_cha(str)
str++
;/
out.push(~buf)
;/
return out.as_cstr()
;/
|