refactor: hemar reboot
This commit is contained in:
130
package/hemar/hemar.sh
Normal file
130
package/hemar/hemar.sh
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/bin/dash
|
||||
|
||||
# 0 - text
|
||||
# 1 - deside tag type
|
||||
# 2 - interpolation
|
||||
# 3 - section
|
||||
# 4 - include
|
||||
# 5 - compute
|
||||
STAGE=0
|
||||
STAGE_BUFFER="$(mktemp)"
|
||||
open_tag_flag=0
|
||||
|
||||
# data structure :)
|
||||
|
||||
# Type = 0..=5
|
||||
#
|
||||
# Text = string # just a text body
|
||||
#
|
||||
# Interpolation = string # path to variable
|
||||
#
|
||||
# Include = string # path to include data
|
||||
#
|
||||
# Section = {
|
||||
# v = string # item variable name for loop
|
||||
# p = string # path to array for iteration
|
||||
# b = [Element] # section body
|
||||
#
|
||||
# }
|
||||
#
|
||||
# End = null
|
||||
#
|
||||
# Compute = {
|
||||
# l = string # programing language
|
||||
# b = string # function body
|
||||
# }
|
||||
#
|
||||
# Element = {
|
||||
# t = Type # element type
|
||||
# b = Text # element body
|
||||
# | Interpolation
|
||||
# | Section
|
||||
# | End
|
||||
# | Include
|
||||
# | Compute
|
||||
# }
|
||||
#
|
||||
# Hemar = {
|
||||
# e = [Element] # elements array
|
||||
#}
|
||||
# finds close pattern and store the char to the STAGE_BUFFER
|
||||
find_close_pattern() {
|
||||
char="${1:?}"
|
||||
if [ "${close_tag_flag:?}" -eq 0 ] && [ "$char" = ']' ]; then
|
||||
close_tag_flag=1
|
||||
elif [ "${close_tag_flag:?}" -eq 1 ] && [ "$char" = '}' ]; then
|
||||
close_tag_flag=0
|
||||
|
||||
# removes first and last white spaces from the buffer
|
||||
sed -i 's/[[:space:]]$//g' "$STAGE_BUFFER"
|
||||
sed -i 's/^[[:space:]]//g' "$STAGE_BUFFER"
|
||||
|
||||
# removes last char from buffer (]) is part of close pattern
|
||||
truncate -s -1 "$STAGE_BUFFER"
|
||||
return 0
|
||||
else
|
||||
printf '%s' "$char" >> "$STAGE_BUFFER"
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# finds open pattern and stores the char to the STAGE_BUFFER
|
||||
find_open_pattern() {
|
||||
char="${1:?}"
|
||||
if [ "${open_tag_flag:?}" -eq 0 ] && [ "$char" = '{' ]; then
|
||||
open_tag_flag=1
|
||||
elif [ "${open_tag_flag:?}" -eq 1 ] && [ "$char" = '[' ]; then
|
||||
open_tag_flag=0
|
||||
|
||||
# removes last char from buffer ({) is part of open pattern
|
||||
truncate -s -1 "$STAGE_BUFFER"
|
||||
return 0
|
||||
else
|
||||
printf '%s' "$char" >> "$STAGE_BUFFER"
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
parse() {
|
||||
char="$1"
|
||||
|
||||
case "$STAGE" in
|
||||
0)
|
||||
if find_open_pattern "$char"; then
|
||||
plex_set "$data_pointer"''
|
||||
STAGE=1
|
||||
fi
|
||||
;;
|
||||
1)
|
||||
if find_close_pattern "$char"; then
|
||||
STAGE=0
|
||||
fi
|
||||
;;
|
||||
2)
|
||||
|
||||
;;
|
||||
3)
|
||||
|
||||
;;
|
||||
4)
|
||||
|
||||
;;
|
||||
*)
|
||||
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Using dd to read one character at a time
|
||||
input=$(cat)
|
||||
i=1
|
||||
while :; do
|
||||
char=$(printf '%s' "$input" | dd bs=1 skip=$((i-1)) count=1 2>/dev/null)
|
||||
[ -z "$char" ] && break
|
||||
|
||||
parse "$char"
|
||||
|
||||
i=$((i+1))
|
||||
done
|
||||
77
package/hemar/plex.sh
Normal file
77
package/hemar/plex.sh
Normal file
@@ -0,0 +1,77 @@
|
||||
#!/bin/dash
|
||||
|
||||
#data_size="$(eval printf '%s' "\$$structname" | wc -c)"
|
||||
#if [ "$data_size" -ge "$(getconf ARG_MAX)" ]; then
|
||||
# # TODO: handle large text
|
||||
# echo "Data too large for an environment variable"
|
||||
# exit 1
|
||||
#fi
|
||||
|
||||
plex_set() {
|
||||
local structname key val regex base esc_key regex esc temp
|
||||
structname=$1 key=$2 val=$3
|
||||
|
||||
# construct regex for ancestors
|
||||
regex="^$key="
|
||||
|
||||
base=$key
|
||||
while expr "$base" : '.*\.' >/dev/null; do
|
||||
base=$(printf '%s\n' "$base" | sed 's/\.[^.]*$//')
|
||||
esc=$(printf '%s\n' "$base" | sed 's/\./\\./g')
|
||||
regex="$regex|^$esc="
|
||||
done
|
||||
|
||||
# add descendants
|
||||
esc_key="$(printf '%s\n' "$key" | sed 's/\./\\./g')"
|
||||
regex="$regex|^${esc_key}\."
|
||||
|
||||
# remove old
|
||||
# <plex>=$(printf '%s\n' "$<plex>" | grep -v -E "$regex")
|
||||
temp="$(eval "printf '%s\\n' \"\$$structname\"" | grep -v -E "$regex")"
|
||||
eval "$structname=\"\$temp\""
|
||||
|
||||
# add new
|
||||
eval "$structname=\$(printf '%s\\n%s=%s\\n' \"\$$structname\" \"\$key\" \"\$val\")"
|
||||
}
|
||||
|
||||
plex_child() {
|
||||
local structname prefix
|
||||
structname=$1 prefix=$2
|
||||
|
||||
eval printf '%s\\n' \"\$"$structname"\" \
|
||||
| grep "^$prefix\." \
|
||||
| sed "s|^$prefix\.||"
|
||||
}
|
||||
|
||||
plex_val() {
|
||||
local structname key
|
||||
structname=$1 key=$2
|
||||
eval printf '%s\n' \"\$"$structname"\" | grep "^$key=" | cut -d= -f2-
|
||||
}
|
||||
|
||||
plex_fetch() {
|
||||
local structname key
|
||||
structname=$1 key=$2
|
||||
if eval printf '%s\\n' \"\$"$structname"\" | grep -q "^$key="; then
|
||||
eval printf '%s\\n' \"\$"$structname"\" | grep "^$key=" | cut -d= -f2-
|
||||
else
|
||||
eval printf '%s\\n' \"\$"$structname"\" | grep "^$key\." | sed "s|^$key\.||"
|
||||
fi
|
||||
}
|
||||
|
||||
plex_push() {
|
||||
local structname prefix val max idx newidx kv
|
||||
structname=${1:?}
|
||||
prefix=${2:?}
|
||||
val=${3:?}
|
||||
|
||||
# find max index
|
||||
max=0
|
||||
for kv in $(plex_fetch "$structname" "$prefix"); do
|
||||
idx=${kv%%=*}
|
||||
[ "$idx" -gt "$max" ] 2>/dev/null && max=$idx
|
||||
done
|
||||
|
||||
newidx=$((max + 1))
|
||||
plex_set "$structname" "$prefix.$newidx" "$val"
|
||||
}
|
||||
6
package/hemar/test.sh
Normal file
6
package/hemar/test.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/bin/dash
|
||||
|
||||
WORKSPACE="$(cd "$(dirname "$0")" && pwd)"
|
||||
|
||||
#. "${WORKSPACE:?}/test/plex.sh"
|
||||
. "${WORKSPACE:?}/test/time.sh"
|
||||
111
package/hemar/test/plex/time.sh
Normal file
111
package/hemar/test/plex/time.sh
Normal file
@@ -0,0 +1,111 @@
|
||||
#!/bin/dash
|
||||
|
||||
. "${WORKSPACE:?}/plex.sh"
|
||||
|
||||
MY_STRUCT=''
|
||||
|
||||
math() {
|
||||
awk "BEGIN {print $1}"
|
||||
}
|
||||
|
||||
elapsed() {
|
||||
local task time count decrease avg
|
||||
task=$1
|
||||
time=$2
|
||||
count=$3
|
||||
decrease=${4:-0}
|
||||
avg=$(math "$time/$count-$decrease")
|
||||
|
||||
printf '\n[%s]\nelapsed %s seconds\n%s per second\n' \
|
||||
"$task" "$avg" "$(math "1/$avg")" >&2
|
||||
printf '%s' "$avg"
|
||||
}
|
||||
|
||||
set_word_length() {
|
||||
local length
|
||||
length=${1:?}
|
||||
|
||||
# shellcheck disable=SC2183
|
||||
__WORD_OFFSET_PATERN="$(printf '%*s' "$length" | tr ' ' '?')"
|
||||
}
|
||||
|
||||
UNIQ_8_WORDS_COUNT=1000
|
||||
DEFAULT_WORD_LENGTH=8
|
||||
set_word_length "$DEFAULT_WORD_LENGTH"
|
||||
|
||||
randomword() {
|
||||
local length
|
||||
length=${1:-$DEFAULT_WORD_LENGTH}
|
||||
LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$length"
|
||||
}
|
||||
|
||||
WORDS=$(randomword $((8 * UNIQ_8_WORDS_COUNT)))
|
||||
|
||||
new_word() {
|
||||
local prefix
|
||||
prefix=${WORDS%"${WORDS#"$__WORD_OFFSET_PATERN"}"}
|
||||
WORDS=${WORDS#"$__WORD_OFFSET_PATERN"}$prefix
|
||||
printf '%s' "$prefix"
|
||||
}
|
||||
|
||||
bench_set() {
|
||||
local task depth count wordtime i start key d end
|
||||
task=$1
|
||||
depth=$2
|
||||
count=$3
|
||||
wordtime=$4
|
||||
i=0
|
||||
start=$(date +%s)
|
||||
while [ "$i" -lt "$count" ]; do
|
||||
key=$(new_word)
|
||||
if [ "$depth" -gt 1 ]; then
|
||||
d=1
|
||||
while [ "$d" -lt "$depth" ]; do
|
||||
key="$key.$(new_word)"
|
||||
d=$((d + 1))
|
||||
done
|
||||
fi
|
||||
plex_set 'MY_STRUCT' "$key" "$i"
|
||||
i=$((i + 1))
|
||||
done
|
||||
end=$(date +%s)
|
||||
elapsed "$task" "$((end - start))" "$count" "$(math "$wordtime*$depth")" >/dev/null
|
||||
}
|
||||
|
||||
DEFAULT_TRIES=1000
|
||||
ACCURATE_TRIES=10000
|
||||
SUPPER_ACCURATE_TRIES=100000
|
||||
|
||||
WORD_CREATE_ACCURACY="$ACCURATE_TRIES"
|
||||
BENCH_ACCURACY="$DEFAULT_TRIES"
|
||||
|
||||
count="$WORD_CREATE_ACCURACY"
|
||||
set_word_length 8
|
||||
i=0
|
||||
start=$(date +%s)
|
||||
while [ "$i" -lt "$count" ]; do
|
||||
new_word >/dev/null
|
||||
i=$((i + 1))
|
||||
done
|
||||
end=$(date +%s)
|
||||
wordtime=$(elapsed 'Word creation' "$((end - start))" "$count")
|
||||
|
||||
bench_set 'Set element with depth 1 length 8' 1 "$BENCH_ACCURACY" "$wordtime"
|
||||
bench_set 'Set element with depth 2 length 8' 2 "$BENCH_ACCURACY" "$wordtime"
|
||||
bench_set 'Set element with depth 3 length 8' 3 "$BENCH_ACCURACY" "$wordtime"
|
||||
|
||||
count="$W"
|
||||
set_word_length 2
|
||||
i=0
|
||||
start=$(date +%s)
|
||||
while [ "$i" -lt "$count" ]; do
|
||||
new_word >/dev/null
|
||||
i=$((i + 1))
|
||||
done
|
||||
end=$(date +%s)
|
||||
wordtime=$(elapsed 'Word creation' "$((end - start))" "$count")
|
||||
|
||||
bench_set 'Set element with depth 1 length 2' 1 "$BENCH_ACCURACY" "$wordtime"
|
||||
bench_set 'Set element with depth 2 length 2' 2 "$BENCH_ACCURACY" "$wordtime"
|
||||
bench_set 'Set element with depth 3 length 2' 3 "$BENCH_ACCURACY" "$wordtime"
|
||||
|
||||
Reference in New Issue
Block a user