include globals.sh
include shopts/nocasematch.sh
#
# filter.sh
#
# Provides filter() and filter_negatable().
#
# Author: konsolebox
# Copyright Free / Public Domain
# 2015/07/05
#
# filter (array, *keywords)
#
# Filters array.
#
# > __A0 = filtered array
# > __I0 = size
#
function filter {
local __R="$1[@]" X
shift
Shopts.NoCaseMatch.save
if [[ ${GLOBALS_CASE_SENSITIVE} == true ]]; then
Shopts.NoCaseMatch.disable
else
Shopts.NoCaseMatch.enable
fi
__A0=() __I0=0
for X in "${!__R}"; do
for __; do
[[ $X == *"$__"* ]] || continue 2
done
__A0[__I0++]=$X
done
Shopts.NoCaseMatch.restore
[[ __I0 -gt 0 ]]
}
# filter_negatable (array, *keywords)
#
# Filters array.
#
# This verion allows negation in keywords.
# TODO: Add more details.
#
# > __A0 = filtered array
# > __I0 = size
#
function filter_negatable {
local __R="$1[@]" X
shift
local VAL=() INV=() NEG=0 N
for __; do
case $__ in
+(!))
(( NEG = (NEG + ${#__}) % 2 ))
;;
!*)
N=${__%%+([^!])*}
(( (NEG + ${#N}) % 2 )) && INV+=("${__:${#N}}") || VAL+=("${__:${#N}}")
;;
*)
(( NEG )) && INV+=("$__") || VAL+=("$__")
;;
esac
done
Shopts.NoCaseMatch.save
if [[ ${GLOBALS_CASE_SENSITIVE} == true ]]; then
Shopts.NoCaseMatch.disable
else
Shopts.NoCaseMatch.enable
fi
__A0=() __I0=0
for X in "${!__R}"; do
for __ in "${VAL[@]}"; do
[[ $X == *"$__"* ]] || continue 2
done
for __ in "${INV[@]}"; do
[[ $X == *"$__"* ]] && continue 2
done
__A0[__I0++]=$X
done
Shopts.NoCaseMatch.restore
[[ __I0 -gt 0 ]]
}