]> git.corax.cc Git - toolbox/commitdiff
include/is: Add functions for determining the content of strings
authorMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:34:51 +0000 (11:34 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:34:51 +0000 (11:34 +0900)
This commit adds ctype.h-style functions for determining what kind of
data is stored in strings.

include/is.sh [new file with mode: 0644]

diff --git a/include/is.sh b/include/is.sh
new file mode 100644 (file)
index 0000000..ac22f82
--- /dev/null
@@ -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
+}