1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 02:49:02 -08:00

rewrite escaping for speed and portability in notice

This commit is contained in:
Connor Olding 2024-07-23 10:34:41 -07:00
parent 9e659006f6
commit bd7f91c7a3

View file

@ -27,18 +27,21 @@ __notice_commaize() { # fun with POSIX shell
REPLY="${2%,}"
}
__notice_escape() {
# doesn't work in posh. it doesn't like "${1%%[$2]*}", bug?
# warning: mksh is O(n^2) with string length!
# note: bash is horribly slow without `export LC_ALL=C`. zsh is fastest.
set -- "" "" "$1" "$2"
while :; do
set -- "$1" "${3%%[$4]*}" "$3" "$4"
[ "$2" != "$3" ] || break
set -- "$1$2\\" "${3#"$2"}" "$3" "$4"
set -- "$1${2%"${2#?}"}" "$2" "${2#?}" "$4"
__notice_escape_print() {
# much faster and more portable than the old version.
# unfortunately, this requires a subshell to capture.
a="$1"
until [ -z "$a" ]; do
case "$a" in
([$2]*) printf %s%.1s \\ "$a";;
(*) printf %.1s "$a";;
esac
a="${a#?}"
done
REPLY="$1$3"
}
__notice_escape() {
REPLY="$(__notice_escape_print "$@")"
}
__notice_urlencode() {