diff options
Diffstat (limited to '%HOME%/.bash_utils')
-rw-r--r-- | %HOME%/.bash_utils/text.sh | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh index e27d392..f74fb23 100644 --- a/%HOME%/.bash_utils/text.sh +++ b/%HOME%/.bash_utils/text.sh @@ -76,6 +76,20 @@ str_toupper() ( done ) +escape_pattern() { + set -o errexit -o nounset -o pipefail + + local pattern + for pattern; do + pattern="${pattern//'\'/'\\'}" + pattern="${pattern//'*'/'\*'}" + pattern="${pattern//'?'/'\?'}" + pattern="${pattern//'['/'\['}" + pattern="${pattern//']'/'\]'}" + echo "$pattern" + done +} + str_contains() ( set -o errexit -o nounset -o pipefail @@ -88,8 +102,8 @@ str_contains() ( local sub="$2" [ -z "$sub" ] && return 0 + sub="$( escape_pattern "$sub" )" - sub="$( printf -- '%q' "$2" )" test "$str" != "${str#*$sub}" ) @@ -105,8 +119,8 @@ str_starts_with() ( local sub="$2" [ -z "$sub" ] && return 0 + sub="$( escape_pattern "$sub" )" - sub="$( printf -- '%q' "$sub" )" test "$str" != "${str#$sub}" ) @@ -122,8 +136,8 @@ str_ends_with() ( local sub="$2" [ -z "$sub" ] && return 0 + sub="$( escape_pattern "$sub" )" - sub="$( printf -- '%q' "$sub" )" test "$str" != "${str%$sub}" ) |