summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoritchyny <itchyny@cybozu.co.jp>2023-07-04 07:48:29 +0900
committerGitHub <noreply@github.com>2023-07-04 07:48:29 +0900
commit4dc2a2fb9df94ec45d107b798173c7db9c695a7f (patch)
treedafbfe800bff415ff6b0ed9c6e2eadeb44817132
parent83f375cc831039396167d4d2b5f901f4b33a8707 (diff)
Implement scan/2 filter (#1961)
-rw-r--r--src/builtin.jq9
-rw-r--r--tests/onig.test4
2 files changed, 9 insertions, 4 deletions
diff --git a/src/builtin.jq b/src/builtin.jq
index 09663511..c6085552 100644
--- a/src/builtin.jq
+++ b/src/builtin.jq
@@ -90,12 +90,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.
diff --git a/tests/onig.test b/tests/onig.test
index 805efaba..d49391fc 100644
--- a/tests/onig.test
+++ b/tests/onig.test
@@ -126,6 +126,10 @@ gsub("^.*?a"; "b")
["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"