Skip to content

Commit b088e83

Browse files
authored
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.
1 parent 96d19ca commit b088e83

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 3 additions & 0 deletions
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: |

jq.1.prebuilt

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.jq

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,16 @@ def scan($re; $flags):
9898
else .string
9999
end;
100100
def scan($re): scan($re; null);
101-
#
102-
# If input is an array, then emit a stream of successive subarrays of length n (or less),
103-
# and similarly for strings.
104-
def _nwise($n):
105-
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
106-
n;
107-
def _nwise(a; $n): a | _nwise($n);
108-
#
101+
109102
# splits/1 produces a stream; split/1 is retained for backward compatibility.
110-
def splits($re; flags): . as $s
111-
# # multiple occurrences of "g" are acceptable
112-
| [ match($re; "g" + flags) | (.offset, .offset + .length) ]
113-
| [0] + . +[$s|length]
114-
| _nwise(2)
115-
| $s[.[0]:.[1] ] ;
103+
def splits($re; $flags):
104+
.[foreach (match($re; $flags+"g"), null) as {$offset, $length}
105+
(null; {start: .next, end: $offset, next: ($offset+$length)})];
116106
def splits($re): splits($re; null);
117-
#
107+
118108
# split emits an array for backward compatibility
119-
def split($re; flags): [ splits($re; flags) ];
120-
#
109+
def split($re; $flags): [ splits($re; $flags) ];
110+
121111
# If s contains capture variables, then create a capture object and pipe it to s, bearing
122112
# in mind that s could be a stream
123113
def sub($re; s; $flags):
@@ -132,12 +122,12 @@ def sub($re; s; $flags):
132122
| .previous = ($edit | .offset + .length ) )
133123
| .result[] + $in[.previous:] )
134124
// $in;
135-
#
125+
136126
def sub($re; s): sub($re; s; "");
137-
#
127+
138128
def gsub($re; s; flags): sub($re; s; flags + "g");
139129
def gsub($re; s): sub($re; s; "g");
140-
#
130+
141131
########################################################################
142132
# generic iterator/generator
143133
def while(cond; update):

tests/manonig.test

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/onig.test

Lines changed: 13 additions & 1 deletion
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)