forked from njm64/zawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.awk
59 lines (52 loc) · 1.09 KB
/
util.awk
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
function ord_init( i, c) {
# Build a mapping table for converting input characters to integers.
# The locale needs to be set to C for this to work correctly.
# e.g. set LC_ALL=C in the environment
for(i = 0; i < 256; i++) {
c = sprintf("%c", i)
ord[c] = i
chr[i] = c
}
}
function logand(a, b, c, m) {
for(m = 32768; m > 0; m = int(m / 2)) {
if(a >= m && b >= m) {
c += m
}
a = a % m
b = b % m
}
return c
}
function logior(a, b, c, m) {
for(m = 32768; m > 0; m = int(m / 2)) {
if(a >= m || b >= m) {
c += m
}
a = a % m
b = b % m
}
return c
}
function lognot(a, b, m) {
for(m = 32768; m > 0; m = int(m / 2)) {
if(a >= m) {
a -= m
} else {
b += m
}
}
return b
}
function test_bit(a, n) {
return int(a / (2 ^ n)) % 2
}
function to_s16(val) {
return val > 32767 ? val - 65536 : val
}
function to_u16(val) {
return val < 0 ? val + 65536 : val % 65536
}
BEGIN {
ord_init()
}