diff options
Diffstat (limited to '')
-rw-r--r-- | %HOME%/.bash_utils/text.sh | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh index 2228a8b..242837f 100644 --- a/%HOME%/.bash_utils/text.sh +++ b/%HOME%/.bash_utils/text.sh @@ -5,18 +5,18 @@ # For details, see https://github.com/egor-tensin/cygwin-home. # Distributed under the MIT License. -alias dos2eol='sed --binary --in-place '"'"'s/\(\r\?\)$//'"'" -alias eol2dos='sed --binary --in-place '"'"'s/\r\?$/\r/'"'" +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 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 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 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//'"'" +alias trimbom='sed --binary --in-place -- '"'"'1 s/^\xef\xbb\xbf//'"'" lint() { trim "$@" && trimeol "$@" && eol "$@" @@ -39,7 +39,7 @@ replace_word() ( local new="$1" shift - sed --binary --in-place "s/\\b$old\\b/$new/g" "$@" + sed --binary --in-place -- "s/\\b$old\\b/$new/g" "$@" ) str_tolower() ( @@ -70,7 +70,7 @@ str_contains() ( local str="$1" local sub - sub="$( printf '%q' "$2" )" + sub="$( printf -- '%q' "$2" )" test "$str" != "${str#*$sub}" ) @@ -85,7 +85,7 @@ str_starts_with() ( local str="$1" local sub - sub="$( printf '%q' "$2" )" + sub="$( printf -- '%q' "$2" )" test "$str" != "${str#$sub}" ) @@ -93,34 +93,29 @@ str_starts_with() ( str_split() ( set -o errexit -o nounset -o pipefail - local zero= + local fmt='%s\n' local -a args while [ "$#" -ne 0 ]; do - case "$1" in - -0|-z) - zero=1 - shift - ;; - + local key="$1" + shift + case "$key" in -h|--help) - echo "usage: ${FUNCNAME[0]} [-z|-0] [-h|--help] [--] STR DELIM" + echo "usage: ${FUNCNAME[0]} [-h|--help] [-z|-0] [--] STR DELIM" return 0 ;; - + -0|-z) + fmt='%s\0' + ;; --) - shift break ;; - -*) - echo "${FUNCNAME[0]}: unrecognized parameter: $1" >&2 + echo "${FUNCNAME[0]}: unrecognized parameter: $key" >&2 return 1 ;; - *) - args+=("$1") - shift + args+=("$key") ;; esac done @@ -128,7 +123,7 @@ str_split() ( args+=("$@") if [ "${#args[@]}" -ne 2 ]; then - echo "usage: ${FUNCNAME[0]} [-z|-0] [-h|--help] [--] STR DELIM" + echo "usage: ${FUNCNAME[0]} [-h|--help] [-z|-0] [--] STR DELIM" return 1 fi @@ -138,14 +133,10 @@ str_split() ( local -a xs local x - IFS="$old_delim" read -ra xs <<< "$str" + IFS="$old_delim" read -a xs -r <<< "$str" for x in ${xs[@]+"${xs[@]}"}; do - if [ -z "$zero" ]; then - printf '%s\n' "$x" - else - printf '%s\0' "$x" - fi + printf "$fmt" "$x" done ) @@ -169,10 +160,10 @@ str_join() ( *) local s="$1" shift - printf '%s' "$s" + printf -- '%s' "$s" for s; do - printf '%s%s' "$delim" "$s" + printf -- '%s%s' "$delim" "$s" done ;; esac |