From f731cdb9a91f46a10d9d0c1712217e76e64519ea Mon Sep 17 00:00:00 2001 From: DeveloperC Date: Fri, 26 Jan 2024 21:19:18 +0000 Subject: [PATCH] perf: improving regex matching performance Using native shell instead of grep improves the performance drastically. Using grep takes roughly x14 times longer on my machine to parse roughly 100 abbreviations. * https://github.com/DeveloperC286/zsh-simple-abbreviations/issues/3 --- src/zsh-simple-abbreviations | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/zsh-simple-abbreviations b/src/zsh-simple-abbreviations index 8d35de1..cba632e 100644 --- a/src/zsh-simple-abbreviations +++ b/src/zsh-simple-abbreviations @@ -1,7 +1,7 @@ #!/usr/bin/env zsh VERSION='0.3.0' -KEY_REGEX="-_a-zA-Z0-9" +KEY_REGEX="^[[:alnum:]]+$" __zsh_simple_abbreviations::expand() { emulate -L zsh -o extended_glob @@ -34,10 +34,10 @@ case $1 in local key=${2} local value=${3} - if [[ $(echo "${key}" | grep -E "^[${KEY_REGEX}]*$") ]]; then + if [[ "${key}" =~ ${KEY_REGEX} ]]; then _zsh_simple_abbreviations[$key]="${value}" else - echo "zsh_simple_abbreviations add sub-command key does not match the regex '^[${KEY_REGEX}]*$'." + echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters." return 1 fi ;; @@ -50,7 +50,7 @@ case $1 in local key=${2} - if [[ $(echo "${key}" | grep -E "^[${KEY_REGEX}]*$") ]]; then + if [[ "${key}" =~ ${KEY_REGEX} ]]; then if [[ -n "${_zsh_simple_abbreviations[$key]}" ]]; then unset "_zsh_simple_abbreviations[$key]" else @@ -58,7 +58,7 @@ case $1 in return 1 fi else - echo "zsh_simple_abbreviations remove sub-command key does not match the regex '^[${KEY_REGEX}]*$'." + echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters." return 1 fi ;;