Skip to content

Commit

Permalink
implement scan/2 function
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Aug 15, 2019
1 parent 37b2d21 commit b41acdd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ def capture($val): ($val|type) as $vt | if $vt == "string" then capture($val; nu
elif $vt == "array" and ($val | length) > 1 then capture($val[0]; $val[1])
elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null)
else error( $vt + " not a string or array") end;
def scan(re):
match(re; "g")
| if (.captures|length > 0)
def scan($re; $flags):
match($re; "g" + $flags)
| if (.captures|length > 0)
then [ .captures | .[] | .string ]
else .string
end ;
end;
def scan($re): scan($re; null);
#
# If input is an array, then emit a stream of successive subarrays of length n (or less),
# and similarly for strings.
Expand Down
4 changes: 4 additions & 0 deletions tests/onig.test
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ gsub( "(.*)"; ""; "x")
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
[[["a,b:c, d, e,f"],["a,b:c:d:e,f"],[", ",", ",", "]],[[":a,b, c, d, e,f, "],[":a,b:c:d:e,f:"],[", ",", ",", ",", ",", "]]]

[.[] | scan("b+"; "i")]
["","bBb","abcABBBCabbbc"]
["bBb","b","BBB","bbb"]

# reference to named captures
gsub("(?<x>.)[^a]*"; "+\(.x)-")
"Abcabc"
Expand Down

0 comments on commit b41acdd

Please sign in to comment.