Skip to content
Myr

Implement scan/2 filter (#1961)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by itchyny · committed by GitHub · parent 83f375c · Browse files · 2 files+9 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+9 added−4 removed

  • tests:+4 added−0 removed
  • other:+5 added−4 removed

2 files+9 added−4 removed

modified:src/builtin.jq+5 added-4 removed
88 unmodified lines
89def capture($val): ($val|type) as $vt | if $vt == "string" then capture($val; null)90 elif $vt == "array" and ($val | length) > 1 then capture($val[0]; $val[1])91 elif $vt == "array" and ($val | length) > 0 then capture($val[0]; null)92 else error( $vt + " not a string or array") end;93def scan(re):94 match(re; "g")95 | if (.captures|length > 0)93def scan($re; $flags):94 match($re; "g" + $flags)95 | if (.captures|length > 0)96 then [ .captures | .[] | .string ]97 else .string98 end ;98 end;99def scan($re): scan($re; null);100#101# If input is an array, then emit a stream of successive subarrays of length n (or less),102# and similarly for strings.103def _nwise($n):
171 unmodified lines
modified:tests/onig.test+4 added
124 unmodified lines
125[.[]|[[sub(", +";":")], [gsub(", +";":")], [scan(", +")]]]126["a,b, c, d, e,f",", a,b, c, d, e,f, "]127[[["a,b:c, d, e,f"],["a,b:c:d:e,f"],[", ",", ",", "]],[[":a,b, c, d, e,f, "],[":a,b:c:d:e,f:"],[", ",", ",", ",", ",", "]]]128129[.[] | scan("b+"; "i")]130["","bBb","abcABBBCabbbc"]131["bBb","b","BBB","bbb"]132133# reference to named captures134gsub("(?<x>.)[^a]*"; "+\(.x)-")135"Abcabc"136"+A-+a-"
31 unmodified lines