aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-04-16 06:53:12 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-04-16 06:53:12 +0300
commitd08033c0293942e8ce19583c73ecfc10e6ef376d (patch)
treee5b8c98c82e3e671b75b95b3677a948511203c54 /%HOME%/.bash_utils
parentbugfix & code style (diff)
downloadlinux-home-d08033c0293942e8ce19583c73ecfc10e6ef376d.tar.gz
linux-home-d08033c0293942e8ce19583c73ecfc10e6ef376d.zip
str_split: bugfix
Diffstat (limited to '%HOME%/.bash_utils')
-rw-r--r--%HOME%/.bash_utils/text.sh18
1 files changed, 11 insertions, 7 deletions
diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh
index be197e1..238eb5e 100644
--- a/%HOME%/.bash_utils/text.sh
+++ b/%HOME%/.bash_utils/text.sh
@@ -227,16 +227,20 @@ str_split() (
fi
local str="${args[0]}"
- local old_delim="${args[1]}"
+ local delim="${args[1]}"
- local -a xs
- local x
+ if [ "${#delim}" -ne 1 ]; then
+ echo "${FUNCNAME[0]}: delimiter must be exactly 1 character long" >&2
+ return 1
+ fi
- IFS="$old_delim" read -a xs -r <<< "$str"
+ local -a xs=()
- for x in ${xs[@]+"${xs[@]}"}; do
- printf -- "$fmt" "$x"
- done
+ # Thanks to this guy for this trick:
+ # http://stackoverflow.com/a/24426608/514684
+ IFS="$delim" read -a xs -d '' -r < <( printf -- "%s$delim\\0" "$str" )
+
+ [ "${#xs[@]}" -gt 0 ] && printf -- "$fmt" ${xs[@]+"${xs[@]}"}
)
str_join() (