Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rindex yields wrong results on overlapped occurrence #2433

Closed
itchyny opened this issue May 28, 2022 · 1 comment · Fixed by #2718
Closed

rindex yields wrong results on overlapped occurrence #2433

itchyny opened this issue May 28, 2022 · 1 comment · Fixed by #2718
Labels
Milestone

Comments

@itchyny
Copy link
Contributor

itchyny commented May 28, 2022

Describe the bug
s | rindex(t) yields wrong results when t appears in s with overlapped occurrence.

To Reproduce

 % jq -n '"ababa" | rindex("aba")'                      
0

Expected behavior
Considering rindex should yield the last occurrence index, the result should be 2 since "ababa"[2:] is "aba".

Environment (please complete the following information):

  • OS and Version: macOS 12.4
  • jq version: jq-1.6-159-gcff5336 (cff5336)

Additional context
The filter works well with overlapped occurrence in arrays.

 % jq -n '["a","b","a","b","a"] | rindex(["a","b","a"])'
2

This issue is an instance of the inconsistent behavior of indices.

 % jq -n '"abababa" | indices("aba")'
[
  0,
  4
]
 % jq -n '["a","b","a","b","a","b","a"] | indices(["a","b","a"])'
[
  0,
  2,
  4
]
@pkoppstein
Copy link
Contributor

@emanuele6 - Any chance of fixing the def ofindex/1 while you're at it?

The line in builtin.jq says it all: def index($i): indices($i) | .[0]; # TODO: optimize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants