-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdutils.csp
More file actions
406 lines (375 loc) · 13 KB
/
stdutils.csp
File metadata and controls
406 lines (375 loc) · 13 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Standard Library Utilities
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright (C) 2017-2023 Michael Lee(李登淳)
#
# Email: lee@covariant.cn, mikecovlee@163.com
# Github: https://github.com/mikecovlee
# Website: http://covscript.org.cn
package stdutils
import codec.json as json
import bitwise
namespace arr
# Print Vector into system.out
# Return: null
function print(arr)
system.out.print("{")
for i = 0, i < arr.size, ++i
try
system.out.print(arr[i])
catch e
if typeid arr[i] == typeid array
print(arr[i])
else
system.out.print(type(arr[i]))
end
end
if i != arr.size - 1
system.out.print(", ")
end
end
system.out.println("}")
end
# Create Multi-Dimension Vector
# Return: Array filled by zero
function create(...dim)
if dim.size == 0
throw runtime.exception("stdutils.arr.create: unexpected argument size(= 0)")
end
if dim.size > 1
var d = dim.pop_front()
if d <= 0
throw runtime.exception("stdutils.arr.create: unexpected dimension(<= 0)")
end
var arr_ldim = create(dim...), arr = new array
foreach i in range(d) do arr.push_back(arr_ldim)
return move(arr)
else
if dim[0] <= 0
throw runtime.exception("stdutils.arr.create: unexpected dimension(<= 0)")
end
var arr = new array
foreach i in range(dim[0]) do arr.push_back(0)
return move(arr)
end
end
# Insert val before pos in arr
# Return: iterator pointing to inserted value
function insert(arr, pos, val)
if pos >= arr.size
throw runtime.exception("stdutils.arr.insert: out of range(pos >= arr.size)")
end
var it = arr.begin
it.next_n(pos)
return arr.insert(it, val)
end
# Insert val in arr[pos]
# Return: iterator pointing to arr[pos + 1]
function erase(arr, pos)
if pos >= arr.size
throw runtime.exception("stdutils.arr.insert: out of range(pos >= arr.size)")
end
var it = arr.begin
it.next_n(pos)
return arr.erase(it)
end
end
context.add_literal("hex", bitwise.hex_literal)
@begin
var crc32_tab =
{
"0x00000000"hex, "0x77073096"hex, "0xee0e612c"hex, "0x990951ba"hex, "0x076dc419"hex,
"0x706af48f"hex, "0xe963a535"hex, "0x9e6495a3"hex, "0x0edb8832"hex, "0x79dcb8a4"hex,
"0xe0d5e91e"hex, "0x97d2d988"hex, "0x09b64c2b"hex, "0x7eb17cbd"hex, "0xe7b82d07"hex,
"0x90bf1d91"hex, "0x1db71064"hex, "0x6ab020f2"hex, "0xf3b97148"hex, "0x84be41de"hex,
"0x1adad47d"hex, "0x6ddde4eb"hex, "0xf4d4b551"hex, "0x83d385c7"hex, "0x136c9856"hex,
"0x646ba8c0"hex, "0xfd62f97a"hex, "0x8a65c9ec"hex, "0x14015c4f"hex, "0x63066cd9"hex,
"0xfa0f3d63"hex, "0x8d080df5"hex, "0x3b6e20c8"hex, "0x4c69105e"hex, "0xd56041e4"hex,
"0xa2677172"hex, "0x3c03e4d1"hex, "0x4b04d447"hex, "0xd20d85fd"hex, "0xa50ab56b"hex,
"0x35b5a8fa"hex, "0x42b2986c"hex, "0xdbbbc9d6"hex, "0xacbcf940"hex, "0x32d86ce3"hex,
"0x45df5c75"hex, "0xdcd60dcf"hex, "0xabd13d59"hex, "0x26d930ac"hex, "0x51de003a"hex,
"0xc8d75180"hex, "0xbfd06116"hex, "0x21b4f4b5"hex, "0x56b3c423"hex, "0xcfba9599"hex,
"0xb8bda50f"hex, "0x2802b89e"hex, "0x5f058808"hex, "0xc60cd9b2"hex, "0xb10be924"hex,
"0x2f6f7c87"hex, "0x58684c11"hex, "0xc1611dab"hex, "0xb6662d3d"hex, "0x76dc4190"hex,
"0x01db7106"hex, "0x98d220bc"hex, "0xefd5102a"hex, "0x71b18589"hex, "0x06b6b51f"hex,
"0x9fbfe4a5"hex, "0xe8b8d433"hex, "0x7807c9a2"hex, "0x0f00f934"hex, "0x9609a88e"hex,
"0xe10e9818"hex, "0x7f6a0dbb"hex, "0x086d3d2d"hex, "0x91646c97"hex, "0xe6635c01"hex,
"0x6b6b51f4"hex, "0x1c6c6162"hex, "0x856530d8"hex, "0xf262004e"hex, "0x6c0695ed"hex,
"0x1b01a57b"hex, "0x8208f4c1"hex, "0xf50fc457"hex, "0x65b0d9c6"hex, "0x12b7e950"hex,
"0x8bbeb8ea"hex, "0xfcb9887c"hex, "0x62dd1ddf"hex, "0x15da2d49"hex, "0x8cd37cf3"hex,
"0xfbd44c65"hex, "0x4db26158"hex, "0x3ab551ce"hex, "0xa3bc0074"hex, "0xd4bb30e2"hex,
"0x4adfa541"hex, "0x3dd895d7"hex, "0xa4d1c46d"hex, "0xd3d6f4fb"hex, "0x4369e96a"hex,
"0x346ed9fc"hex, "0xad678846"hex, "0xda60b8d0"hex, "0x44042d73"hex, "0x33031de5"hex,
"0xaa0a4c5f"hex, "0xdd0d7cc9"hex, "0x5005713c"hex, "0x270241aa"hex, "0xbe0b1010"hex,
"0xc90c2086"hex, "0x5768b525"hex, "0x206f85b3"hex, "0xb966d409"hex, "0xce61e49f"hex,
"0x5edef90e"hex, "0x29d9c998"hex, "0xb0d09822"hex, "0xc7d7a8b4"hex, "0x59b33d17"hex,
"0x2eb40d81"hex, "0xb7bd5c3b"hex, "0xc0ba6cad"hex, "0xedb88320"hex, "0x9abfb3b6"hex,
"0x03b6e20c"hex, "0x74b1d29a"hex, "0xead54739"hex, "0x9dd277af"hex, "0x04db2615"hex,
"0x73dc1683"hex, "0xe3630b12"hex, "0x94643b84"hex, "0x0d6d6a3e"hex, "0x7a6a5aa8"hex,
"0xe40ecf0b"hex, "0x9309ff9d"hex, "0x0a00ae27"hex, "0x7d079eb1"hex, "0xf00f9344"hex,
"0x8708a3d2"hex, "0x1e01f268"hex, "0x6906c2fe"hex, "0xf762575d"hex, "0x806567cb"hex,
"0x196c3671"hex, "0x6e6b06e7"hex, "0xfed41b76"hex, "0x89d32be0"hex, "0x10da7a5a"hex,
"0x67dd4acc"hex, "0xf9b9df6f"hex, "0x8ebeeff9"hex, "0x17b7be43"hex, "0x60b08ed5"hex,
"0xd6d6a3e8"hex, "0xa1d1937e"hex, "0x38d8c2c4"hex, "0x4fdff252"hex, "0xd1bb67f1"hex,
"0xa6bc5767"hex, "0x3fb506dd"hex, "0x48b2364b"hex, "0xd80d2bda"hex, "0xaf0a1b4c"hex,
"0x36034af6"hex, "0x41047a60"hex, "0xdf60efc3"hex, "0xa867df55"hex, "0x316e8eef"hex,
"0x4669be79"hex, "0xcb61b38c"hex, "0xbc66831a"hex, "0x256fd2a0"hex, "0x5268e236"hex,
"0xcc0c7795"hex, "0xbb0b4703"hex, "0x220216b9"hex, "0x5505262f"hex, "0xc5ba3bbe"hex,
"0xb2bd0b28"hex, "0x2bb45a92"hex, "0x5cb36a04"hex, "0xc2d7ffa7"hex, "0xb5d0cf31"hex,
"0x2cd99e8b"hex, "0x5bdeae1d"hex, "0x9b64c2b0"hex, "0xec63f226"hex, "0x756aa39c"hex,
"0x026d930a"hex, "0x9c0906a9"hex, "0xeb0e363f"hex, "0x72076785"hex, "0x05005713"hex,
"0x95bf4a82"hex, "0xe2b87a14"hex, "0x7bb12bae"hex, "0x0cb61b38"hex, "0x92d28e9b"hex,
"0xe5d5be0d"hex, "0x7cdcefb7"hex, "0x0bdbdf21"hex, "0x86d3d2d4"hex, "0xf1d4e242"hex,
"0x68ddb3f8"hex, "0x1fda836e"hex, "0x81be16cd"hex, "0xf6b9265b"hex, "0x6fb077e1"hex,
"0x18b74777"hex, "0x88085ae6"hex, "0xff0f6a70"hex, "0x66063bca"hex, "0x11010b5c"hex,
"0x8f659eff"hex, "0xf862ae69"hex, "0x616bffd3"hex, "0x166ccf45"hex, "0xa00ae278"hex,
"0xd70dd2ee"hex, "0x4e048354"hex, "0x3903b3c2"hex, "0xa7672661"hex, "0xd06016f7"hex,
"0x4969474d"hex, "0x3e6e77db"hex, "0xaed16a4a"hex, "0xd9d65adc"hex, "0x40df0b66"hex,
"0x37d83bf0"hex, "0xa9bcae53"hex, "0xdebb9ec5"hex, "0x47b2cf7f"hex, "0x30b5ffe9"hex,
"0xbdbdf21c"hex, "0xcabac28a"hex, "0x53b39330"hex, "0x24b4a3a6"hex, "0xbad03605"hex,
"0xcdd70693"hex, "0x54de5729"hex, "0x23d967bf"hex, "0xb3667a2e"hex, "0xc4614ab8"hex,
"0x5d681b02"hex, "0x2a6f2b94"hex, "0xb40bbe37"hex, "0xc30c8ea1"hex, "0x5a05df1b"hex,
"0x2d02ef8d"hex
}
@end
function crc32(str)
var crc32val = "0xFFFFFFFF"hex
foreach ch in str
crc32val = crc32_tab.at(crc32val.logic_xor(bitwise.bitset.from_number(to_integer(ch))).logic_and("0xFF"hex).to_number()).logic_xor(crc32val.shift_right(8).logic_and("0x00FFFFFF"hex))
end
return crc32val.logic_xor("0xFFFFFFFF"hex).to_hash()
end
function crc32_file(path)
var ifs = iostream.fstream(path, iostream.openmode.bin_in)
if !ifs.good()
return 0
end
var crc32val = "0xFFFFFFFF"hex
loop
var ch = ifs.get()
if ifs.eof()
break
end
crc32val = crc32_tab.at(crc32val.logic_xor(bitwise.bitset.from_number(to_integer(ch))).logic_and("0xFF"hex).to_number()).logic_xor(crc32val.shift_right(8).logic_and("0x00FFFFFF"hex))
end
return crc32val.logic_xor("0xFFFFFFFF"hex).to_hash()
end
# Console Progress Bar
class progress_bar
var last_percentage = -1
var progress = 0
var total = 100
var width = 50
function construct(_total)
total = _total
end
function reset()
last_percentage = -1
progress = 0
end
function show()
var percentage = progress/total
var black_blocks = to_integer(percentage*width)
var white_blocks = width - black_blocks
if last_percentage == black_blocks
return
end
last_percentage = black_blocks
system.console.echo(false)
system.out.print("\r")
foreach i in range(width + 5) do system.out.print(' ')
system.out.print("\r")
system.out.print("[")
foreach i in range(black_blocks) do system.out.print("#")
foreach i in range(white_blocks) do system.out.print(" ")
system.out.print("] " + to_integer(percentage*100) + "%")
system.console.echo(true)
end
function finish()
progress = total
show()
system.out.println("")
reset()
end
end
# JSON Utils
function open_json(path)
var ifs = iostream.ifstream(path)
return json.to_var(json.from_stream(ifs))
end
function save_json(val, path)
var ofs = iostream.ofstream(path)
ofs.print(json.to_string(json.from_var(val)))
end
# CSV Reader
function read_csv(file_name)
var ifs = iostream.ifstream(file_name)
if !ifs.good()
return null
end
var data = new array, line = new array, buff = new string
var input_escapes = 0
var escape_stack = 0
loop
var ch = ifs.get()
if ifs.eof()
line.push_back(buff)
data.push_back(line)
break
end
if escape_stack > 0
if ch == '\"'
++input_escapes
end
if input_escapes - escape_stack == 1
++escape_stack
input_escapes = 0
else
if input_escapes == escape_stack
--escape_stack
input_escapes = 0
end
end
buff += ch
continue
end
if ch == '\"'
++escape_stack
end
if ch == ','
line.push_back(buff)
buff = new string
continue
end
if ch == '\n'
line.push_back(buff)
buff = new string
data.push_back(line)
line.clear()
continue
end
buff += ch
end
return move(data)
end
# Coroutine Utils
class coroutine_queue
var data = new array
var incoming = false
function yield(...args)
if !args.empty()
data = args
incoming = true
else
incoming = false
end
runtime.yield()
end
function avail()
return incoming
end
function get()
if !incoming
return null
end
incoming = false
if data.size == 1
return data.front
else
return data
end
end
end
function coroutine_worker(obj, args)
obj->running = true
obj->func(obj->queue, args...)
obj->running = false
end
namespace coroutine_status
constant normal = 0
constant error = -1
constant finish = -2
end
class coroutine
var co = null
var func = null
var running = false
var queue = new coroutine_queue
function construct(_func)
func = _func
end
function join(...args)
co = runtime.create_co_s(coroutine_worker, {&this, args})
return runtime.resume(co)
end
function detach(...args)
co = runtime.create_co_s(coroutine_worker, {&this, args})
end
function resume(...args)
if !args.empty()
queue.data = args
queue.incoming = true
else
queue.incoming = false
end
return runtime.resume(co)
end
function avail()
return queue.incoming
end
function get()
if !queue.incoming
return null
end
queue.incoming = false
if queue.data.size == 1
return queue.data.front
else
return queue.data
end
end
end
# String Formatter
function format(fmt, map)
var str = new string
var fmt_buff = new string
var fmt_flag = 0
foreach ch in fmt
if ch == '{'
++fmt_flag
continue
end
if fmt_flag == 1
if ch == '}'
if --fmt_flag == 0
if map.exist(fmt_buff)
str += map.at(fmt_buff)
fmt_buff = new string
else
str += "{}"
end
continue
end
end
fmt_buff += ch
continue
end
if fmt_flag > 1
foreach i in range(fmt_flag) do str += '{'
fmt_flag = 0
end
str += ch
end
return move(str)
end