Skip to content

Commit

Permalink
Refactor splits/2 using foreach syntax (remove private filter _nwise)
Browse files Browse the repository at this point in the history
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 jqlang#3148.
  • Loading branch information
itchyny committed Feb 16, 2025
1 parent b86ff49 commit 66e26e0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
3 changes: 3 additions & 0 deletions docs/content/manual/dev/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,9 @@ sections:
- program: 'splits(", *")'
input: '"ab,cd, ef, gh"'
output: ['"ab"','"cd"','"ef"','"gh"']
- program: 'splits(",? *"; "n")'
input: '"ab,cd ef, gh"'
output: ['"ab"','"cd"','"ef"','"gh"']

- title: "`sub(regex; tostring)`, `sub(regex; tostring; flags)`"
body: |
Expand Down
6 changes: 5 additions & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 10 additions & 20 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,16 @@ def scan($re; $flags):
else .string
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.
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def _nwise(a; $n): a | _nwise($n);
#

# splits/1 produces a stream; split/1 is retained for backward compatibility.
def splits($re; flags): . as $s
# # multiple occurrences of "g" are acceptable
| [ match($re; "g" + flags) | (.offset, .offset + .length) ]
| [0] + . +[$s|length]
| _nwise(2)
| $s[.[0]:.[1] ] ;
def splits($re; $flags):
.[foreach (match($re; $flags+"g"), null) as {$offset, $length}
(null; {start: .next, end: $offset, next: ($offset+$length)})];
def splits($re): splits($re; null);
#

# split emits an array for backward compatibility
def split($re; flags): [ splits($re; flags) ];
#
def split($re; $flags): [ splits($re; $flags) ];

# If s contains capture variables, then create a capture object and pipe it to s, bearing
# in mind that s could be a stream
def sub($re; s; $flags):
Expand All @@ -133,12 +123,12 @@ def sub($re; s; $flags):
| .previous = ($edit | .offset + .length ) )
| .result[] + $in[.previous:] )
// $in;
#

def sub($re; s): sub($re; s; "");
#

def gsub($re; s; flags): sub($re; s; flags + "g");
def gsub($re; s): sub($re; s; "g");
#

########################################################################
# generic iterator/generator
def while(cond; update):
Expand Down
7 changes: 7 additions & 0 deletions tests/manonig.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion tests/onig.test
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,20 @@ sub("(?<x>.)"; "\(.x)!")
"aB"
["AB","ab","cc"]

# splits and _nwise
# splits
[splits("")]
"ab"
["","a","b",""]

[splits("c")]
"ab"
["ab"]

[splits("a+"; "i")]
"abAABBabA"
["","b","BB","b",""]

[splits("b+"; "i")]
"abAABBabA"
["a","AA","a","A"]

0 comments on commit 66e26e0

Please sign in to comment.