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.
This commit is contained in:
1 parent
96d19ca2ee
commit
b088e83a82
5 files changed
+37
-21
No files matched your search
@@ -2736,6 +2736,9 @@ sections:
|
|||||||
- program: 'splits(", *")'
|
- program: 'splits(", *")'
|
||||||
input: '"ab,cd, ef, gh"'
|
input: '"ab,cd, ef, gh"'
|
||||||
output: ['"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)`"
|
- title: "`sub(regex; tostring)`, `sub(regex; tostring; flags)`"
|
||||||
body: |
|
body: |
|
||||||
|
|||||||
Generated
+4
@@ -3032,6 +3032,10 @@ These provide the same results as their \fBsplit\fR counterparts, but as a strea
|
|||||||
jq \'splits(", *")\'
|
jq \'splits(", *")\'
|
||||||
"ab,cd, ef, gh"
|
"ab,cd, ef, gh"
|
||||||
=> "ab", "cd", "ef", "gh"
|
=> "ab", "cd", "ef", "gh"
|
||||||
|
|
||||||
|
jq \'splits(",? *"; "n")\'
|
||||||
|
"ab,cd ef, gh"
|
||||||
|
=> "ab", "cd", "ef", "gh"
|
||||||
.
|
.
|
||||||
.fi
|
.fi
|
||||||
.
|
.
|
||||||
|
|||||||
+10
-20
@@ -98,26 +98,16 @@ def scan($re; $flags):
|
|||||||
else .string
|
else .string
|
||||||
end;
|
end;
|
||||||
def scan($re): scan($re; null);
|
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.
|
# splits/1 produces a stream; split/1 is retained for backward compatibility.
|
||||||
def splits($re; flags): . as $s
|
def splits($re; $flags):
|
||||||
# # multiple occurrences of "g" are acceptable
|
.[foreach (match($re; $flags+"g"), null) as {$offset, $length}
|
||||||
| [ match($re; "g" + flags) | (.offset, .offset + .length) ]
|
(null; {start: .next, end: $offset, next: ($offset+$length)})];
|
||||||
| [0] + . +[$s|length]
|
|
||||||
| _nwise(2)
|
|
||||||
| $s[.[0]:.[1] ] ;
|
|
||||||
def splits($re): splits($re; null);
|
def splits($re): splits($re; null);
|
||||||
#
|
|
||||||
# split emits an array for backward compatibility
|
# 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
|
# If s contains capture variables, then create a capture object and pipe it to s, bearing
|
||||||
# in mind that s could be a stream
|
# in mind that s could be a stream
|
||||||
def sub($re; s; $flags):
|
def sub($re; s; $flags):
|
||||||
@@ -132,12 +122,12 @@ def sub($re; s; $flags):
|
|||||||
| .previous = ($edit | .offset + .length ) )
|
| .previous = ($edit | .offset + .length ) )
|
||||||
| .result[] + $in[.previous:] )
|
| .result[] + $in[.previous:] )
|
||||||
// $in;
|
// $in;
|
||||||
#
|
|
||||||
def sub($re; s): sub($re; s; "");
|
def sub($re; s): sub($re; s; "");
|
||||||
#
|
|
||||||
def gsub($re; s; flags): sub($re; s; flags + "g");
|
def gsub($re; s; flags): sub($re; s; flags + "g");
|
||||||
def gsub($re; s): sub($re; s; "g");
|
def gsub($re; s): sub($re; s; "g");
|
||||||
#
|
|
||||||
########################################################################
|
########################################################################
|
||||||
# generic iterator/generator
|
# generic iterator/generator
|
||||||
def while(cond; update):
|
def while(cond; update):
|
||||||
|
|||||||
Generated
+7
@@ -64,6 +64,13 @@ splits(", *")
|
|||||||
"ef"
|
"ef"
|
||||||
"gh"
|
"gh"
|
||||||
|
|
||||||
|
splits(",? *"; "n")
|
||||||
|
"ab,cd ef, gh"
|
||||||
|
"ab"
|
||||||
|
"cd"
|
||||||
|
"ef"
|
||||||
|
"gh"
|
||||||
|
|
||||||
sub("[^a-z]*(?<x>[a-z]+)"; "Z\(.x)"; "g")
|
sub("[^a-z]*(?<x>[a-z]+)"; "Z\(.x)"; "g")
|
||||||
"123abc456def"
|
"123abc456def"
|
||||||
"ZabcZdef"
|
"ZabcZdef"
|
||||||
|
|||||||
+13
-1
@@ -192,8 +192,20 @@ sub("(?<x>.)"; "\(.x)!")
|
|||||||
"aB"
|
"aB"
|
||||||
["AB","ab","cc"]
|
["AB","ab","cc"]
|
||||||
|
|
||||||
# splits and _nwise
|
# splits
|
||||||
[splits("")]
|
[splits("")]
|
||||||
"ab"
|
"ab"
|
||||||
["","a","b",""]
|
["","a","b",""]
|
||||||
|
|
||||||
|
[splits("c")]
|
||||||
|
"ab"
|
||||||
|
["ab"]
|
||||||
|
|
||||||
|
[splits("a+"; "i")]
|
||||||
|
"abAABBabA"
|
||||||
|
["","b","BB","b",""]
|
||||||
|
|
||||||
|
[splits("b+"; "i")]
|
||||||
|
"abAABBabA"
|
||||||
|
["a","AA","a","A"]
|
||||||
|
|
||||||
Reference in new issue
Block a user