-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbashful-text.inc.sh
223 lines (201 loc) · 5.85 KB
/
bashful-text.inc.sh
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
#!/bin/bash
# Bashful is copyright 2009-2016 Dejay Clayton, all rights reserved:
# https://github.com/dejayc/bashful
# Bashful is licensed under the 2-Clause BSD License:
# http://opensource.org/licenses/BSD-2-Clause
# Declare the module name.
declare BASHFUL_MODULE='text'
# Verify execution context and module dependencies, and register the module.
{
declare BASHFUL_MODULE_VAR="BASHFUL_LOADED_${BASHFUL_MODULE}"
[[ -z "${!BASHFUL_MODULE_VAR-}" ]] || return 0
# Ensure the module is sourced, not executed, generating an error
# otherwise.
[[ "${BASH_ARGV}" != '' ]] || {
echo "ERROR: ${BASH_SOURCE[0]##*/} must be sourced, not executed"
exit 1
} >&2
# Register the module.
declare "${BASHFUL_MODULE_VAR}"="${BASHFUL_MODULE}"
}
# function escapedExtendedRegex
#
# Returns an escaped representation of the passed string, with each character
# preceded by a backslash if the character is a special POSIX extended regular
# expression character. Special characters are '\', '.', '?', '*', '+', '{',
# '}', '-', '^', '$', '|', '(', and ')'.
#
# Examples:
#
# $ escapedExtendedRegex 'Hello? I need $5 (please)'
# Hello\? I need \$5 \(please\)
function escapedExtendedRegex()
{
local REGEX="${1-}"
# These following variables exist to prevent various bash shell versions,
# and even syntax highlighting in different text editors, from becoming
# confused due to discrepancies in how they handle backslash escaping.
local RB='}'
local LP='('
# Replace all the special characters wth their escaped counterparts.
REGEX="${REGEX//[\\]/\\\\}"
REGEX="${REGEX//[.]/\\.}"
REGEX="${REGEX//[?]/\\?}"
REGEX="${REGEX//[*]/\\*}"
REGEX="${REGEX//[+]/\\+}"
REGEX="${REGEX//[{]/\\{}"
REGEX="${REGEX//[\}]/\\${RB}}"
REGEX="${REGEX//[-]/\\-}"
REGEX="${REGEX//[\^]/\\^}"
REGEX="${REGEX//[\$]/\\\$}"
REGEX="${REGEX//[|]/\\|}"
REGEX="${REGEX//[\(]/\\${LP}}"
REGEX="${REGEX//[\)]/\\)}"
echo -n "${REGEX}"
}
# function orderedBracketExpression
#
# Returns an ordered POSIX bracket expression for the passed argument,
# ensuring that within the bracket expression, right-bracket ']' appears
# first, if it appears, and dash '-' appears last, if it appears. All
# other symbols will remain in their present order, and all duplicate symbols
# are discarded. Backslash '\' will be escaped with another backslash,
# appearing as '\\'.
#
# Note that this function is only meant to reorder bracket expressions that
# do not contain character classes, collating symbols, or character ranges.
#
# When using unsanitized variables to dynamically specify the matching
# characters within the POSIX bracket expression of a regular expression,
# guaranteeing the proper order of special characters within the bracket
# expression can help eliminate errors related to the matching process.
#
# Examples:
#
# $ orderedBracketExpression ',;[(\-)]'
# ],;[(\\)-
#
# $ orderedBracketExpression ',;--,--;--'
# ,;-
#
# $ orderedBracketExpression ',;--,]--;--'
# ],;-
function orderedBracketExpression()
{
local EXPR="${1-}"
declare -i HAS_DASH=0
declare -i HAS_RIGHT_BRACKET=0
local ORDERED=''
[[ "${EXPR}" =~ []] ]] && {
let HAS_RIGHT_BRACKET=1
EXPR="${EXPR//]/}"
}
[[ "${EXPR}" =~ - ]] && {
let HAS_DASH=1
EXPR="${EXPR//-/}"
}
local UNIQUE="${EXPR}"
EXPR=''
while [ -n "${UNIQUE}" ]
do
local CHAR="${UNIQUE:0:1}"
[[ "${CHAR}" == '\' ]] && CHAR='\\'
EXPR="${EXPR}${CHAR}"
UNIQUE="${UNIQUE//${CHAR}/}"
done
[[ ${HAS_RIGHT_BRACKET} -eq 0 ]] || echo -n ']'
echo -n "${EXPR}"
[[ ${HAS_DASH} -eq 0 ]] || echo -n '-'
true # true prevents 'set -e' from aborting
}
# function trimmed:
#
# Returns the argument passed into this function, with leading and trailing
# whitespace trimmed. To trim multiple arguments, please refer to the
# function 'translatedList' in 'bashful-list'.
#
# Examples:
#
# $ printf '['; trimmed ''; printf ']'
# []
#
# $ printf '['; trimmed 'none'; printf ']'
# [none]
#
# $ printf '['; trimmed ' leading'; printf ']'
# [leading]
#
# $ printf '['; trimmed 'trailing '; printf ']'
# [trailing]
#
# $ printf '['; trimmed ' both '; printf ']'
# [both]
#
# $ printf '['; trimmed ' embedded ws '; printf ']'
# [embedded ws]
function trimmed()
{
local TEXT="${1-}"
TEXT="${TEXT#"${TEXT%%[![:space:]]*}"}"
echo -n "${TEXT%"${TEXT##*[![:space:]]}"}"
}
# function trimmedLeading:
#
# Returns the argument passed into this function, with leading whitespace
# trimmed. To trim multiple arguments, please refer to the function
# 'translatedList' in 'bashful-list'.
#
# Examples:
#
# $ printf '['; trimmedLeading ''; printf ']'
# []
#
# $ printf '['; trimmedLeading 'none'; printf ']'
# [none]
#
# $ printf '['; trimmedLeading ' leading'; printf ']'
# [leading]
#
# $ printf '['; trimmedLeading 'trailing '; printf ']'
# [trailing ]
#
# $ printf '['; trimmedLeading ' both '; printf ']'
# [both ]
#
# $ printf '['; trimmedLeading ' embedded ws '; printf ']'
# [embedded ws ]
function trimmedLeading()
{
local TEXT="${1-}"
echo -n "${TEXT#"${TEXT%%[![:space:]]*}"}"
}
# function trimmedTrailing:
#
# Returns the argument passed into this function, with trailing whitespace
# trimmed. To trim multiple arguments, please refer to the function
# 'translatedList' in 'bashful-list'.
#
# Examples:
#
# $ printf '['; trimmedTrailing ''; printf ']'
# []
#
# $ printf '['; trimmedTrailing 'none'; printf ']'
# [none]
#
# $ printf '['; trimmedTrailing ' leading'; printf ']'
# [ leading]
#
# $ printf '['; trimmedTrailing 'trailing '; printf ']'
# [trailing]
#
# $ printf '['; trimmedTrailing ' both '; printf ']'
# [ both]
#
# $ printf '['; trimmedTrailing ' embedded ws '; printf ']'
# [ embedded ws]
function trimmedTrailing()
{
local TEXT="${1-}"
echo -n "${TEXT%"${TEXT##*[![:space:]]}"}"
}