diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-04-15 08:49:25 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-04-15 08:49:25 +0300 |
commit | ae9cf856455d3bd2bed0abcae621b0f75d222256 (patch) | |
tree | 44f1b1b360c88932e6624ec5c526496674fc7c20 /%HOME% | |
parent | str_split: interface update (diff) | |
download | linux-home-ae9cf856455d3bd2bed0abcae621b0f75d222256.tar.gz linux-home-ae9cf856455d3bd2bed0abcae621b0f75d222256.zip |
str_contains, str_*_with: proper escaping
Diffstat (limited to '%HOME%')
-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}" ) |