From 1bc7101cf6c3e574e5cdb2fb1f08fdf21cf52f60 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sun, 18 Jul 2021 11:08:49 +0200 Subject: [PATCH] prevent "^((.*))$" in regex: easier to read, small perf improvement. --- day19/ex1.bash | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/day19/ex1.bash b/day19/ex1.bash index 2bc5c26..b0a15b2 100755 --- a/day19/ex1.bash +++ b/day19/ex1.bash @@ -21,7 +21,7 @@ function buildtree { local name="var_$prof" # local var name, w/ depth eval "local $name" # ... declared here shift 2 - local args=$* res="" arg tmp + local args=$* res="" arg t for arg in $args; do if [[ -z "${arg/[|ab]}" ]]; then @@ -29,9 +29,12 @@ function buildtree { else if [[ ! -v MATCH[$arg] ]]; then buildtree "$((prof+1))" "$name" "${RULE[$arg]}" - tmp="${!name}" - [[ ! "$tmp" =~ ^.$ ]] && tmp="($tmp)" - MATCH[$arg]="$tmp" + t="${!name}" + #[[ ! "$t" =~ ^.$ ]] && t="($t)" + if [[ ! "$t" =~ ^[ab]+$ ]] && [[ "${t:0:1}${t: -1}" != "()" ]]; then + t="($t)" + fi + MATCH[$arg]="$t" fi res+=${MATCH[$arg]} fi @@ -54,7 +57,7 @@ while read -r line; do done buildtree 1 var_0 0 -#printf "RULE ZERO = %s\n" "${MATCH[0]}" +printf "RULE ZERO = %s\n" "${MATCH[0]}" for str in "${STRING[@]}"; do [[ "$str" =~ ^${MATCH[0]}$ ]] && ((res++))