From 7fb50e1cd63ae5e460b0aed686c5e4e32feda85f Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 3 Apr 2021 11:34:51 +0900 Subject: [PATCH] include/is: Add functions for determining the content of strings This commit adds ctype.h-style functions for determining what kind of data is stored in strings. --- include/is.sh | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 include/is.sh diff --git a/include/is.sh b/include/is.sh new file mode 100644 index 0000000..ac22f82 --- /dev/null +++ b/include/is.sh @@ -0,0 +1,77 @@ +#!bin/bash + +__init() { + return 0 +} + +is_digit() { + local str + + str="$1" + + if [[ "$str" =~ ^[0-9]+$ ]]; then + return 0 + fi + + return 1 +} + +is_hex() { + local str + + str="$1" + + if [[ "$str" =~ ^[0-9a-fA-F]+$ ]]; then + return 0 + fi + + return 1 +} + +is_upper() { + local str + + str="$1" + + if [[ "$str" =~ ^[A-Z]+$ ]]; then + return 0 + fi + + return 1 +} + +is_lower() { + local str + + str="$1" + + if [[ "$str" =~ ^[a-z]+$ ]]; then + return 0 + fi + + return 1 +} + +is_alpha() { + local str + + str="$1" + + if is_upper "$str" || is_lower "$str"; then + return 0 + fi + + return 1 +} + +is_alnum() { + local str + + str="$1" + + if is_alpha "$str" || is_digit "$str"; then + return 0 + fi + + return 1 +} -- 2.47.3