@@ -15,12 +15,15 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
15
15
16
16
# #
17
17
# SUBPROCESS
18
- # conv_chr_psst <charCode>
18
+ # conv_chr_psst <charCode> [<charCode>] [<charCode>] ...
19
19
#
20
20
# SUMMARY
21
21
# Returns character whose character code is `charCode`.
22
22
# E.g `65` becomes `A`, `97` becomes `a` and `32` becomes space.
23
23
#
24
+ # If multiple character codes are passed, the resulting characters are
25
+ # returned as a concatinated string.
26
+ #
24
27
# PARAMETERS
25
28
# charCode: Character code of desired character.
26
29
#
@@ -31,44 +34,43 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
31
34
# 3: Character code is newline (`\n``), which cannot be captured.
32
35
#
33
36
# OUTPUT
34
- # stdout: The characters whose code is `charCode`.
37
+ # stdout: The character whose code is `charCode`.
35
38
#
36
39
# SAMPLE
37
40
# digit9=$( chr_psst 57 )
38
41
#
39
42
conv_chr_psst ()
40
43
(
41
44
func=" chr_psst"
42
- assert_argc_psst " $func " 1 $#
45
+ assert_minargc_psst " $func " 1 $#
43
46
assert_hasarg_psst " $func " " charCode" " $1 "
44
47
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
46
54
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
54
58
)
55
59
56
60
57
61
# #
58
62
# SUBPROCESS
59
- # conv_ord_psst <char >
63
+ # conv_ord_psst <chars >
60
64
#
61
65
# SUMMARY
62
66
# Returns character code of character.
63
67
# E.g `A` becomes `65`, `a` becomes `97`, and space becomes `32`.
64
68
#
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 .
67
71
#
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.
72
74
#
73
75
# OUTPUT
74
76
# stdout: The character code of `char`.
@@ -78,13 +80,23 @@ conv_chr_psst()
78
80
#
79
81
conv_ord_psst ()
80
82
(
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 "
83
86
84
- char=$1
87
+ chars=$1
88
+ [ ${# chars} -eq 1 ] && { LC_CTYPE=C printf " %d" " '$chars " ; return 0; }
85
89
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
+ )
88
101
89
- LC_CTYPE=C printf " %d" " '$char "
90
102
)
0 commit comments