File tree 4 files changed +109
-0
lines changed
4 files changed +109
-0
lines changed Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+ #
3
+ # https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4
+ #
5
+ # Util functions to manipulate binary numbers.
6
+
7
+ if [ -n " ${BINARY_MOD:- } " ]; then return 0; fi
8
+ readonly BINARY_MOD=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd )
9
+
10
+ . ${BINARY_MOD} /../lang/p.sh
11
+
12
+
13
+ # ----------
14
+ # Functions.
15
+
16
+ function binary_d2b() {
17
+ # From decimal to binary.
18
+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
19
+ [ $# -ne 1 ] && { ctx_wn $ctx ; return $EC ; }
20
+ local -r n=" ${1} "
21
+ shift 1 || { ctx_wn $ctx ; return $EC ; }
22
+
23
+ # TODO: check for errors.
24
+ echo " obase=2; ${n} " | bc
25
+ }
26
+
27
+ function binary_b2d() {
28
+ # From binary to decimal.
29
+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
30
+ [ $# -ne 1 ] && { ctx_wn $ctx ; return $EC ; }
31
+ local -r n=" ${1} "
32
+ shift 1 || { ctx_wn $ctx ; return $EC ; }
33
+
34
+ # TODO: check for errors.
35
+ echo " ibase=2;obase=A; ${n} " | bc
36
+ }
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+ #
3
+ # https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4
+ #
5
+ # Unit tests for the binary functions.
6
+
7
+ if [ -n " ${BINARY_TEST_MOD:- } " ]; then return 0; fi
8
+ readonly BINARY_TEST_MOD=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd )
9
+
10
+ . ${BINARY_TEST_MOD} /binary.sh
11
+ . ${BINARY_TEST_MOD} /../testing/bunit.sh
12
+
13
+
14
+ # ----------
15
+ # Functions.
16
+
17
+ function test_binary_d2b() {
18
+ local res
19
+
20
+ res=$( binary_d2b " 3" )
21
+ [ " ${res} " = " 11" ] || \
22
+ assert_fail
23
+
24
+ res=$( binary_d2b " 33" )
25
+ [ " ${res} " = " 100001" ] || \
26
+ assert_fail
27
+
28
+ return 0
29
+ }
30
+ readonly -f test_binary_d2b
31
+
32
+ function test_binary_b2d() {
33
+ local res
34
+
35
+ res=$( binary_b2d " 011" )
36
+ [ " ${res} " = " 3" ] || \
37
+ assert_fail
38
+
39
+ res=$( binary_b2d " 100001" )
40
+ [ " ${res} " = " 33" ] || \
41
+ assert_fail
42
+
43
+ # binary_b2d "100001a" && \
44
+ # assert_fail
45
+
46
+ return 0
47
+ }
48
+ readonly -f test_binary_b2d
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+ #
3
+ # https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4
+ #
5
+ # Util char functions.
6
+
7
+ if [ -n " ${CHAR_MOD:- } " ]; then return 0; fi
8
+ readonly CHAR_MOD=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd )
9
+
10
+ . ${CHAR_MOD} /../lang/p.sh
11
+
12
+
13
+ # ----------
14
+ # Functions.
15
+
16
+ function char_alphabet() {
17
+ # Print alphabet on a single line.
18
+ local ctx; is_ctx " ${1} " && ctx=" ${1} " && shift
19
+ [ $# -ne 0 ] && { ctx_wn $ctx ; return $EC ; }
20
+ shift 0 || { ctx_wn $ctx ; return $EC ; }
21
+
22
+ echo {a..z}
23
+ }
Original file line number Diff line number Diff line change @@ -14,7 +14,9 @@ readonly UTIL_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
14
14
. ${UTIL_PACKAGE} /math.sh
15
15
. ${UTIL_PACKAGE} /rand.sh
16
16
. ${UTIL_PACKAGE} /strings.sh
17
+ . ${UTIL_PACKAGE} /char.sh
17
18
. ${UTIL_PACKAGE} /regexp.sh
18
19
. ${UTIL_PACKAGE} /flags.sh
19
20
. ${UTIL_PACKAGE} /complex.sh
20
21
. ${UTIL_PACKAGE} /user.sh
22
+ . ${UTIL_PACKAGE} /binary.sh
You can’t perform that action at this time.
0 commit comments