diff --git a/src/util/binary.sh b/src/util/binary.sh new file mode 100644 index 0000000..629a72d --- /dev/null +++ b/src/util/binary.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# +# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE +# +# Util functions to manipulate binary numbers. + +if [ -n "${BINARY_MOD:-}" ]; then return 0; fi +readonly BINARY_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +. ${BINARY_MOD}/../lang/p.sh + + +# ---------- +# Functions. + +function binary_d2b() { + # From decimal to binary. + local ctx; is_ctx "${1}" && ctx="${1}" && shift + [ $# -ne 1 ] && { ctx_wn $ctx; return $EC; } + local -r n="${1}" + shift 1 || { ctx_wn $ctx; return $EC; } + + # TODO: check for errors. + echo "obase=2; ${n}" | bc +} + +function binary_b2d() { + # From binary to decimal. + local ctx; is_ctx "${1}" && ctx="${1}" && shift + [ $# -ne 1 ] && { ctx_wn $ctx; return $EC; } + local -r n="${1}" + shift 1 || { ctx_wn $ctx; return $EC; } + + # TODO: check for errors. + echo "ibase=2;obase=A; ${n}" | bc +} diff --git a/src/util/binary_test.sh b/src/util/binary_test.sh new file mode 100644 index 0000000..c2d6e96 --- /dev/null +++ b/src/util/binary_test.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# +# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE +# +# Unit tests for the binary functions. + +if [ -n "${BINARY_TEST_MOD:-}" ]; then return 0; fi +readonly BINARY_TEST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +. ${BINARY_TEST_MOD}/binary.sh +. ${BINARY_TEST_MOD}/../testing/bunit.sh + + +# ---------- +# Functions. + +function test_binary_d2b() { + local res + + res=$(binary_d2b "3") + [ "${res}" = "11" ] || \ + assert_fail + + res=$(binary_d2b "33") + [ "${res}" = "100001" ] || \ + assert_fail + + return 0 +} +readonly -f test_binary_d2b + +function test_binary_b2d() { + local res + + res=$(binary_b2d "011") + [ "${res}" = "3" ] || \ + assert_fail + + res=$(binary_b2d "100001") + [ "${res}" = "33" ] || \ + assert_fail + + #binary_b2d "100001a" && \ + #assert_fail + + return 0 +} +readonly -f test_binary_b2d diff --git a/src/util/char.sh b/src/util/char.sh new file mode 100644 index 0000000..fc6520d --- /dev/null +++ b/src/util/char.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE +# +# Util char functions. + +if [ -n "${CHAR_MOD:-}" ]; then return 0; fi +readonly CHAR_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +. ${CHAR_MOD}/../lang/p.sh + + +# ---------- +# Functions. + +function char_alphabet() { + # Print alphabet on a single line. + local ctx; is_ctx "${1}" && ctx="${1}" && shift + [ $# -ne 0 ] && { ctx_wn $ctx; return $EC; } + shift 0 || { ctx_wn $ctx; return $EC; } + + echo {a..z} +} diff --git a/src/util/p.sh b/src/util/p.sh index 8423d5a..a444608 100644 --- a/src/util/p.sh +++ b/src/util/p.sh @@ -14,7 +14,9 @@ readonly UTIL_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) . ${UTIL_PACKAGE}/math.sh . ${UTIL_PACKAGE}/rand.sh . ${UTIL_PACKAGE}/strings.sh +. ${UTIL_PACKAGE}/char.sh . ${UTIL_PACKAGE}/regexp.sh . ${UTIL_PACKAGE}/flags.sh . ${UTIL_PACKAGE}/complex.sh . ${UTIL_PACKAGE}/user.sh +. ${UTIL_PACKAGE}/binary.sh