Skip to content

Commit 3c68462

Browse files
committed
fix: import and match
1 parent afcd762 commit 3c68462

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

src/uu/sort/src/sort.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout};
3939
use std::num::{IntErrorKind, NonZero};
4040
use std::ops::Range;
4141
#[cfg(unix)]
42-
use std::os::unix::ffi::{OsStrExt};
42+
use std::os::unix::ffi::OsStrExt;
4343
use std::path::Path;
4444
use std::path::PathBuf;
4545
use std::str::Utf8Error;
@@ -2042,26 +2042,35 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
20422042
path: files0_from.clone(),
20432043
error,
20442044
})?;
2045-
#[cfg(unix)]
2046-
let f = OsStr::from_bytes(&line);
2047-
#[cfg(not(unix))]
2048-
let f_str = String::from_utf8_lossy(&line);
2049-
#[cfg(not(unix))]
2050-
let f = OsStr::new(f_str.as_ref());
2051-
2052-
if f == STDIN_FILE {
2053-
return Err(SortError::MinusInStdIn.into());
2054-
}
2055-
if f.is_empty() {
2056-
return Err(SortError::ZeroLengthFileName {
2057-
file: files0_from,
2058-
line_num: line_num + 1,
2045+
2046+
let f: OsString = {
2047+
#[cfg(unix)]
2048+
{
2049+
OsStr::from_bytes(&line).to_os_string()
2050+
}
2051+
#[cfg(not(unix))]
2052+
{
2053+
String::from_utf8_lossy(&line).into_owned().into()
2054+
}
2055+
};
2056+
2057+
match f.to_str() {
2058+
Some(s) if s == STDIN_FILE => {
2059+
return Err(SortError::MinusInStdIn.into());
2060+
}
2061+
Some("") => {
2062+
return Err(SortError::ZeroLengthFileName {
2063+
file: files0_from,
2064+
line_num: line_num + 1,
2065+
}
2066+
.into());
20592067
}
2060-
.into());
2068+
_ => {}
20612069
}
20622070

2063-
files.push(f.to_os_string());
2071+
files.push(f);
20642072
}
2073+
20652074
if files.is_empty() {
20662075
return Err(SortError::EmptyInputFile { file: files0_from }.into());
20672076
}

tests/by-util/test_sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ fn test_files0_from_non_utf8() {
18591859
let (at, mut ucmd) = at_and_ucmd!();
18601860

18611861
// non-UTF-8 bytes (0xFF)
1862-
let filename = std::ffi::OsString::from_vec(vec![b'a', 0xFF, b'b']);
1862+
let filename = std::ffi::OsString::from_vec(b"a\xffb".into());
18631863
std::fs::write(at.plus(&filename), b"20\n10\n").unwrap();
18641864

18651865
let list_contents = vec![b'a', 0xFF, b'b', 0];

0 commit comments

Comments
 (0)