diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-01-08 16:42:56 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-01-08 16:42:56 +0300 |
commit | e4d7526f318d2a925f8e69ed5a630e5c7d6f527c (patch) | |
tree | 820b96edf12491dbfd04773274686ce4451c97e8 /%HOME%/.bash_utils | |
parent | free: --human (diff) | |
download | linux-home-e4d7526f318d2a925f8e69ed5a630e5c7d6f527c.tar.gz linux-home-e4d7526f318d2a925f8e69ed5a630e5c7d6f527c.zip |
add str_ends_with
Diffstat (limited to '')
-rw-r--r-- | %HOME%/.bash_utils/text.sh | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh index 144bc2c..22c20e3 100644 --- a/%HOME%/.bash_utils/text.sh +++ b/%HOME%/.bash_utils/text.sh @@ -85,9 +85,11 @@ str_contains() ( fi local str="$1" - local sub - sub="$( printf -- '%q' "$2" )" + local sub="$2" + + [ -z "$sub" ] && return 0 + sub="$( printf -- '%q' "$2" )" test "$str" != "${str#*$sub}" ) @@ -100,12 +102,31 @@ str_starts_with() ( fi local str="$1" - local sub - sub="$( printf -- '%q' "$2" )" + local sub="$2" + + [ -z "$sub" ] && return 0 + sub="$( printf -- '%q' "$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="$( printf -- '%q' "$sub" )" + test "$str" != "${str%$sub}" +) + str_split() ( set -o errexit -o nounset -o pipefail |