base.core: qwc supporte les classes de caractères

This commit is contained in:
Jephté Clain 2017-04-20 23:19:55 +04:00
parent 78e0a8e99c
commit 540e718180
1 changed files with 36 additions and 35 deletions

View File

@ -107,7 +107,7 @@ function qvals() {
}
function qwc() {
# Dans la chaine $*, remplacer \ par \\, " par \", $ par \$, ` par \`, puis
# quoter la chaine avec ", sauf les wildcards * et ?
# quoter la chaine avec ", sauf les wildcards *, ? et [class]
# Cela permet de quoter une chaine permettant de glober des fichiers, e.g
# eval "ls $(qwc "$value")"
# Note: la protection de ! n'est pas effectuée, parce que le comportement du
@ -119,44 +119,45 @@ function qwc() {
s="${s//\"/\\\"}"
s="${s//\$/\\\$}"
s="${s//\`/\\\`}"
local r a b
local r a b c
while [ -n "$s" ]; do
if [[ "$s" == *\** ]]; then
if [[ "$s" == *\?* ]]; then
a="${s%%\**}"
b="${s%%\?*}"
if [ ${#a} -lt ${#b} ]; then
s="${s#*\*}"
r="$r\"$a\"*"
else
s="${s#*\?}"
r="$r\"$b\"?"
fi
else
a="${s%%\**}"
s="${s#*\*}"
r="$r\"$a\"*"
fi
elif [[ "$s" == *\?* ]]; then
if [[ "$s" == *\** ]]; then
a="${s%%\**}"
b="${s%%\?*}"
if [ ${#a} -lt ${#b} ]; then
s="${s#*\*}"
r="$r\"$a\"*"
else
s="${s#*\?}"
r="$r\"$b\"?"
fi
else
a="${s%%\?*}"
s="${s#*\?}"
r="$r\"$a\"?"
fi
else
a=; b=; c=
a=; [[ "$s" == *\** ]] && { a="${s%%\**}"; a=${#a}; }
b=; [[ "$s" == *\?* ]] && { b="${s%%\?*}"; b=${#b}; }
c=; [[ "$s" == *\[* ]] && { c="${s%%\[*}"; c=${#c}; }
if [ -z "$a" -a -z "$b" -a -z "$c" ]; then
r="$r\"$s\""
break
fi
if [ -n "$a" ]; then
[ -n "$b" ] && [ $a -lt $b ] && b=
[ -n "$c" ] && [ $a -lt $c ] && c=
fi
if [ -n "$b" ]; then
[ -n "$a" ] && [ $b -lt $a ] && a=
[ -n "$c" ] && [ $b -lt $c ] && c=
fi
if [ -n "$c" ]; then
[ -n "$a" ] && [ $c -lt $a ] && a=
[ -n "$b" ] && [ $c -lt $b ] && b=
fi
if [ -n "$a" ]; then # PREFIX*
a="${s%%\**}"
s="${s#*\*}"
[ -n "$a" ] && r="$r\"$a\""
r="$r*"
elif [ -n "$b" ]; then # PREFIX?
a="${s%%\?*}"
s="${s#*\?}"
[ -n "$a" ] && r="$r\"$a\""
r="$r?"
elif [ -n "$c" ]; then # PREFIX[class]
a="${s%%\[*}"
b="${s#*\[}"; b="${b%%\]*}"
s="${s:$((${#a} + ${#b} + 2))}"
[ -n "$a" ] && r="$r\"$a\""
r="$r[$b]"
fi
done
recho_ "$r"
}