Skip to contentSkip to conversation

mbuto: add a function to join elements of a list into a string

PATCH Applied Sevinj Aghayeva 2 participants 2 messages Apr 15, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mbuto: add a function to join elements of a list into a string

In multiple places we loop over a list by joining its elements with a prefix, suffix, and a delimiter into a single string. Add a function for this functionality and replace these loops with a function.

---
 mbuto | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
mbuto +21 -15
index 14176c3..c1695fb 100755
@@ -223,21 +223,11 @@ profile_kselftests() {
 	if [ -f ${__cfg} ]; then
 		__mods=$(${AWK} -F'=' '/=m/ {print $1}' ${__cfg} | ${SORT} -u)
 
-		__egrep_pattern=""
-		for __c in ${__mods}; do
-	    		__egrep_pattern='^obj-\$\('"${__c}"'\).*.o$|'"${__egrep_pattern}"
-		done
-		__egrep_pattern=${__egrep_pattern%|}
-
-		__kmods_needed=
-		__find_pattern=
-		for __mo in $(${EGREP} -rn --include "*Makefile" ${__egrep_pattern} | \
-				${AWK} -F'+=' '/+=/ {print $2}'); do
-			__m=$(${BASENAME} -s .o ${__mo})
-			__find_pattern="-name ${__m}.ko -o ${__find_pattern}"
-			__kmods_needed="${__m} ${__kmods_needed}"
-		done
-		__find_pattern=${__find_pattern%-o }
+		__egrep_pattern=$(list_join "${__mods}" '^obj-\$\(' '\).*.o$' '|')
+		__egrep_result=$(${BASENAME} -a -s .o \
+				$(${EGREP} -rn --include "*Makefile" ${__egrep_pattern} | \
+  				${AWK} -F'+=' '/+=/ {print $2}'))
+		__find_pattern=$(list_join "${__egrep_result}" '-name ' '.ko ' '-o ')
 
 		KMODS=$(${FIND} "${MODDIR}" ${__find_pattern} | ${TR} '\n' ' ')
 
@@ -245,6 +235,7 @@ profile_kselftests() {
 
 		KMODS="$(${BASENAME} -a -s .ko ${KMODS})"
 
+		__kmods_needed=$(list_join "${__egrep_result}" '' '' ' ')
 		__kmods_missing=$(list_diff "${__kmods_needed}" "${KMODS}")
 
 		if [ ! -z "${__kmods_missing}" ]; then
@@ -435,6 +426,21 @@ list_diff() {
 	printf '%s' "${__diff}"
 }
 
+# list_join() - Given a list of tokens $1, prepend prefix $2 and append suffix
+# $3 to each token and join them with a delimiter $3 into a single string
+# $1: list
+# $2: prefix
+# $3: suffix
+# $4: delimiter
+list_join() {
+	__s=
+	for __t in ${1}; do
+		__s="${2}${__t}${3}${4}${__s}"
+	done
+	__s=${__s%${4}}
+	printf '%s' "${__s}"
+}
+
 ################################################################################
 
 
Signature · 1 line
2.25.1
#1 lore raw

Applied, minor changes:

Quoted 27 lines · On Fri, 15 Apr 2022 06:23:17 -0400 Sevinj Aghayeva <[email protected]>…

In multiple places we loop over a list by joining its elements with a prefix, suffix, and a delimiter into a single string. Add a function for this functionality and replace these loops with a function.

---
 mbuto | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
mbuto +0 -10
index 14176c3..c1695fb 100755
@@ -223,21 +223,11 @@ profile_kselftests() {
 	if [ -f ${__cfg} ]; then
 		__mods=$(${AWK} -F'=' '/=m/ {print $1}' ${__cfg} | ${SORT} -u)
 
-		__egrep_pattern=""
-		for __c in ${__mods}; do
-	    		__egrep_pattern='^obj-\$\('"${__c}"'\).*.o$|'"${__egrep_pattern}"
-		done
-		__egrep_pattern=${__egrep_pattern%|}
-
-		__kmods_needed=
-		__find_pattern=
-		for __mo in $(${EGREP} -rn --include "*Makefile" ${__egrep_pattern} | \
-				${AWK} -F'+=' '/+=/ {print $2}'); do

mbuto profile_kselftests() { · line 230

Quoted 10 lines
mbuto +5 -5
-			__m=$(${BASENAME} -s .o ${__mo})
-			__find_pattern="-name ${__m}.ko -o ${__find_pattern}"
-			__kmods_needed="${__m} ${__kmods_needed}"
-		done
-		__find_pattern=${__find_pattern%-o }
+		__egrep_pattern=$(list_join "${__mods}" '^obj-\$\(' '\).*.o$' '|')
+		__egrep_result=$(${BASENAME} -a -s .o \
+				$(${EGREP} -rn --include "*Makefile" ${__egrep_pattern} | \
+  				${AWK} -F'+=' '/+=/ {print $2}'))
+		__find_pattern=$(list_join "${__egrep_result}" '-name ' '.ko ' '-o ')

Aligned arguments like you would do in C (in the kernel), and dropped "egrep_" from variable names (that's already clear), it's slightly more readable now and fits in 80 columns.

Indeed, in general, it's not very readable anyway, but I guess it's an acceptable trade-off considering the language at hand.

Quoted 12 lines
mbuto +1 -0
 		KMODS=$(${FIND} "${MODDIR}" ${__find_pattern} | ${TR} '\n' ' ')
 
@@ -245,6 +235,7 @@ profile_kselftests() {
 
 		KMODS="$(${BASENAME} -a -s .ko ${KMODS})"
 
+		__kmods_needed=$(list_join "${__egrep_result}" '' '' ' ')
 		__kmods_missing=$(list_diff "${__kmods_needed}" "${KMODS}")
 
 		if [ ! -z "${__kmods_missing}" ]; then

mbuto list_diff() { · line 430

Quoted 5 lines
mbuto +2 -0
@@ -435,6 +426,21 @@ list_diff() {
 	printf '%s' "${__diff}"
 }
 
+# list_join() - Given a list of tokens $1, prepend prefix $2 and append suffix
+# $3 to each token and join them with a delimiter $3 into a single string

I reworded this a bit: arguments are already described below.

mbuto list_diff() { · line 434

Quoted 4 lines
mbuto +4 -0
+# $1: list
+# $2: prefix
+# $3: suffix
+# $4: delimiter

...clarified the usage of delimiter, used tabs for consistency.

Quoted 12 lines
mbuto +9 -0
+list_join() {
+	__s=
+	for __t in ${1}; do
+		__s="${2}${__t}${3}${4}${__s}"
+	done
+	__s=${__s%${4}}
+	printf '%s' "${__s}"
+}
+
 ################################################################################
 
 
Signature · 1 line
Stefano