Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 1b89ab2

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Improve performance of basic conv module
1 parent c1ef2fc commit 1b89ab2

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

lib/psst/basic/conv.inc.sh

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,46 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
2828
# charCode: Character code of desired character.
2929
#
3030
# RETURNS
31-
# 0: Character code was valid.
32-
# 1: Character code was no integer number.
33-
# 2: Character code was out of valid range (0-255).
34-
# 3: Character code is newline (`\n``), which cannot be captured.
31+
# 0: Success.
32+
# 2: Character code was no integer number.
33+
# 3: Character code was out of valid range (0-255).
34+
# 4: One or more trailing newline characters (\n) have been dropped.
3535
#
3636
# OUTPUT
3737
# stdout: The character whose code is `charCode`.
3838
#
39+
# NOTE
40+
# To prevent that trailing newlines are getting dropped, just append an extra
41+
# charcode and later on strip that char again as shown in the sample code.
42+
#
3943
# SAMPLE
4044
# digit9=$( chr_psst 57 )
4145
#
46+
# # Prevent newlines from getting dropped
47+
# newline=$( chr_psst 10 95 )
48+
# newline=${newline%_} # strip "_" at end as 95 == "_"
49+
#
4250
conv_chr_psst()
4351
(
4452
func="chr_psst"
4553
assert_minargc_psst "$func" 1 $#
4654
assert_hasarg_psst "$func" "charCode" "$1"
4755

48-
while [ -n "${1-}" ]
56+
for char in "$@"
4957
do
50-
charCode=$1
51-
test_is_int_psst "$charCode" || return 1
52-
{ [ "$charCode" -lt 0 ] || [ "$charCode" -gt 255 ]; } && return 2
53-
[ "$charCode" != "10" ] || return 3
54-
55-
printf "%b" "$( printf '\%o' "$charCode" )"
56-
shift
58+
test_is_int_psst "$char" || return 2;
59+
{ [ "$char" -lt 0 ] || [ "$char" -gt 255 ]; } && return 3
5760
done
61+
62+
if [ $# -eq 1 ]
63+
then
64+
printf "%b" "$( printf '\\0%o' "$char" )"
65+
else
66+
printf "%s" "$*" | xargs printf '\\\\0%o' | xargs printf "%b"
67+
fi
68+
69+
[ "$char" -eq 10 ] && return 4
70+
return 0
5871
)
5972

6073

@@ -87,16 +100,7 @@ conv_ord_psst()
87100
chars=$1
88101
[ ${#chars} -eq 1 ] && { LC_CTYPE=C printf "%d" "'$chars"; return 0; }
89102

90-
printf "%s" "$chars" | (
91-
sep=
92-
while read -r _unused_ octalValue <<EOF
93-
$( dd bs=1 count=1 2>/dev/null | od -b )
94-
EOF
95-
do
96-
[ -n "$octalValue" ] || return 0
97-
LC_CTYPE=C printf "%s%d" "$sep" "0$octalValue"
98-
sep=" "
99-
done
100-
)
101-
103+
printf "%s" "$chars" \
104+
| od -t u1 \
105+
| awk '{for(i=2;i<=NF;i++){printf "%s%s",sep,$i;sep=" "}}'
102106
)

test/basic/bin/test_conv.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ set -e
3232

3333
# Argument must be integer number
3434
conv_chr_psst "abc" >/dev/null || {
35-
[ $? = 1 ] || test_fail_psst $LINENO
35+
[ $? = 2 ] || test_fail_psst $LINENO
3636
}
3737

3838
# Argument must be in valid range
3939
conv_chr_psst "350" >/dev/null || {
40-
[ $? = 2 ] || test_fail_psst $LINENO
40+
[ $? = 3 ] || test_fail_psst $LINENO
4141
}
4242

4343

@@ -48,12 +48,12 @@ conv_chr_psst "350" >/dev/null || {
4848
[ "$( conv_chr_psst 65 97 32 )" = "Aa " ] || test_fail_psst $LINENO
4949

5050

51-
[ "$( conv_chr_psst 10 )" = "" ] || test_fail_psst $LINENO
52-
if conv_chr_psst "10" >/dev/null
51+
[ "$( conv_chr_psst 65 10 )" = "A" ] || test_fail_psst $LINENO
52+
if conv_chr_psst 65 10 >/dev/null
5353
then
5454
test_fail_psst $LINENO
5555
else
56-
[ $? = 3 ] || test_fail_psst $LINENO
56+
[ $? = 4 ] || test_fail_psst $LINENO
5757
fi
5858

5959

@@ -81,4 +81,12 @@ set -e
8181
[ "$( conv_ord_psst " " )" = 32 ] || test_fail_psst $LINENO
8282
[ "$( conv_ord_psst "$NL_CHAR_PSST" )" = 10 ] || test_fail_psst $LINENO
8383
[ "$( conv_ord_psst "Aa " )" = "65 97 32" ] || test_fail_psst $LINENO
84-
[ "$( conv_ord_psst "A$NL_CHAR_PSST" )" = "65 10" ] || test_fail_psst $LINENO
84+
[ "$( conv_ord_psst "A$NL_CHAR_PSST" )" = "65 10" ] || test_fail_psst $LINENO
85+
86+
# =============================================================================
87+
# Test ord-chr roundtrip
88+
89+
testStr="Hello World! How are you today?"
90+
testStr2=$( conv_ord_psst "$testStr" )
91+
testStr3=$( eval "conv_chr_psst $testStr2" )
92+
[ "$testStr" = "$testStr3" ] || test_fail_psst $LINENO

0 commit comments

Comments
 (0)