-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_number
More file actions
executable file
·130 lines (125 loc) · 3.27 KB
/
check_number
File metadata and controls
executable file
·130 lines (125 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
# #region Help & Version
function show_help() {
cat <<_EOF_
Assert that the input is a correctly formatted number.
Usage: $0 [-idsurk] [-b BASE] NUMBER
Options:
-i, --integer: must be a integer
-d, --decimal: must be a decimal
-s, --signed: must be signed
-u, --unsigned: must be unsigned (if remove_plus, can be labeled positive)
-r, --remove_plus: Remove the plus sign from the output if in the input
-k, --keep_plus: Keep the plus sign from the output if in the input
_EOF_
}
function show_version() {
cat <<_EOF_
$0 1.0.0
Copyright (C) 2025 Justin Morris
_EOF_
}
# #endregion Help & Version
shopt -s extglob globasciiranges
readonly DECIMAL_MATCHER='[[:digit:]]'
# readonly HEX_MATCHER='[[:xdigit:]]'
# readonly OCTAL_MATCHER='[01234567]'
# readonly PRE_DECIMAL_MATCHER=''
# readonly PRE_HEX_MATCHER='0[xX]'
# readonly PRE_OCTAL_MATCHER='0'
declare pattern='^'
declare pattern_body='MATCH+\.?$'
declare is_decimal='false'
declare is_signed='false'
declare remove_plus='true'
# declare -i allowed_base=10
# declare force_base='false'
while [[ "$1" =~ ^(-[[:alpha:]]|--[[:alpha:]_]+)$ ]]; do
case "$1" in
(-i | --integer)
pattern_body='MATCH+\.?$'
if ! declare -r is_decimal='false'; then exit 1; fi
shift
;;
(-d | --decimal)
pattern_body='(MATCH+\.?|MATCH*\.MATCH+)$'
if ! declare -r is_decimal='true'; then exit 1; fi
shift
;;
(-s | --signed)
pattern='^(-|\+)?'
if ! declare -r is_signed='true'; then exit 1; fi
shift
;;
(-u | --unsigned)
pattern='^'
if ! declare -r is_signed='false'; then exit 1; fi
shift
;;
(-r | --remove_plus)
if ! declare -r remove_plus='true'; then exit 1; fi
shift
;;
(-k | --keep_plus)
if ! declare -r remove_plus='false'; then exit 1; fi
shift
;;
# (-b | --base)
# shift
# case "$1" in
# (decimal)
# if ! declare -r allowed_base=10; then exit 1; fi
# ;;
# (hex | hexadecimal)
# if ! declare -r allowed_base=16; then exit 1; fi
# ;;
# (oct | octal)
# if ! declare -r allowed_base=8; then exit 1; fi
# ;;
# ([[:digit:]]+)
# if ! declare -r allowed_base="$1"; then exit 1; fi
# if ((allowed_base < 2 || allowed_base > 64)); then
# echo "Base must be decimal, oct(al), hex(adecimal), or an unsigned integer between 2 & 64 inclusive; was $1" 1>&2
# exit 1
# fi
# ;;
# (*)
# echo "Base must be decimal, oct(al), hex(adecimal), or an unsigned integer between 2 & 64 inclusive; was $1" 1>&2
# exit 1
# ;;
# esac
# shift
# ;;
(-h | --help)
show_help
exit 0
;;
(-v | --version)
show_version
exit 0
;;
(*)
break;
;;
esac
done
pattern+="${pattern_body//MATCH/$DECIMAL_MATCHER}"
declare -r pattern
declare -r text="$*"
if [[ "$text" =~ $pattern ]]; then
if eval $remove_plus && [ "${text:0:1}" = '+' ]; then
echo -n "${text:1}"
else
echo -n "$text"
fi
elif eval "$remove_plus" && [ "${text:0:1}" = '+' ] && [[ "${text:1}" =~ $pattern ]]; then
echo -n "${text:1}"
# elif [ "$text" = '_' ]; then :; # index is last entry
else
err_msg="must be a"
if eval $is_signed; then err_msg+=" signed "; else err_msg+="n unsigned "; fi
if eval $is_decimal; then err_msg+="decimal number"; else err_msg+="integer"; fi
echo "$err_msg; value was '$text'" 1>&2
exit 1
fi
exit 0