mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 00:38:23 +01:00
fix range semantic in detect_columns, str substring, str index-of (#12894)
# Description Fixes: https://github.com/nushell/nushell/issues/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
This commit is contained in:
parent
7ede90cba5
commit
ac4125f8ed
@ -25,8 +25,8 @@ pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
|
|||||||
Range::IntRange(range) => {
|
Range::IntRange(range) => {
|
||||||
let start = range.start().try_into().unwrap_or(0);
|
let start = range.start().try_into().unwrap_or(0);
|
||||||
let end = match range.end() {
|
let end = match range.end() {
|
||||||
Bound::Included(v) => v as isize,
|
Bound::Included(v) => (v + 1) as isize,
|
||||||
Bound::Excluded(v) => (v - 1) as isize,
|
Bound::Excluded(v) => v as isize,
|
||||||
Bound::Unbounded => isize::MAX,
|
Bound::Unbounded => isize::MAX,
|
||||||
};
|
};
|
||||||
Ok((start, end))
|
Ok((start, end))
|
||||||
|
@ -405,7 +405,7 @@ mod tests {
|
|||||||
let range = Range::new(
|
let range = Range::new(
|
||||||
Value::int(0, Span::test_data()),
|
Value::int(0, Span::test_data()),
|
||||||
Value::int(1, Span::test_data()),
|
Value::int(1, Span::test_data()),
|
||||||
Value::int(3, Span::test_data()),
|
Value::int(2, Span::test_data()),
|
||||||
RangeInclusion::Inclusive,
|
RangeInclusion::Inclusive,
|
||||||
Span::test_data(),
|
Span::test_data(),
|
||||||
)
|
)
|
||||||
|
@ -70,7 +70,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Get part of a string. Note that the start is included but the end is excluded, and that the first character of a string is index 0."
|
"Get part of a string. Note that the first character of a string is index 0."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
@ -108,12 +108,12 @@ impl Command for SubCommand {
|
|||||||
Example {
|
Example {
|
||||||
description:
|
description:
|
||||||
"Get a substring \"nushell\" from the text \"good nushell\" using a range",
|
"Get a substring \"nushell\" from the text \"good nushell\" using a range",
|
||||||
example: " 'good nushell' | str substring 5..12",
|
example: " 'good nushell' | str substring 5..11",
|
||||||
result: Some(Value::test_string("nushell")),
|
result: Some(Value::test_string("nushell")),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Count indexes and split using grapheme clusters",
|
description: "Count indexes and split using grapheme clusters",
|
||||||
example: " '🇯🇵ほげ ふが ぴよ' | str substring --grapheme-clusters 4..6",
|
example: " '🇯🇵ほげ ふが ぴよ' | str substring --grapheme-clusters 4..5",
|
||||||
result: Some(Value::test_string("ふが")),
|
result: Some(Value::test_string("ふが")),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
@ -31,12 +31,12 @@ fn detect_columns_with_legacy_and_flag_c() {
|
|||||||
(
|
(
|
||||||
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
||||||
"[[c1,c3,c4,c5]; ['a b',c,d,e]]",
|
"[[c1,c3,c4,c5]; ['a b',c,d,e]]",
|
||||||
"0..1",
|
"0..0",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
||||||
"[[c1,c2,c3,c4]; [a,b,c,'d e']]",
|
"[[c1,c2,c3,c4]; [a,b,c,'d e']]",
|
||||||
"(-2)..(-1)",
|
"(-2)..(-2)",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
"$\"c1 c2 c3 c4 c5(char nl)a b c d e\"",
|
||||||
@ -77,7 +77,7 @@ drwxr-xr-x 4 root root 4.0K Mar 20 08:18 ~(char nl)
|
|||||||
['drwxr-xr-x', '4', 'root', 'root', '4.0K', 'Mar 20 08:18', '~'],
|
['drwxr-xr-x', '4', 'root', 'root', '4.0K', 'Mar 20 08:18', '~'],
|
||||||
['-rw-r--r--', '1', 'root', 'root', '3.0K', 'Mar 20 07:23', '~asdf']
|
['-rw-r--r--', '1', 'root', 'root', '3.0K', 'Mar 20 07:23', '~asdf']
|
||||||
]";
|
]";
|
||||||
let range = "5..7";
|
let range = "5..6";
|
||||||
let cmd = format!(
|
let cmd = format!(
|
||||||
"({} | detect columns -c {} -s 1 --no-headers) == {}",
|
"({} | detect columns -c {} -s 1 --no-headers) == {}",
|
||||||
pipeline(body),
|
pipeline(body),
|
||||||
|
@ -269,7 +269,7 @@ fn substring_errors_if_start_index_is_greater_than_end_index() {
|
|||||||
cwd: dirs.test(), pipeline(
|
cwd: dirs.test(), pipeline(
|
||||||
r#"
|
r#"
|
||||||
open sample.toml
|
open sample.toml
|
||||||
| str substring 6..5 fortune.teller.phone
|
| str substring 6..4 fortune.teller.phone
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -342,7 +342,7 @@ fn substrings_the_input_and_treats_start_index_as_zero_if_blank_start_index_give
|
|||||||
cwd: dirs.test(), pipeline(
|
cwd: dirs.test(), pipeline(
|
||||||
r#"
|
r#"
|
||||||
open sample.toml
|
open sample.toml
|
||||||
| str substring ..2 package.name
|
| str substring ..1 package.name
|
||||||
| get package.name
|
| get package.name
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
@ -3,7 +3,7 @@ use crate::repl::tests::{fail_test, run_test, TestResult};
|
|||||||
#[test]
|
#[test]
|
||||||
fn cjk_in_substrings() -> TestResult {
|
fn cjk_in_substrings() -> TestResult {
|
||||||
run_test(
|
run_test(
|
||||||
r#"let s = '[Rust 程序设计语言](title-page.md)'; let start = ($s | str index-of '('); let end = ($s | str index-of ')'); $s | str substring ($start + 1)..($end)"#,
|
r#"let s = '[Rust 程序设计语言](title-page.md)'; let start = ($s | str index-of '('); let end = ($s | str index-of ')'); $s | str substring ($start + 1)..<($end)"#,
|
||||||
"title-page.md",
|
"title-page.md",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user