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

Ranges and str substring are inconsistent (off by 1) #7761

Closed
rgwood opened this issue Jan 15, 2023 · 5 comments · Fixed by #12894 · May be fixed by #12890
Closed

Ranges and str substring are inconsistent (off by 1) #7761

rgwood opened this issue Jan 15, 2023 · 5 comments · Fixed by #12894 · May be fixed by #12890
Labels
inconsistent-behavior Behavior between different commands or types inconsistent/unexpected
Milestone

Comments

@rgwood
Copy link
Contributor

rgwood commented Jan 15, 2023

Describe the bug

1..3 produces 3 numbers:

〉1..3
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯

str substring 1..3 produces 2 characters:

"abcdefg" | str substring 1..3
bc

That's an unpleasant inconsistency.

How to reproduce

Use str substring with a range, observe that it produces 1 fewer character than there are numbers in the range.

Expected behavior

I expect that a range with n numbers should produce a string of n characters when used with str substring.

Screenshots

No response

Configuration

key value
version 0.74.1
branch main
commit_hash b004aac
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.65.0 (897e37553 2022-11-02)
rust_channel 1.65.0-x86_64-pc-windows-msvc
cargo_version cargo 1.65.0 (4bc8f24d3 2022-10-20)
pkg_version 0.74.1
build_time 2023-01-12 12:55:53 -08:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins

Additional context

No response

@sholderbach sholderbach added the inconsistent-behavior Behavior between different commands or types inconsistent/unexpected label Jan 15, 2023
@MehulG
Copy link
Contributor

MehulG commented Jan 16, 2023

Hey, could you assign me this issue? If I understood it correctly, the inconsistency should be resolved by having something like this:

〉"abcdefg" | str substring 1..3
bcd

@webbedspace
Copy link
Contributor

While you're at it, could you make str substring 1..<3 only produce bc like one would expect? Currently it's treated the same as 1..3.

@MehulG
Copy link
Contributor

MehulG commented Jan 16, 2023

yes, definitely

@rgwood
Copy link
Contributor Author

rgwood commented Mar 1, 2023

@MehulG Let us know if you're still interested in this one.

@fdncred
Copy link
Collaborator

fdncred commented May 30, 2023

@MehulG Were you going to work in this?

kubouch pushed a commit that referenced this issue May 22, 2024
…2894)

# Description
Fixes: #7761

It's still unsure if we want to change the `range semantic` itself, but
it's good to keep range semantic consistent between nushell commands.

# User-Facing Changes
### Before
```nushell
❯ "abc" | str substring 1..=2
b
```
### After
```nushell
❯ "abc" | str substring 1..=2
bc
```

# Tests + Formatting
Adjust tests to fit new behavior
@hustcer hustcer added this to the v0.94.0 milestone May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inconsistent-behavior Behavior between different commands or types inconsistent/unexpected
Projects
None yet
6 participants