mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
Merge pull request #2098 from miDeb/sort-trailing-separator
sort: fix tokenization for trailing separators
This commit is contained in:
commit
372d08c341
2 changed files with 30 additions and 13 deletions
|
@ -351,20 +351,18 @@ fn tokenize_default(line: &str) -> Vec<Field> {
|
||||||
|
|
||||||
/// Split between separators. These separators are not included in fields.
|
/// Split between separators. These separators are not included in fields.
|
||||||
fn tokenize_with_separator(line: &str, separator: char) -> Vec<Field> {
|
fn tokenize_with_separator(line: &str, separator: char) -> Vec<Field> {
|
||||||
let mut tokens = vec![0..0];
|
let mut tokens = vec![];
|
||||||
let mut previous_was_separator = false;
|
let separator_indices =
|
||||||
for (idx, char) in line.char_indices() {
|
line.char_indices()
|
||||||
if previous_was_separator {
|
.filter_map(|(i, c)| if c == separator { Some(i) } else { None });
|
||||||
tokens.push(idx..0);
|
let mut start = 0;
|
||||||
}
|
for sep_idx in separator_indices {
|
||||||
if char == separator {
|
tokens.push(start..sep_idx);
|
||||||
tokens.last_mut().unwrap().end = idx;
|
start = sep_idx + 1;
|
||||||
previous_was_separator = true;
|
}
|
||||||
} else {
|
if start < line.len() {
|
||||||
previous_was_separator = false;
|
tokens.push(start..line.len());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
tokens.last_mut().unwrap().end = line.len();
|
|
||||||
tokens
|
tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1383,4 +1381,14 @@ mod tests {
|
||||||
vec![0..0, 1..1, 2..2, 3..9, 10..18,]
|
vec![0..0, 1..1, 2..2, 3..9, 10..18,]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tokenize_fields_trailing_custom_separator() {
|
||||||
|
let line = "a";
|
||||||
|
assert_eq!(tokenize(line, Some('a')), vec![0..0]);
|
||||||
|
let line = "aa";
|
||||||
|
assert_eq!(tokenize(line, Some('a')), vec![0..0, 1..1]);
|
||||||
|
let line = "..a..a";
|
||||||
|
assert_eq!(tokenize(line, Some('a')), vec![0..2, 3..5]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -581,3 +581,12 @@ fn test_check_silent() {
|
||||||
.fails()
|
.fails()
|
||||||
.stdout_is("");
|
.stdout_is("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_trailing_separator() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-t", "x", "-k", "1,1"])
|
||||||
|
.pipe_in("aax\naaa\n")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("aax\naaa\n");
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue