2021-10-09 04:45:25 +02:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
2021-10-25 18:58:58 +02:00
|
|
|
engine::{Command, EngineState, Stack},
|
2022-11-09 22:55:05 +01:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
|
|
|
|
Value,
|
2021-10-09 04:45:25 +02:00
|
|
|
};
|
2023-04-07 13:46:11 +02:00
|
|
|
use regex::Regex;
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-10-09 04:45:25 +02:00
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split row"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("split row")
|
2023-07-14 05:20:35 +02:00
|
|
|
.input_output_types(vec![
|
|
|
|
(Type::String, Type::List(Box::new(Type::String))),
|
2023-07-27 21:32:25 +02:00
|
|
|
(
|
|
|
|
Type::List(Box::new(Type::String)),
|
|
|
|
(Type::List(Box::new(Type::String))),
|
|
|
|
),
|
2023-07-14 05:20:35 +02:00
|
|
|
])
|
|
|
|
.allow_variants_without_examples(true)
|
2021-11-17 05:22:37 +01:00
|
|
|
.required(
|
|
|
|
"separator",
|
|
|
|
SyntaxShape::String,
|
2023-04-07 13:46:11 +02:00
|
|
|
"a character or regex that denotes what separates rows",
|
2021-11-17 05:22:37 +01:00
|
|
|
)
|
2022-05-06 17:53:02 +02:00
|
|
|
.named(
|
|
|
|
"number",
|
|
|
|
SyntaxShape::Int,
|
|
|
|
"Split into maximum number of items",
|
|
|
|
Some('n'),
|
|
|
|
)
|
2023-04-07 13:46:11 +02:00
|
|
|
.switch("regex", "use regex syntax for separator", Some('r'))
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::Strings)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-03-01 06:33:02 +01:00
|
|
|
"Split a string into multiple rows using a separator."
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 15:47:09 +02:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2023-06-11 00:02:00 +02:00
|
|
|
vec!["separate", "divide", "regex"]
|
2022-06-06 15:47:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 04:45:25 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-09 04:45:25 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
2023-02-05 22:17:46 +01:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-10-25 08:31:39 +02:00
|
|
|
split_row(engine_state, stack, call, input)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
2022-02-19 16:24:48 +01:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Split a string into rows of char",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "'abc' | split row ''",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
make split row works like python and rust ways (#7413)
# Description
Fixes: #7389
Make split row works more like python or rust, especially, when the
input string stars/ends with separator, append a empty string to result.
Here are examples:
python:
```python
In [6]: "\nasdf\nghi\n".split("\n")
Out[6]: ['', 'asdf', 'ghi', '']
```
rust:
```rust
fn main() {
let x = "\nabc\ndef\n";
let y = x.split("\n").collect::<Vec<&str>>();
println!("{:?}", y); // outputs: ["", "abc", "def", ""]
}
```
# User-Facing Changes
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 16:21:53 +01:00
|
|
|
Value::test_string(""),
|
2022-02-19 16:24:48 +01:00
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
make split row works like python and rust ways (#7413)
# Description
Fixes: #7389
Make split row works more like python or rust, especially, when the
input string stars/ends with separator, append a empty string to result.
Here are examples:
python:
```python
In [6]: "\nasdf\nghi\n".split("\n")
Out[6]: ['', 'asdf', 'ghi', '']
```
rust:
```rust
fn main() {
let x = "\nabc\ndef\n";
let y = x.split("\n").collect::<Vec<&str>>();
println!("{:?}", y); // outputs: ["", "abc", "def", ""]
}
```
# User-Facing Changes
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 16:21:53 +01:00
|
|
|
Value::test_string(""),
|
2022-02-19 16:24:48 +01:00
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2022-02-19 16:24:48 +01:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Split a string into rows by the specified separator",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "'a--b--c' | split row '--'",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2022-02-19 16:24:48 +01:00
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2022-02-19 16:24:48 +01:00
|
|
|
},
|
make split row works like python and rust ways (#7413)
# Description
Fixes: #7389
Make split row works more like python or rust, especially, when the
input string stars/ends with separator, append a empty string to result.
Here are examples:
python:
```python
In [6]: "\nasdf\nghi\n".split("\n")
Out[6]: ['', 'asdf', 'ghi', '']
```
rust:
```rust
fn main() {
let x = "\nabc\ndef\n";
let y = x.split("\n").collect::<Vec<&str>>();
println!("{:?}", y); // outputs: ["", "abc", "def", ""]
}
```
# User-Facing Changes
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 16:21:53 +01:00
|
|
|
Example {
|
|
|
|
description: "Split a string by '-'",
|
2022-12-16 17:51:00 +01:00
|
|
|
example: "'-a-b-c-' | split row '-'",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
make split row works like python and rust ways (#7413)
# Description
Fixes: #7389
Make split row works more like python or rust, especially, when the
input string stars/ends with separator, append a empty string to result.
Here are examples:
python:
```python
In [6]: "\nasdf\nghi\n".split("\n")
Out[6]: ['', 'asdf', 'ghi', '']
```
rust:
```rust
fn main() {
let x = "\nabc\ndef\n";
let y = x.split("\n").collect::<Vec<&str>>();
println!("{:?}", y); // outputs: ["", "abc", "def", ""]
}
```
# User-Facing Changes
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 16:21:53 +01:00
|
|
|
Value::test_string(""),
|
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
|
|
|
Value::test_string(""),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
make split row works like python and rust ways (#7413)
# Description
Fixes: #7389
Make split row works more like python or rust, especially, when the
input string stars/ends with separator, append a empty string to result.
Here are examples:
python:
```python
In [6]: "\nasdf\nghi\n".split("\n")
Out[6]: ['', 'asdf', 'ghi', '']
```
rust:
```rust
fn main() {
let x = "\nabc\ndef\n";
let y = x.split("\n").collect::<Vec<&str>>();
println!("{:?}", y); // outputs: ["", "abc", "def", ""]
}
```
# User-Facing Changes
_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-10 16:21:53 +01:00
|
|
|
},
|
2023-04-07 13:46:11 +02:00
|
|
|
Example {
|
|
|
|
description: "Split a string by regex",
|
|
|
|
example: r"'a b c' | split row -r '\s+'",
|
2023-09-03 16:27:29 +02:00
|
|
|
result: Some(Value::list(
|
|
|
|
vec![
|
2023-04-07 13:46:11 +02:00
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
|
|
|
],
|
2023-09-03 16:27:29 +02:00
|
|
|
Span::test_data(),
|
|
|
|
)),
|
2023-04-07 13:46:11 +02:00
|
|
|
},
|
2022-02-19 16:24:48 +01:00
|
|
|
]
|
|
|
|
}
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn split_row(
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-09 04:45:25 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
2023-02-05 22:17:46 +01:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-10-09 04:45:25 +02:00
|
|
|
let name_span = call.head;
|
2021-10-25 08:31:39 +02:00
|
|
|
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
|
2023-04-07 13:46:11 +02:00
|
|
|
let regex = if call.has_flag("regex") {
|
|
|
|
Regex::new(&separator.item)
|
|
|
|
} else {
|
|
|
|
let escaped = regex::escape(&separator.item);
|
|
|
|
Regex::new(&escaped)
|
|
|
|
}
|
|
|
|
.map_err(|err| {
|
|
|
|
ShellError::GenericError(
|
|
|
|
"Error with regular expression".into(),
|
|
|
|
err.to_string(),
|
|
|
|
Some(separator.span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
|
|
|
)
|
|
|
|
})?;
|
2022-05-06 17:53:02 +02:00
|
|
|
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
|
2021-10-28 06:13:10 +02:00
|
|
|
input.flat_map(
|
2023-04-07 13:46:11 +02:00
|
|
|
move |x| split_row_helper(&x, ®ex, max_split, name_span),
|
2021-10-28 06:13:10 +02:00
|
|
|
engine_state.ctrlc.clone(),
|
|
|
|
)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
2023-04-07 13:46:11 +02:00
|
|
|
fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Span) -> Vec<Value> {
|
2023-09-03 16:27:29 +02:00
|
|
|
let span = v.span();
|
2023-08-24 22:48:05 +02:00
|
|
|
match v {
|
2023-09-03 16:27:29 +02:00
|
|
|
Value::Error { error, .. } => {
|
|
|
|
vec![Value::error(*error.clone(), span)]
|
2023-08-24 22:48:05 +02:00
|
|
|
}
|
|
|
|
v => {
|
|
|
|
let v_span = v.span();
|
|
|
|
|
2021-10-11 20:45:31 +02:00
|
|
|
if let Ok(s) = v.as_string() {
|
2022-05-06 17:53:02 +02:00
|
|
|
match max_split {
|
2023-04-07 13:46:11 +02:00
|
|
|
Some(max_split) => regex
|
|
|
|
.splitn(&s, max_split)
|
|
|
|
.map(|x: &str| Value::string(x, v_span))
|
2022-05-06 17:53:02 +02:00
|
|
|
.collect(),
|
2023-04-07 13:46:11 +02:00
|
|
|
None => regex
|
|
|
|
.split(&s)
|
|
|
|
.map(|x: &str| Value::string(x, v_span))
|
2022-05-06 17:53:02 +02:00
|
|
|
.collect(),
|
|
|
|
}
|
2021-10-11 20:45:31 +02:00
|
|
|
} else {
|
2023-09-03 16:27:29 +02:00
|
|
|
vec![Value::error(
|
|
|
|
ShellError::PipelineMismatch {
|
2023-03-01 20:34:48 +01:00
|
|
|
exp_input_type: "string".into(),
|
|
|
|
dst_span: name,
|
|
|
|
src_span: v_span,
|
2023-09-03 16:27:29 +02:00
|
|
|
},
|
|
|
|
name,
|
|
|
|
)]
|
2021-10-11 20:45:31 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2021-10-09 04:45:25 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
2021-10-09 04:45:25 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
test_examples(SubCommand {})
|
|
|
|
}
|
|
|
|
}
|