aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/text.sh
blob: e9d97022fa848c043b733994c654671995e9f28d (plain) (blame)
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
#!/usr/bin/env bash

# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Linux/Cygwin environment" project.
# For details, see https://github.com/egor-tensin/linux-home.
# Distributed under the MIT License.

alias dos2eol='sed --binary --in-place -- '"'"'s/\(\r\?\)$//'"'"
alias eol2dos='sed --binary --in-place -- '"'"'s/\r\?$/\r/'"'"

alias trim='sed --binary --in-place -- '"'"'s/[[:blank:]]*\(\r\?\)$/\1/'"'"

alias trimeol='sed --binary --in-place -e :a -e '"'"'/^\n*$/{$d;N;ba}'"'"' --'
alias trimdoseol='sed --binary --in-place -e :a -e '"'"'/^\(\r\n\)*\r$/{$d;N;ba}'"'"' --'

alias eol='sed --binary --in-place -- '"'"'$a\'"'"
alias doseol='sed --binary --in-place -- '"'"'$s/\r\?$/\r/;a\'"'"

alias trimbom='sed --binary --in-place -- '"'"'1 s/^\xef\xbb\xbf//'"'"

lint() {
    trim "$@" && trimeol "$@" && eol "$@"
}

doslint() {
    trim "$@" && trimdoseol "$@" && doseol "$@"
}

_sed_escape_pattern() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} STR" >&2
        return 1
    fi

    # Only $^*./\[] need to be escaped according to this:
    # https://unix.stackexchange.com/a/33005/60124
    local s="$1"
    s="${s//'\'/'\\'}"
    s="${s//'/'/'\/'}"
    s="${s//'$'/'\$'}"
    s="${s//'^'/'\^'}"
    s="${s//'*'/'\*'}"
    s="${s//'.'/'\.'}"
    s="${s//'['/'\['}"
    s="${s//']'/'\]'}"
    s="${s//$'\n'/'\n'}"
    echo "$s"
)

_sed_escape_substitution() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} STR" >&2
        return 1
    fi

    local s="$1"
    s="${s//'\'/'\\'}"
    s="${s//'/'/'\/'}"
    s="${s//'&'/'\&'}"
    s="${s//$'\n'/'\n'}"
    echo "$s"
)

file_replace() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -lt 3 ]; then
        echo "usage: ${FUNCNAME[0]} OLD NEW PATH..." >&2
        return 1
    fi

    local old="$1"
    old="$( _sed_escape_pattern "$old" )"
    shift
    local new="$1"
    new="$( _sed_escape_substitution "$new" )"
    shift

    sed --binary --in-place -- "s/$old/$new/g" "$@"
)

file_replace_word() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -lt 3 ]; then
        echo "usage: ${FUNCNAME[0]} OLD NEW PATH..." >&2
        return 1
    fi

    local old="$1"
    old="$( _sed_escape_pattern "$old" )"
    shift
    local new="$1"
    new="$( _sed_escape_substitution "$new" )"
    shift

    sed --binary --in-place -- "s/\\b$old\\b/$new/g" "$@"
)

str_replace() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 3 ]; then
        echo "usage: ${FUNCNAME[0]} STR SUB REP" >&2
        return 1
    fi

    local str="$1"
    local sub="$2"
    local rep="$3"

    sub="$( _bash_escape_pattern "$sub" )"

    echo "${str//$sub/$rep}"
)

str_tolower() (
    set -o errexit -o nounset -o pipefail

    local s
    for s; do
        echo "${s,,}"
    done
)

str_toupper() (
    set -o errexit -o nounset -o pipefail

    local s
    for s; do
        echo "${s^^}"
    done
)

_bash_escape_pattern() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} STR" >&2
        return 1
    fi

    # Only *?\[] need to be escaped according to:
    # http://wiki.bash-hackers.org/syntax/pattern#normal_pattern_language
    local s="$1"
    s="${s//'\'/'\\'}"
    s="${s//'*'/'\*'}"
    s="${s//'?'/'\?'}"
    s="${s//'['/'\['}"
    s="${s//']'/'\]'}"
    echo "$s"
)

