-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGawkStringFunctions.gawk
More file actions
70 lines (59 loc) · 1.95 KB
/
GawkStringFunctions.gawk
File metadata and controls
70 lines (59 loc) · 1.95 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
# AWK String-processing functions
function RTrim(src) { sub(/[ \t]+$/, "", src); return src }
function LTrim(src) { sub(/^[ \t]+/, "", src); return src }
#function Trim(src) { return LTrim(RTrim(src)) }
# GAWK ONLY!
function Trim(src) {
return gensub(/[ \t]+$/, "", 1, gensub(/^[ \t]+/, "", 1, src))
}
function Repeat(rln, pchr, result) {
if (rln > 0) {
result = sprintf("%*s", rln, pchr)
gsub(/ /, pchr, result)
}
else result = ""
return result
}
function LPad(src, rln, pchr) {
return (Repeat(rln - length(src), pchr) src)
}
function RPad(src, rln, pchr) {
return (src Repeat(rln - length(src), pchr))
}
function ELength(src, tln) {
gsub(/[\t]/, Repeat(tln ? tln : 8, " "), src)
return length(src)
}
function Center(src, cln, pchr, rln) {
src = Trim(src)
cln = cln - length(src)
rln = int(cln / 2)
return (Repeat(rln, pchr) src Repeat(cln - rln, pchr))
}
#*******************************************************************************
#** TrimChar
#**
#** Trims one (if multi == 0) or all (if multi > 0) occurences of char from
#** the beginning and end of src
#**
#** Arguments:
#**
#** src - string to be trimmed. Modified in place.
#** char - char to be trimmed from src
#** multi - indicates whether or not multiple consecutive occurences of char
#** will be trimmed
#**
#** Return values: modified string
#*******************************************************************************
function TrimChar(src, char, multi)
{
# Create the regexp to trim the chars from the beginning.
# If we should trim multiple occurences of the sequence,
# append the "+" operator to the expression
sRegExpBegin = "^[" char "]" (multi > 0 ? "+" : "")
# Create the regexp to trim the chars from the end.
# If we should trim multiple occurences of the sequence,
# append the "+" operator to the expression
sRegExpEnd = "[" char (multi > 0 ? "]+$" : "]$")
return gensub(sRegExpEnd, "", 1, gensub(sRegExpBegin, "", 1, src))
}