Skip to content

Commit 95ed68f

Browse files
committed
Refactor splits/2 using foreach syntax (remove private filter _nwise)
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.
1 parent b86ff49 commit 95ed68f

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

Diff for: docs/content/manual/dev/manual.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,9 @@ sections:
27362736
- program: 'splits(", *")'
27372737
input: '"ab,cd, ef, gh"'
27382738
output: ['"ab"','"cd"','"ef"','"gh"']
2739+
- program: 'splits(",? *"; "n")'
2740+
input: '"ab,cd ef, gh"'
2741+
output: ['"ab"','"cd"','"ef"','"gh"']
27392742

27402743
- title: "`sub(regex; tostring)`, `sub(regex; tostring; flags)`"
27412744
body: |

Diff for: jq.1.prebuilt

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/builtin.jq

+10-20
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,16 @@ def scan($re; $flags):
9999
else .string
100100
end;
101101
def scan($re): scan($re; null);
102-
#
103-
# If input is an array, then emit a stream of successive subarrays of length n (or less),
104-
# and similarly for strings.
105-
def _nwise($n):
106-
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
107-
n;
108-
def _nwise(a; $n): a | _nwise($n);
109-
#
102+
110103
# splits/1 produces a stream; split/1 is retained for backward compatibility.
111-
def splits($re; flags): . as $s
112-
# # multiple occurrences of "g" are acceptable
113-
| [ match($re; "g" + flags) | (.offset, .offset + .length) ]
114-
| [0] + . +[$s|length]
115-
| _nwise(2)
116-
| $s[.[0]:.[1] ] ;
104+
def splits($re; $flags):
105+
.[foreach (match($re; $flags+"g"), null) as {$offset, $length}
106+
(null; {start: .last, end: $offset, last: ($offset+$length)})];
117107
def splits($re): splits($re; null);
118-
#
108+
119109
# split emits an array for backward compatibility
120-
def split($re; flags): [ splits($re; flags) ];
121-
#
110+
def split($re; $flags): [ splits($re; $flags) ];
111+
122112
# If s contains capture variables, then create a capture object and pipe it to s, bearing
123113
# in mind that s could be a stream
124114
def sub($re; s; $flags):
@@ -133,12 +123,12 @@ def sub($re; s; $flags):
133123
| .previous = ($edit | .offset + .length ) )
134124
| .result[] + $in[.previous:] )
135125
// $in;
136-
#
126+
137127
def sub($re; s): sub($re; s; "");
138-
#
128+
139129
def gsub($re; s; flags): sub($re; s; flags + "g");
140130
def gsub($re; s): sub($re; s; "g");
141-
#
131+
142132
########################################################################
143133
# generic iterator/generator
144134
def while(cond; update):

Diff for: tests/manonig.test

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/onig.test

+13-1
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,20 @@ sub("(?<x>.)"; "\(.x)!")
192192
"aB"
193193
["AB","ab","cc"]
194194

195-
# splits and _nwise
195+
# splits
196196
[splits("")]
197197
"ab"
198198
["","a","b",""]
199199

200+
[splits("c")]
201+
"ab"
202+
["ab"]
203+
204+
[splits("a+"; "i")]
205+
"abAABBabA"
206+
["","b","BB","b",""]
207+
208+
[splits("b+"; "i")]
209+
"abAABBabA"
210+
["a","AA","a","A"]
211+

0 commit comments

Comments
 (0)