Skip to content

Commit

Permalink
interp: Replace find with overloaded match that support buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Oct 20, 2021
1 parent 093ee71 commit eedfd16
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ notable is support for arbitrary-precision integers.
- `format_root/0` return root value of format for value
- `parent/0` return parent value
- `parents/0` output parents of value
- All `find` and `grep` functions take 1 or 2 arguments. First is a scalar to match, where a string is
- All `match` and `grep` functions take 1 or 2 arguments. First is a scalar to match, where a string is
treated as a regexp. A buffer scalar will be matches exact bytes. Second argument are regexp
flags with addition that "b" will treat each byte in the input buffer as a code point, this
makes it possible to match exact bytes, ex: `find("\u00ff"; b")` will match the byte `0xff` and not
makes it possible to match exact bytes, ex: `match("\u00ff"; b")` will match the byte `0xff` and not
the UTF-8 encoded codepoint for 255.
- `find/1`, `find/2` match in buffer and output match buffers
- `match/1`, `match/2` overloaded to support buffers. Match in buffer and output match buffers
- `grep/1`, `grep/2` recursively match value and buffer
- `vgrep/1`, `vgrep/2` recursively match value
- `bgrep/1`, `bgrep/2` recursively match buffer
Expand Down
4 changes: 2 additions & 2 deletions pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (i *Interp) makeFunctions() []Function {
{[]string{"path_unescape"}, 0, 0, i.pathUnescape, nil},
{[]string{"aes_ctr"}, 1, 2, i.aesCtr, nil},

{[]string{"find"}, 1, 2, nil, i.find},
{[]string{"_bits_match"}, 1, 2, nil, i._bitsMatch},
}

return fs
Expand Down Expand Up @@ -835,7 +835,7 @@ func (i *Interp) aesCtr(c interface{}, a []interface{}) interface{} {
return buf.Bytes()
}

func (i *Interp) find(c interface{}, a []interface{}) gojq.Iter {
func (i *Interp) _bitsMatch(c interface{}, a []interface{}) gojq.Iter {
var ok bool

bv, err := toBufferView(c)
Expand Down
7 changes: 7 additions & 0 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def tobytesrange: _tobitsrange(8);
def tobits: _tobitsrange(1; false);
def tobytes: _tobitsrange(8; false);

# overload match to support buffers
def _orig_match($val): match($val);
def _orig_match($regex; $flags): match($regex; $flags);
def _is_buffer: type == "buffer";
def match($val): if _is_buffer then _bits_match($val) else _orig_match($val) end;
def match($regex; $flags): if _is_buffer then _bits_match($regex; $flags) else _orig_match($regex; $flags) end;

def formats:
_registry.formats;

Expand Down
2 changes: 1 addition & 1 deletion pkg/interp/grep.jq
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def vgrep($v; $flags):
def vgrep($v): vgrep($v; "");

def _buf_grep_any_cond($v; $flags):
(isempty(find($v; $flags)) | not)? // false;
(isempty(tobytesrange | match($v; $flags)) | not)? // false;
def bgrep($v; $flags):
_grep(
$v;
Expand Down
8 changes: 4 additions & 4 deletions pkg/interp/testdata/grep.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ mp3> bgrep(44100, "ID", "^ID3$", "^ID.?$", "Info", "magic", "\u00ff", [0x49, 0x4
mp3> "64ff65ff66" | hex | bgrep("\u00ff"; "b")
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0|64 ff 65 ff 66| |d.e.f| |.: none 0x0-0x4.7 (5)
mp3> "64ff65ff66" | hex | find("\u00ff"; "b")
mp3> "64ff65ff66" | hex | match("\u00ff"; "b")
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| ff 65 ff 66| | .e.f| |.: none 0x1-0x4.7 (4)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| ff 66| | .f| |.: none 0x3-0x4.7 (2)
mp3> "aöaöa" | find("ö")
mp3> "aöaöa" | tobytes | match("ö")
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: none 0x1-0x6.7 (6)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| c3 b6 61| | ..a| |.: none 0x4-0x6.7 (3)
mp3> "aöaöa" | find("\u00c3"; "b")
mp3> "aöaöa" | tobytes | match("\u00c3"; "b")
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: none 0x1-0x6.7 (6)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| c3 b6 61| | ..a| |.: none 0x4-0x6.7 (3)
mp3> "aöaöa" | find([0xc3])
mp3> "aöaöa" | tobytes | match([0xc3])
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
0x0| c3 b6 61 c3 b6 61| | ..a..a| |.: none 0x1-0x6.7 (6)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
Expand Down

0 comments on commit eedfd16

Please sign in to comment.