Skip to content

Commit

Permalink
perf: improving regex matching performance
Browse files Browse the repository at this point in the history
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.

* #3
  • Loading branch information
DeveloperC286 committed Jan 26, 2024
1 parent 513d71d commit f731cdb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/zsh-simple-abbreviations
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
;;
Expand All @@ -50,15 +50,15 @@ 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
echo "zsh_simple_abbreviations remove sub-command key does not match any abbreviations."
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
;;
Expand Down

0 comments on commit f731cdb

Please sign in to comment.