str_contains() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 2 ]; then
        echo "usage: ${FUNCNAME[0]} STR SUB" >&2
        return 1
    fi

    local str="$1"
    local sub="$2"

    [ -z "$sub" ] && return 0
    sub="$( _bash_escape_pattern "$sub" )"

    test "$str" != "${str#*$sub}"
)

str_starts_with() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 2 ]; then
        echo "usage: ${FUNCNAME[0]} STR SUB" >&2
        return 1
    fi

    local str="$1"
    local sub="$2"

    [ -z "$sub" ] && return 0
    sub="$( _bash_escape_pattern "$sub" )"

    test "$str" != "${str#$sub}"
)

str_ends_with() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 2 ]; then
        echo "usage: ${FUNCNAME[0]} STR SUB" >&2
        return 1
    fi

    local str="$1"
    local sub="$2"

    [ -z "$sub" ] && return 0
    sub="$( _bash_escape_pattern "$sub" )"

    test "$str" != "${str%$sub}"
)

str_split() (
    set -o errexit -o nounset -o pipefail

    local fmt='%s\n'
    local -a args

    while [ "$#" -ne 0 ]; do
        local key="$1"
        shift
        case "$key" in
            -h|--help)
                echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero] [--] STR DELIM"
                return 0
                ;;
            -0|--null|-z|--zero)
                fmt='%s\0'
                ;;
            --)
                break
                ;;
            -*)
                echo "${FUNCNAME[0]}: unrecognized parameter: $key" >&2
                return 1
                ;;
            *)
                args+=("$key")
                ;;
        esac
    done

    args+=("$@")

    if [ "${#args[@]}" -ne 2 ]; then
        echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero] [--] STR DELIM" >&2
        return 1
    fi

    local str="${args[0]}"
    local delim="${args[1]}"

    if [ "${#delim}" -ne 1 ]; then
        echo "${FUNCNAME[0]}: delimiter must be exactly 1 character long" >&2
        return 1
    fi

    local -a xs=()

    # Thanks to this guy for this trick:
    # http://stackoverflow.com/a/24426608/514684
    IFS="$delim" read -a xs -d '' -r < <( printf -- "%s$delim\\0" "$str" )

    [ "${#xs[@]}" -gt 0 ] && printf -- "$fmt" ${xs[@]+"${xs[@]}"}
)

str_join() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -lt 1 ]; then
        echo "usage: ${FUNCNAME[0]} DELIM [STR]..." >&2
        return 1
    fi

    local delim="$1"
    shift

    [ "$#" -eq 0 ] && return 0

    local str="$1"
    shift

    echo -n "$str"
    for str; do
        echo -n "$delim$str"
    done
    echo
)

str_grep() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} PATTERN" >&2
        return 1
    fi

    local pattern="$1"

    find . -type f -exec grep --fixed-strings --binary-files=without-match --regexp="$pattern" -- {} +
)

str_grep_word() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} PATTERN" >&2
        return 1
    fi

    local pattern="$1"

    find . -type f -exec grep --basic-regexp --binary-files=without-match --regexp="\b$pattern\b" -- {} +
)

str_iconv_fromto() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -lt 3 ]; then
        echo "usage: ${FUNCNAME[0]} SRC_ENCODING DEST_ENCODING PATH..." >&2
        return 1
    fi

    local src_encoding="$1"
    shift
    local dest_encoding="$1"
    shift

    local path path_dir tmp_path rm_tmp_path

    for path; do
        path_dir="$( dirname -- "$path" )"
        tmp_path="$( mktemp -- "$path_dir/${FUNCNAME[0]}_XXX" )"
        if iconv --from-code="$src_encoding" --to-code="$dest_encoding" -- "$path" > "$tmp_path"; then
            mv -f -- "$tmp_path" "$path"
        else
            rm -f -- "$tmp_path"
            return 1
        fi
    done
)

str_iconv() (
    set -o errexit -o nounset -o pipefail

    if [ "$#" -lt 2 ]; then
        echo "usage: ${FUNCNAME[0]} DEST_ENCODING PATH..." >&2
        return 1
    fi

    local dest_encoding="$1"
    shift

    local src_encoding path
    for path; do
        src_encoding="$( file --brief --mime-encoding -- "$path" )"
        str_iconv_fromto "$src_encoding" "$dest_encoding" "$path"
    done
)