From d3093d4d72283019226ab0f8f76de520a0509e2a Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 14 Feb 2022 00:35:47 +0000 Subject: made parseconf insanely fast --- src/parseconf.sh | 41 ++++++++++++----------------------------- test/parseconf.sh | 2 +- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/parseconf.sh b/src/parseconf.sh index aa5885d..f92bf0e 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -28,7 +28,7 @@ parse_line() { shift local value="$@" - [ "$key" = "include" ] && parse $value && return + [ "$key" = "include" ] && cat $value | parse && return [ "$key" = "]" ] && unset list[-1] && printf "\n" && return [ "$key" = "}" ] && unset level[-1] && return @@ -53,30 +53,13 @@ parse_line() { # parse () { local file="$1" + export level=() export list=() while IFS= read -r line; do # strip whitespace - line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line") - parse_line $line - done < "$file" -} - -# Filter the parsed file for specific keys -# -filter () { - local pattern= - - [ $# = 0 ] && - pattern=".*" || - pattern=$(sed "s/\*/[^:]*/g"<<< "$@") - - $print_keys && - pattern="s/^($pattern:.+)/\1/p" || - pattern="s/^$pattern:(.+)/\1/p" - - - parse $CONF_FILE | sed -rn $pattern + parse_line $line + done < "/dev/stdin" } # Use the env variable if exists @@ -98,7 +81,7 @@ while getopts ":f:c:v" opt; do print_keys=false ;; c) - count="${OPTARG}" + count="${OPTARG}q" ;; *) esac @@ -106,12 +89,12 @@ done shift $((OPTIND-1)) -if echo $@ | grep -q "."; then - [ -z ${count} ] && - filter "$@" || - filter "$@" | tail -n $count -else - parse $CONF_FILE -fi +[ $# = 0 ] && + pattern=".*" || + pattern=$(sed "s/\*/[^:]*/g" <<< "$@") +$print_keys && + pattern="s/^($pattern:.+)/\1/p;${count}" || + pattern="s/^$pattern:(.+)/\1/p;${count}" +sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | parse $@ | sed -rn $pattern diff --git a/test/parseconf.sh b/test/parseconf.sh index 3af55e1..69dd78c 100755 --- a/test/parseconf.sh +++ b/test/parseconf.sh @@ -116,6 +116,6 @@ dict { } " retval=$(${PARSECONF} -c 1 "dict.*" <<< "$config") - [ "$retval" = "dict.d:4" ] + [ "$retval" = "dict.a:1" ] } -- cgit v1.2.1