Skip to content

Commit 93dedfc

Browse files
committed
Implement scan/2 filter
1 parent 13d1930 commit 93dedfc

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/builtin.jq

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ def capture($val): ($val|type) as $vt | if $vt == "string" then capture($val; nu
9090
elif $vt == "array" and ($val | length) > 1 then capture($val[0]; $val[1])
9191
elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null)
9292
else error( $vt + " not a string or array") end;
93-
def scan(re):
94-
match(re; "g")
95-
| if (.captures|length > 0)
93+
def scan($re; $flags):
94+
match($re; "g" + $flags)
95+
| if (.captures|length > 0)
9696
then [ .captures | .[] | .string ]
9797
else .string
98-
end ;
98+
end;
99+
def scan($re): scan($re; null);
99100
#
100101
# If input is an array, then emit a stream of successive subarrays of length n (or less),
101102
# and similarly for strings.

tests/onig.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ gsub( "(.*)"; ""; "x")
8787
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
8888
[[["a,b:c, d, e,f"],["a,b:c:d:e,f"],[", ",", ",", "]],[[":a,b, c, d, e,f, "],[":a,b:c:d:e,f:"],[", ",", ",", ",", ",", "]]]
8989

90+
[.[] | scan("b+"; "i")]
91+
["","bBb","abcABBBCabbbc"]
92+
["bBb","b","BBB","bbb"]
93+
9094
# reference to named captures
9195
gsub("(?<x>.)[^a]*"; "+\(.x)-")
9296
"Abcabc"

0 commit comments

Comments
 (0)