From 37c8e4a16bf03509b032385482a1c13041adaf65 Mon Sep 17 00:00:00 2001 From: Isaac David Date: Sat, 7 Oct 2017 22:48:24 -0500 Subject: blacklist.sh: make `blacklist-normalize` more maintainable For about the same amount of code we can save us the hassle of messing with sed expressions every time the number of fields changes in blacklist.txt. by knowing the delimiter character(s) and the number of times it's supposed to appear, a corresponding normalizing expression is constructed on the fly. --- src/lib/blacklist.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/lib/blacklist.sh b/src/lib/blacklist.sh index 6c507f4..10eafef 100755 --- a/src/lib/blacklist.sh +++ b/src/lib/blacklist.sh @@ -2,6 +2,7 @@ # This may be included with or without `set -euE` # Copyright (C) 2013-2014, 2016-2017 Luke Shumaker +# Copyright (C) 2017 Isaac David # # License: GNU GPLv2+ # @@ -24,11 +25,14 @@ # Usage: blacklist-normalize <$file # Normalizes the syntax of the blacklist on stdin. blacklist-normalize() { - sed -r -e '/^#/d' \ - -e 's/^[^:]*$/&::::/' \ - -e 's/^[^:]*:[^:]*$/&:::/' \ - -e 's/^[^:]*(:[^:]*){2}$/&::/' \ - -e 's/^[^:]*(:[^:]*){3}$/&:/' + # dynamically build sed expression based on number of fields + local -a expr + local fields=5 sep=: i + for (( i = $fields - 2; i >= 0; --i )); do + expr+=('-e' "s/^[^:]*(:[^:]*){${i}}$/&${sep}/") + sep+=${sep:0:1} + done + sed -r -e '/^#/d' "${expr[@]}" } # Usage: blacklist-cat -- cgit v1.2.2