From 5313601c79e4016661b878434a9823cba86620fe Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Tue, 27 Feb 2024 17:59:19 +1300 Subject: [PATCH] configure: add basic list utilities inlist determines whether an item is in a list removefrom returns a list with an element removed trim strips whitespace from a string, using xargs --- configure | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/configure b/configure index 950e84c..16cb5c3 100755 --- a/configure +++ b/configure @@ -25,6 +25,44 @@ throw () { exit 1 } +# +## list utilities +# + +# Returns 0 if the given item is in the given list, otherwise returns 1. +inlist () { + local item="$1" + local list="$2" + + for elem in $list; do + if [ "$elem" = "$item" ]; then + return 0 + fi + done + + return 1 +} + +# Removes the given item from the given list and returns the new list. +removefrom () { + local item="$1" + local list="$2" + local newlist="" + + for elem in $list; do + if ! [ "$elem" = "$item" ]; then + newlist="$newlist $elem" + fi + done + + printf '%s' "$newlist" +} + +# Trims the given string of leading and trailing whitespace. +trim () { + printf '%s' "$1" | xargs +} + # ## flag generators #