mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
fix range bugs in str substring
, str index-of
, slice
, bytes at
(#14863)
- fixes #14769 # Description ## Bugs - `str substring 0..<0` When passed a range containing no elements, for non-zero cases `str substring` behaves correctly: ```nushell ("hello world" | str substring 1..<1) == "" # => true ``` but if the range is `0..<0`, it returns the whole string instead ```nushell "hello world" | str substring 0..<0 # => hello world ``` - `[0 1 2] | range 0..<0` Similar behavior to `str substring` - `str index-of` - off-by-one on end bounds - underflow on negative start bounds - `bytes at` has inconsistent behavior, works correctly when the size is known, returns one byte less when it's not known (streaming) This can be demonstrated by comparing the outputs of following snippets ```nushell "hello world" | into binary | bytes at ..<5 | decode # => hello "hello world" | into binary | chunks 1 | bytes collect | bytes at ..<5 | decode # => hell ``` - `bytes at` panics on decreasing (`5..3`) ranges if the input size is known. Does not panic with streaming input. ## Changes - implement `FromValue` for `IntRange`, as it is very common to use integer ranges as arguments - `IntRange::absolute_start` can now point one-past-end - `IntRange::absolute_end` converts relative `Included` bounds to absolute `Excluded` bounds - `IntRange::absolute_bounds` is a convenience method that calls the other `absolute_*` methods and transforms reverse ranges to empty at `start` (`5..3` => `5..<5`) - refactored `str substring` tests to allow empty exclusive range tests - fix the `0..<0` case for `str substring` and `str index-of` - `IntRange::distance` never returns `Included(0)` As a general rule `Included(n) == Excluded(n + 1)`. This makes returning `Included(0)` bug prone as users of the function will likely rely on this general rule and cause bugs. - `ByteStream::slice` no longer has an off-by-one on inputs without a known size. This affected `bytes at`. - `bytes at` no longer panics on reverse ranges - `bytes at` is now consistent between streaming and non streaming inputs. # User-Facing Changes There should be no noticeable changes other than the bugfix. # Tests + Formatting - 🟢 toolkit fmt - 🟢 toolkit clippy - 🟢 toolkit test - 🟢 toolkit test stdlib # After Submitting N/A
This commit is contained in:
@ -15,7 +15,12 @@ pub fn returns_error_for_relative_range_on_infinite_stream() {
|
||||
pub fn returns_bytes_for_fixed_range_on_infinite_stream_including_end() {
|
||||
let actual = nu!("nu --testbin iecho 3 | bytes at ..10 | decode");
|
||||
assert_eq!(
|
||||
actual.out, "33333",
|
||||
actual.out, "333333",
|
||||
"Expected bytes from index 0 to 10, but got different output"
|
||||
);
|
||||
let actual = nu!("nu --testbin iecho 3 | bytes at ..10 | decode");
|
||||
assert_eq!(
|
||||
actual.out, "333333",
|
||||
"Expected bytes from index 0 to 10, but got different output"
|
||||
);
|
||||
}
|
||||
@ -24,7 +29,7 @@ pub fn returns_bytes_for_fixed_range_on_infinite_stream_including_end() {
|
||||
pub fn returns_bytes_for_fixed_range_on_infinite_stream_excluding_end() {
|
||||
let actual = nu!("nu --testbin iecho 3 | bytes at ..<9 | decode");
|
||||
assert_eq!(
|
||||
actual.out, "3333",
|
||||
actual.out, "33333",
|
||||
"Expected bytes from index 0 to 8, but got different output"
|
||||
);
|
||||
}
|
||||
|
@ -66,3 +66,17 @@ fn negative_indices() {
|
||||
assert_eq!(actual.out, "1");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_to_zero_exclusive() {
|
||||
let actual = nu!(r#"[0 1 2 3] | slice 0..<0 | to nuon"#);
|
||||
|
||||
assert_eq!(actual.out, "[]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_negative_one_inclusive() {
|
||||
let actual = nu!(r#"[0 1 2 3] | slice 2..-1 | to nuon"#);
|
||||
|
||||
assert_eq!(actual.out, "[2, 3]");
|
||||
}
|
||||
|
Reference in New Issue
Block a user