From bd7f91c7a3a8a15c9b84f3839c74b50a3a5a7679 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 23 Jul 2024 10:34:41 -0700 Subject: [PATCH] rewrite escaping for speed and portability in `notice` --- sh/notice | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/sh/notice b/sh/notice index 51a3732..ea78c51 100755 --- a/sh/notice +++ b/sh/notice @@ -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() {