commit:     a9dd85a8a1954ee516fc989a04afd7f43e58ffe8
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Fri Aug  2 18:41:50 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Aug  2 18:58:20 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=a9dd85a8

Have hr() employ a divide-by-16 strategy

A factor of 16 was shown to be faster on average by timing how long it
takes for bash to print a rule 5000 times for all lengths between 40 and
132, inclusive.

Factor  Time       StdDev
8       87.004000  3.961607
16      82.893000  3.971257

Further, 16 remains a factor of 80, which is often the number of columns
that a terminal emulator is initialised with.

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

 functions.sh   | 12 ++++++------
 test-functions |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/functions.sh b/functions.sh
index b7e6c1d..9b6220b 100644
--- a/functions.sh
+++ b/functions.sh
@@ -164,7 +164,7 @@ has_systemd()
 #
 hr()
 {
-       local char hr i length
+       local c hr i length
 
        if [ "$#" -ge 2 ] && is_int "$2"; then
                length=$2
@@ -173,15 +173,15 @@ hr()
        else
                length=80
        fi
-       char=${1--}
-       char=${char%"${char#?}"}
+       c=${1--}
+       c=${c%"${c#?}"}
        i=0
-       while [ "$(( i += 8 ))" -le "${length}" ]; do
-               hr=${hr}${char}${char}${char}${char}${char}${char}${char}${char}
+       while [ "$(( i += 16 ))" -le "${length}" ]; do
+               
hr=${hr}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}${c}
        done
        i=${#hr}
        while [ "$(( i += 1 ))" -le "${length}" ]; do
-               hr=${hr}${char}
+               hr=${hr}${c}
        done
        printf '%s\n' "${hr}"
 }

diff --git a/test-functions b/test-functions
index 43b5320..2ad76e8 100755
--- a/test-functions
+++ b/test-functions
@@ -514,10 +514,10 @@ test_hr() {
                eq  0  "$(printf '%80s' | tr ' ' -)"  -    N/A  \
                eq  0  ''                             -    0    \
                eq  0  -                              -    1    \
-               eq  0  ---------                      -    9    \
+               eq  0  -----------------              -    17    \
                eq  0  ''                             xyz  0    \
                eq  0  x                              xyz  1    \
-               eq  0  xxxxxxxxx                      xyz  9
+               eq  0  xxxxxxxxxxxxxxxxx              xyz  17
 
        callback() {
                shift

Reply via email to