-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.awk
More file actions
51 lines (41 loc) · 1.29 KB
/
stdlib.awk
File metadata and controls
51 lines (41 loc) · 1.29 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
# String manipulation functions
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
function sleep(seconds) {
system("sleep " seconds)
}
function center(s, screen_width) {
half_width = int(screen_width/2)
printf "%" int(half_width+length(s)/2) "s\n", s
}
function repeat( str, n, rep, i){
for(i=0; i<n; i++ )
rep = rep str
return rep
}
# List functions
function init(list) {}
function append(list, item) {
list[length(list)+1] = item
}
function remove(list, idx, n) {
if (idx == 1 && length(list) == 1) {
delete list[idx]
} else if (1 <= idx && idx <= length(list)) {
delete list[idx]
n = asort(list)
}
}
function array_length(array, l) {l = 0; for (item in array) {l++}; return l}
# Randomness
function randint(min, max) {return int((rand() * (max - min + 1))) + min}
function randchar(string) {return substr(string, randint(1, length(string)), 1)}
function chance(probability) {return randint(0, 100) > probability}
# Math
function min(x, y) { return x < y ? x : y }
function max(x, y) { return x < y ? y : x }
function abs(v) {return v < 0 ? -v : v}
function round(nr) {
return int(sprintf("%d\n",nr + 0.5))
}