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

Commit d31d716

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Add support for mutli-char conversion
1 parent 5e0fc9e commit d31d716

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

lib/psst/basic/conv.inc.sh

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
1515

1616
##
1717
# SUBPROCESS
18-
# conv_chr_psst <charCode>
18+
# conv_chr_psst <charCode> [<charCode>] [<charCode>] ...
1919
#
2020
# SUMMARY
2121
# Returns character whose character code is `charCode`.
2222
# E.g `65` becomes `A`, `97` becomes `a` and `32` becomes space.
2323
#
24+
# If multiple character codes are passed, the resulting characters are
25+
# returned as a concatinated string.
26+
#
2427
# PARAMETERS
2528
# charCode: Character code of desired character.
2629
#
@@ -31,44 +34,43 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
3134
# 3: Character code is newline (`\n``), which cannot be captured.
3235
#
3336
# OUTPUT
34-
# stdout: The characters whose code is `charCode`.
37+
# stdout: The character whose code is `charCode`.
3538
#
3639
# SAMPLE
3740
# digit9=$( chr_psst 57 )
3841
#
3942
conv_chr_psst()
4043
(
4144
func="chr_psst"
42-
assert_argc_psst "$func" 1 $#
45+
assert_minargc_psst "$func" 1 $#
4346
assert_hasarg_psst "$func" "charCode" "$1"
4447

45-
charCode=$1
48+
while [ -n "${1-}" ]
49+
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
4654

47-
test_is_int_psst "$charCode" || return 1
48-
{ [ "$charCode" -lt 0 ] || [ "$charCode" -gt 255 ]; } && return 2
49-
[ "$charCode" != "10" ] || return 3
50-
51-
pattern=$( printf "%03o" "$charCode" )
52-
# shellcheck disable=SC2059
53-
printf "\\${pattern}"
55+
printf "%b" "$( printf '\%o' "$charCode" )"
56+
shift
57+
done
5458
)
5559

5660

5761
##
5862
# SUBPROCESS
59-
# conv_ord_psst <char>
63+
# conv_ord_psst <chars>
6064
#
6165
# SUMMARY
6266
# Returns character code of character.
6367
# E.g `A` becomes `65`, `a` becomes `97`, and space becomes `32`.
6468
#
65-
# PARAMETERS
66-
# char: Character whose code shall be returned.
69+
# If multiple characters are passed, the resulting character codes are
70+
# returned as a concatinated string separated by space.
6771
#
68-
# RETURNS
69-
# 0: Argument was a valid character.
70-
# 1: Argument contained zero characters.
71-
# 2: Argument contained more than one character.
72+
# PARAMETERS
73+
# char: Characters whose code shall be returned.
7274
#
7375
# OUTPUT
7476
# stdout: The character code of `char`.
@@ -78,13 +80,23 @@ conv_chr_psst()
7880
#
7981
conv_ord_psst()
8082
(
81-
func_psst="ord_psst"
82-
assert_argc_psst "$func_psst" 1 $#
83+
func="ord_psst"
84+
assert_argc_psst "$func" 1 $#
85+
assert_hasarg_psst "$func" "chars" "$1"
8386

84-
char=$1
87+
chars=$1
88+
[ ${#chars} -eq 1 ] && { LC_CTYPE=C printf "%d" "'$chars"; return 0; }
8589

86-
[ -n "$char" ] || return 1
87-
[ ${#char} -eq 1 ] || return 2
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+
)
88101

89-
LC_CTYPE=C printf "%d" "'$char"
90102
)

test/basic/bin/test_conv.sh

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ fi
2121
set +e
2222
( conv_chr_psst 2>/dev/null )
2323
[ $? = 127 ] || test_fail_psst $LINENO
24-
25-
( conv_chr_psst 1 2 2>/dev/null )
26-
[ $? = 127 ] || test_fail_psst $LINENO
2724
set -e
2825

2926
# Argument must not be empty
@@ -48,6 +45,8 @@ conv_chr_psst "350" >/dev/null || {
4845
[ "$( conv_chr_psst 65 )" = "A" ] || test_fail_psst $LINENO
4946
[ "$( conv_chr_psst 97 )" = "a" ] || test_fail_psst $LINENO
5047
[ "$( conv_chr_psst 32 )" = " " ] || test_fail_psst $LINENO
48+
[ "$( conv_chr_psst 65 97 32 )" = "Aa " ] || test_fail_psst $LINENO
49+
5150

5251
[ "$( conv_chr_psst 10 )" = "" ] || test_fail_psst $LINENO
5352
if conv_chr_psst "10" >/dev/null
@@ -71,24 +70,15 @@ set +e
7170
set -e
7271

7372
# Argument must not be empty
74-
if conv_ord_psst ""
75-
then
76-
test_fail_psst $LINENO
77-
else
78-
[ $? = 1 ] || test_fail_psst $LINENO
79-
fi
80-
81-
# Argument must be single character
82-
83-
if conv_ord_psst "12"
84-
then
85-
test_fail_psst $LINENO
86-
else
87-
[ $? = 2 ] || test_fail_psst $LINENO
88-
fi
73+
set +e
74+
( conv_ord_psst "" 2>/dev/null )
75+
[ $? = 127 ] || test_fail_psst $LINENO
76+
set -e
8977

9078
# Test some conversions
9179
[ "$( conv_ord_psst A )" = 65 ] || test_fail_psst $LINENO
9280
[ "$( conv_ord_psst a )" = 97 ] || test_fail_psst $LINENO
9381
[ "$( conv_ord_psst " " )" = 32 ] || test_fail_psst $LINENO
9482
[ "$( conv_ord_psst "$NL_CHAR_PSST" )" = 10 ] || test_fail_psst $LINENO
83+
[ "$( 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

0 commit comments

Comments
 (0)