1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 18:56:52 +03:00
fq/pkg/interp/grep.jq

47 lines
1.2 KiB
Plaintext
Raw Normal View History

include "internal";
include "binary";
include "decode";
def grep_by(f):
( ..
| select(f)?
);
2021-09-23 19:35:04 +03:00
def _value_grep_string_cond($v; $flags):
if type == "string" then test($v; $flags)
else false
end;
2021-09-23 19:35:04 +03:00
def _value_grep_other_cond($v; $flags):
. == $v;
2021-09-23 19:35:04 +03:00
def vgrep($v; $flags):
if $v | type == "string" then
grep_by(_is_scalar and _value_grep_string_cond($v; $flags))
else
grep_by(_is_scalar and _value_grep_other_cond($v; $flags))
end;
2021-09-23 19:35:04 +03:00
def vgrep($v): vgrep($v; "");
def _buf_grep_any_cond($v; $flags):
(isempty(tobytesrange | match($v; $flags)) | not)? // false;
2021-09-23 19:35:04 +03:00
def bgrep($v; $flags):
if $v | type == "string" then
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags))
else
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags))
end;
2021-09-23 19:35:04 +03:00
def bgrep($v): bgrep($v; "");
def grep($v; $flags):
if $v | type == "string" then
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_string_cond($v; $flags))
else
grep_by(_is_scalar and _buf_grep_any_cond($v; $flags) or _value_grep_other_cond($v; $flags))
end;
2021-09-23 19:35:04 +03:00
def grep($v): grep($v; "");
def fgrep($v; $flags):
grep_by(_is_decode_value and (._name | test($v; $flags))? // false);
2021-09-23 19:35:04 +03:00
def fgrep($v): fgrep($v; "");