From 1e3c825b74b2f7285f118bc9c05ea95642bd8d62 Mon Sep 17 00:00:00 2001 From: Kevin Daudt <kdaudt@alpinelinux.org> Date: Thu, 17 Oct 2024 14:37:51 +0200 Subject: [PATCH] functions: add helpers to convert case Posix shell can do a lot of things, but there is no native way to change the case of a string. Some packages require this functionality, and they use `tr` in a subshell to achieve that. In the global scope of APKBUILDs we want to avoid exec in the global scope. By providing helper functions, we can avoid this. The functions only handle the basic ascii letters, which should suffice for the usecases that are needed in APKBUILD files. --- functions.sh.in | 20 ++++++++++++++++++++ tests/functions_test | 16 +++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/functions.sh.in b/functions.sh.in index 7b009806..189a67f7 100644 --- a/functions.sh.in +++ b/functions.sh.in @@ -273,6 +273,26 @@ error2() { printf " %s\n" "$1" >&2 } +lowercase() { + str=$1 + for pair in Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz; do + orig=${pair:0:1} + repl=${pair:1:1} + str=${str//"$orig"/"$repl"} + done + echo "$str" +} + +uppercase() { + str=$1 + for pair in aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ; do + orig=${pair:0:1} + repl=${pair:1:1} + str=${str//"$orig"/"$repl"} + done + echo "$str" +} + set_xterm_title() { if [ "$TERM" = xterm ] && [ -n "$USE_COLORS" ]; then printf "\033]0;$1\007" >&2 diff --git a/tests/functions_test b/tests/functions_test index fd6e5943..8665895a 100755 --- a/tests/functions_test +++ b/tests/functions_test @@ -7,7 +7,9 @@ init_tests \ funcs_check_missing_apk \ funcs_check_missing_git \ funcs_check_env_set_but_empty \ - funcs_check_defconfig + funcs_check_defconfig \ + funcs_check_lowercase \ + funcs_check_uppercase FUNCS=$(atf_get_srcdir)/../functions.sh @@ -85,3 +87,15 @@ funcs_check_defconfig_body() { -o match:"envcflags" \ sh -e -c " . $FUNCS; echo \"\$CFLAGS\"" } + +funcs_check_lowercase_body() { + atf_check \ + -o match:"aabcdefghijklkmnopqrstuvwxyz" \ + sh -e -c " . $FUNCS; lowercase 'AABCDEFGHIJKLKMNOPQRSTUVWXYZ'" +} + +funcs_check_uppercase_body() { + atf_check \ + -o match:"AABCDEFGHIJKLKMNOPQRSTUVWXYZ" \ + sh -e -c " . $FUNCS; uppercase 'aabcdefghijklkmnopqrstuvwxyz'" +} -- GitLab