Skip to content

Commit

Permalink
builtin.c: fix zero-width bug, so uniq/1 is no longer needed
Browse files Browse the repository at this point in the history
builtin.c: f_match: Zero-width match : ensure '"qux" | match("(?=u)"; "g")' matches just once

rm uniq/1 as it is no longer needed

In manual.yml, replace nonsensical sub/1 example
  • Loading branch information
pkoppstein committed Jul 2, 2023
1 parent 0d6a400 commit d8cd39e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
31 changes: 4 additions & 27 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1428,30 +1428,6 @@ sections:
input: '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]'
output: ['{"foo":2, "bar":3}']

- title: "`uniq(stream)`"
body: |
The `uniq` function produces a substream of the given stream
by emitting in turn the first item from each run within it.
No sorting takes place.
examples:
- program: '[uniq(1,1,2,null,null,1)]'
input: 'null'
output: ['[1,2,null,1]']

- program: '[uniq(.[])]'
input: '[1,1,2,null,null,1]'
output: ['[1,2,null,1]']

- program: '[uniq(empty)]'
input: 'null'
output: ['[]']

- program: '[true, false | [uniq(1,1,2)]]'
input: null
output: ['[[1,2],[1,2]]']

- title: "`unique`, `unique_by(path_exp)`"
body: |
Expand Down Expand Up @@ -2508,9 +2484,10 @@ sections:
would take the form: `"\(.x)"`.
example:
- program: 'sub("^[^a-z]*(?<x>[a-z]*).*")'
input: '"123abc456"'
output: '"ZabcZabc"'

- program: 'sub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)"; "g")'
input: '"123abc456def"'
output: ['"ZabcZdef"']

- program: '[sub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)")]'
input: '"aB"'
Expand Down
3 changes: 2 additions & 1 deletion src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,8 @@ static jv f_match(jq_state *jq, jv input, jv regex, jv modifiers, jv testmode) {
match = jv_object_set(match, jv_string("string"), jv_string(""));
match = jv_object_set(match, jv_string("captures"), jv_array());
result = jv_array_append(result, match);
start += 1;
// ensure '"qux" | match("(?=u)"; "g")' matches just once
start = (const UChar*)(input_string+region->end[0]+1);
continue;
}

Expand Down
10 changes: 1 addition & 9 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,11 @@ def splits($re): splits($re; null);
# split emits an array for backward compatibility
def split($re; flags): [ splits($re; flags) ];
#
# stream-oriented
def uniq(s):
foreach s as $x (null;
if . and $x == .[0] then .[1] = false
else [$x, true]
end;
if .[1] then .[0] else empty end);
#
# 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):
. as $in
| (reduce uniq(match($re; $flags)) as $edit
| (reduce match($re; $flags) as $edit
({result: [], previous: 0};
$in[ .previous: ($edit | .offset) ] as $gap
# create the "capture" objects (one per item in s)
Expand Down
7 changes: 0 additions & 7 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1732,10 +1732,3 @@ false
1
1

[uniq(1,1,2,3,3,4)]
null
[1,2,3,4]

[uniq(empty)]
null
[]
54 changes: 42 additions & 12 deletions tests/man.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
"Hello, world!"
"Hello, world!"

. | tojson
12345678909876543212345
"12345678909876543212345"

map([., . == 1]) | tojson
[1, 1.000, 1.0, 100e-2]
"[[1,true],[1.000,true],[1.0,true],[1.00,true]]"

. as $big | [$big, $big + 1] | map(. > 10000000000000000000000000000000)
10000000000000000000000000000001
[true, false]

.foo
{"foo": 42, "bar": "less interesting data"}
42
Expand Down Expand Up @@ -163,11 +175,12 @@ null
-1

.[] | length
[[1,2], "string", {"a":2}, null]
[[1,2], "string", {"a":2}, null, -5]
2
6
1
0
5

utf8bytelength
"\u03bc"
Expand Down Expand Up @@ -343,28 +356,28 @@ flatten
[{"foo": "bar"}, [{"foo": "baz"}]]
[{"foo": "bar"}, {"foo": "baz"}]

range(2;4)
range(2; 4)
null
2
3

[range(2;4)]
[range(2; 4)]
null
[2,3]

[range(4)]
null
[0,1,2,3]

[range(0;10;3)]
[range(0; 10; 3)]
null
[0,3,6,9]

[range(0;10;-1)]
[range(0; 10; -1)]
null
[]

[range(0;-5;-1)]
[range(0; -5; -1)]
null
[0,-1,-2,-3,-4]

Expand Down Expand Up @@ -551,6 +564,10 @@ join(" ")
1
[1,2,4,8,16,32,64]

[repeat(.*2, error)?]
1
[2]

[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]
4
24
Expand Down Expand Up @@ -658,7 +675,7 @@ true
false
false

if . == 0 then "zero" elif . == 1 then "one" else "many" end
if . == 0 then "zero" elif . == 1 then "one" else "many" end
2
"many"

Expand Down Expand Up @@ -841,8 +858,21 @@ true
{"foo": 42}
{"foo": 43}

.[]|tonumber?
["1", "hello", "3", 4]
1
3
4
.a = .b
{"a": {"b": 10}, "b": 20}
{"a":20,"b":20}

.a |= .b
{"a": {"b": 10}, "b": 20}
{"a":10,"b":20}

(.a, .b) = range(3)
null
{"a":0,"b":0}
{"a":1,"b":1}
{"a":2,"b":2}

(.a, .b) |= range(3)
null
{"a":0,"b":0}

0 comments on commit d8cd39e

Please sign in to comment.