From e4d7526f318d2a925f8e69ed5a630e5c7d6f527c Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 8 Jan 2017 16:42:56 +0300 Subject: add str_ends_with --- %HOME%/.bash_utils/text.sh | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to '%HOME%/.bash_utils') 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 -- cgit v1.2.3