Commit Graph
10 Commits
Author SHA1 Message Date
WinkleMad f1f39b42e3 Advance past the whole codepoint after a zero-width regex match (#3586) 2026-07-19 21:13:51 +09:00
itchyny b088e83a82 Refactor splits/2 using foreach syntax (remove private filter _nwise) (#3260)
This commit refactors `splits/2` implementation using `foreach` syntax
and reduces intermediate arrays. This refactoring removes an unnecessary
private (and undocumented) filter `_nwise` and closes #3148.
2025-05-18 08:27:44 +09:00
Mattias Wadman 2755664a8e Non-matched optional capture group should be null and offset -1 (#3238)
Fixes #3093
2025-02-04 19:18:58 +01:00
Emanuele Torre 01dfd8b86d Populate captures also for zero-width matches
Instead of just using {"captures":[]}.

sub functions are use captures for replacement expressions.
If we don't populate, captures for empty matches, the replacement
expression is run with an empty object as input instead of an object
containing the named captures with "" as value:

* before:

  $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")'
  "ZfooZbarZnull"

* after:

  $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")'
  "ZfooZbarZ"

---

I also removed a redundant

  result = NULL;
  if (result) {
    ...
  }
2023-07-18 12:12:50 -05:00
itchyny 600e602548 Fix empty regular expression matches (fix #2565) 2023-07-09 12:51:53 -05:00
itchyny 4dc2a2fb9d Implement scan/2 filter (#1961) 2023-07-04 07:48:29 +09:00
pkoppstein 83f375cc83 Revamp sub/3 to resolve most issues with gsub (and sub with "g") (#2641)
The primary purpose of this commit is to rectify most problems with
`gsub` (and also `sub` with the `g` option), in particular fix #1425 ('\b'),
fix #2354 (lookahead), and fix #2532 (regex == `"^(?!cd ).*$|^cd "`).

This commit also partly resolves #2148 and resolves #1206 in that
`gsub` no longer loops infinitely; however, because the new `gsub`
depends critically on `match/2`, the behavior when regex == `""` is
sometimes non-standard.

The documentation has been updated to reflect the fact that `sub`
and `gsub` are intended to be regular in the second argument.

Also, `_nwise/1` has been tweaked to take advantage of TCO.
2023-07-04 07:46:29 +09:00
pkoppstein ad8d1a8cc8 Two sub bugs fixed; two new gsub tests have been added.
./jq --run-tests < tests/onig.test
has been run successfully:

...
Testing 'gsub("a";"b")' at line number 70
Testing 'gsub( "(.*)"; "";  "x")' at line number 74
...
22 of 22 tests passed (0 malformed)
2015-12-09 15:28:01 -06:00
David Tolnay 6c3934de15 Fix decoding of UTF-8 sequence length (fix #922) 2015-08-22 12:31:36 -07:00
David Tolnay 1146b8b84a separate jq, oniguruma, sh, and man tests 2015-06-18 23:55:43 -05:00