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

Change range semantic to behaves more like rust. #12890

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

WindSoilder
Copy link
Collaborator

@WindSoilder WindSoilder commented May 17, 2024

Description

We have decided to make range to behave like rust, that is:

> 1..3
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
╰───┴───╯
> 1..=3
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯
> 1..<3
Error: nu::shell::external_command

  × External command failed
   ╭─[entry #2:1:1]
 1 │ 1..<3
   · ──┬──
   ·   ╰── executable was not found
   ╰────
  help: No such file or directory (os error 2)

This pr also:

  1. removes 1..<3 syntax. The main change is happened in crates/nu-parser/src/parser.rs:parse_range
  2. change range semantic in these commands due to changes in crates/nu-cmd-base/src/util.rs
  • detect_columns (for -c flag)
  • str substring
  • str index-of (for -r flag)
  • bytes at
    Take str substring as example:

Before

❯ "abcd" | str substring 1..=3
bc

After

❯ "abcd" | str substring 1..=3
bcd

The result of "abcd" | str substring 1..3 remains unchanged because the semantic of 1..3 is changed from [1,2,3] to [1,2], so it returns "bc".

Apart from changes in crates/nu-parser/src/parser.rs:parse_range and crates/nu-cmd-base/src/util.rs, remaining changes are most about adjusting test code to follow new semantic

Fixes: #7761

User-Facing Changes

It's a big breaking change, because the semantic of 1..3 is changed from [1,2,3] to [1,2]

Tests + Formatting

Adjusted all tests

After Submitting

Need to update documentation about ranges

@WindSoilder WindSoilder added pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes pr:release-note-mention Addition/Improvement to be mentioned in the release notes pr:language This PR makes some language changes labels May 17, 2024
@WindSoilder WindSoilder added the pr:commands This PR changes our commands in some way label May 17, 2024
@WindSoilder WindSoilder marked this pull request as draft May 17, 2024 13:50
@WindSoilder
Copy link
Collaborator Author

Make as draft because the decision is not finally made

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes pr:commands This PR changes our commands in some way pr:language This PR makes some language changes pr:release-note-mention Addition/Improvement to be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ranges and str substring are inconsistent (off by 1)
1 participant