Fix: string_to_table in ssv.rs now filters comments. (issue #11997) (#12035)

- Fixes #11997

# Description
Fixes the issue that comments are not ignored in SSV formatted data.
![Fix
image](https://github.com/nushell/nushell/assets/64328283/1c1bd7dd-ced8-4276-8c21-b50e1c0dba53)

# User-Facing Changes
If you have a comment in the beginning of SSV formatted data it is now
not included in the SSV table.

# Tests + Formatting
The PR adds one test in the ssv.rs file. All previous test-cases are
still passing. Clippy and Fmt have been ran.
This commit is contained in:
Olilin1 2024-03-01 14:11:13 +01:00 committed by GitHub
parent 262914cf92
commit 39cf43ef06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -239,7 +239,9 @@ fn string_to_table(
aligned_columns: bool, aligned_columns: bool,
split_at: usize, split_at: usize,
) -> Vec<Vec<(String, String)>> { ) -> Vec<Vec<(String, String)>> {
let mut lines = s.lines().filter(|l| !l.trim().is_empty()); let mut lines = s
.lines()
.filter(|l| !l.trim().is_empty() && !l.trim().starts_with('#'));
let separator = " ".repeat(std::cmp::max(split_at, 1)); let separator = " ".repeat(std::cmp::max(split_at, 1));
let (ls, header_options) = if noheaders { let (ls, header_options) = if noheaders {
@ -314,6 +316,24 @@ mod tests {
(String::from(x), String::from(y)) (String::from(x), String::from(y))
} }
#[test]
fn it_filters_comment_lines() {
let input = r#"
a b
1 2
3 4
#comment line
"#;
let result = string_to_table(input, false, true, 1);
assert_eq!(
result,
vec![
vec![owned("a", "1"), owned("b", "2")],
vec![owned("a", "3"), owned("b", "4")]
]
);
}
#[test] #[test]
fn it_trims_empty_and_whitespace_only_lines() { fn it_trims_empty_and_whitespace_only_lines() {
let input = r#" let input = r#"