diff options
author | Kerin Millar <kfm@plushkava.net> | 2024-06-27 21:42:23 +0100 |
---|---|---|
committer | Kerin Millar <kfm@plushkava.net> | 2024-06-28 18:39:33 +0100 |
commit | b720081b68dfba3ed4ebacb4d7564977708bfa25 (patch) | |
tree | 3fcb3558e61081797cd316c6ca3de1d9fa95648c | |
parent | Add the up() function to experimental (diff) | |
download | gentoo-functions-b720081b68dfba3ed4ebacb4d7564977708bfa25.tar.gz gentoo-functions-b720081b68dfba3ed4ebacb4d7564977708bfa25.tar.bz2 gentoo-functions-b720081b68dfba3ed4ebacb4d7564977708bfa25.zip |
Add the int_between() and str_between() functions to experimental
Signed-off-by: Kerin Millar <kfm@plushkava.net>
-rw-r--r-- | functions/experimental.sh | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/functions/experimental.sh b/functions/experimental.sh index e02923a..1aac078 100644 --- a/functions/experimental.sh +++ b/functions/experimental.sh @@ -13,6 +13,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. # This technique is loosely based on the IO::Interactive::Tiny module from CPAN. @@ -53,6 +71,30 @@ 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 # four characters must form the special prefix, ".../". It recurses upwards from |