commit:     b720081b68dfba3ed4ebacb4d7564977708bfa25
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Jun 27 20:42:23 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Jun 28 17:39:33 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=b720081b

Add the int_between() and str_between() functions to experimental

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>

 functions/experimental.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/functions/experimental.sh b/functions/experimental.sh
index e02923a..1aac078 100644
--- a/functions/experimental.sh
+++ b/functions/experimental.sh
@@ -12,6 +12,24 @@
 
 warn "sourcing the experimental module from gentoo-functions; no stability 
guarantee is provided"
 
+#
+# Expects three parameters, all of which must be integers, and determines
+# whether the first is numerically greater than or equal to the second, and
+# numerically lower than or equal to the third.
+#
+int_between()
+{
+       if [ "$#" -lt 3 ]; then
+               warn "int_between: too few arguments (got $#, expected 3)"
+               false
+       elif ! is_int "$2" || ! is_int "$3"; then
+               _warn_for_args int_between "$@"
+               false
+       else
+               is_int "$1" && [ "$1" -ge "$2" ] && [ "$1" -le "$3" ]
+       fi
+}
+
 #
 # Returns 0 provided that two conditions hold. Firstly, that the standard input
 # is connected to a tty. Secondly, that the standard output has not been 
closed.
@@ -52,6 +70,30 @@ prepend_ts()
        prepend_ts
 }
 
+#
+# Expects three parameters and determines whether the first is 
lexicographically
+# greater than or equal to the second, and lexicographically lower than or 
equal
+# to the third. The effective system collation shall affect the results, given
+# the involvement of the sort(1) utility.
+#
+str_between()
+{
+       local i
+
+       if [ "$#" -ne 3 ]; then
+               warn "str_between: wrong number of arguments (got $#, expected 
3)"
+               false
+       else
+               set -- "$2" "$1" "$3"
+               i=0
+               printf '%s\n' "$@" |
+               sort |
+               while IFS= read -r line; do
+                       eval "[ \"\${line}\" = \"\$$(( i += 1 ))\" ]" || ! break
+               done
+       fi
+}
+
 #
 # Takes the first parameter as either a relative pathname or an integer
 # referring to a number of iterations. To be recognised as a pathname, the 
first

Reply via email to