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

Index range expansion treats ending index 0 as 1 #8213

Closed
kidonng opened this issue Aug 8, 2021 · 4 comments
Closed

Index range expansion treats ending index 0 as 1 #8213

kidonng opened this issue Aug 8, 2021 · 4 comments
Labels
bug Something that's not working as intended
Milestone

Comments

@kidonng
Copy link
Contributor

kidonng commented Aug 8, 2021

fish --version: 3.3.1

Quoting the docs:

List indices start at 1 in fish, not 0 like in other languages.

An invalid index is silently ignored resulting in no value (not even an empty string, just no argument at all).

However, it seems index range expansion treats ending index 0 as 1:

$ set -S LESS
$LESS: set in universal scope, exported, with 4 elements
$LESS[1]: |-R|
$LESS[2]: |-i|
$LESS[3]: |-M|
$LESS[4]: |--incsearch|

$ echo $LESS[0]
fish: array indices start at 1, not 0.
echo $LESS[0]
           ^

$ echo $LESS[2..0]
-i -R

$ echo $LESS[2..1]
-i -R

This behavior is, at least for me, unexpected and seems in contrary to what the docs says (it is
an invalid index but doesn't result in no value).

Or in other words, why can the index end with 0 if it can't start at 0?

@kopischke
Copy link

kopischke commented Aug 8, 2021

I am sorry to be that guy, but actually, your example seems to work as specified. To my understanding, $LESS[2..0] means “all the indexes in reverse order down from 2 to 0“, which includes index 1 and thus outputs the values for the valid index values 2 and 1, then silently drops the invalid index 0.

Unlike the $LESS[0] example, where nothing but the invalid 0 index has been given and hence the error message helps understanding why there is no output, I am not sure there would be more value in refusing to output the valid values in the $LESS[2..0] range just because it ends with the invalid 0 index.

@kidonng
Copy link
Contributor Author

kidonng commented Aug 8, 2021

There's also this:

$ echo $LESS[0..2]
fish: array indices start at 1, not 0.
echo $LESS[0..2]
           ^

$ echo $LESS[-0..2]
-R -i

@kopischke
Copy link

kopischke commented Aug 8, 2021

Well, there is no such thing as a negative zero, just zero, so this is apparently interpreted as $LESS[0..2], a sequence to which my above observations apply. You could make an argument that the minus denotes user intent to count from the range end, I suppose, in which case it should output the last two values instead of the first two, but that has nothing to do with 0 being a valid index.

@kopischke
Copy link

Ah, I see I have missed your first example. That is an inconsistency, right enough; fish should either process all ranges still valid after dropping the 0 index, or error out on all ranges including a 0 index.

@ridiculousfish ridiculousfish added this to the fish-future milestone Aug 9, 2021
@ridiculousfish ridiculousfish added the bug Something that's not working as intended label Aug 9, 2021
@mqudsi mqudsi closed this as completed in 5326462 Aug 15, 2021
@zanchey zanchey modified the milestones: fish-future, fish 3.4.0 Aug 16, 2021
zanchey added a commit that referenced this issue Aug 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something that's not working as intended
Projects
None yet
Development

No branches or pull requests

4 participants