Menu

[a31335]: / source / filter.sh  Maximize  Restore  History

Download this file

108 lines (84 with data), 1.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
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 ]]
}