1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

sort: allow some functions to be called with OsStr

This commit is contained in:
Michael Debertol 2021-05-07 23:40:07 +02:00 committed by Sylvestre Ledru
parent c0c240f194
commit 64c1f16421

View file

@ -20,8 +20,8 @@ mod external_sort;
mod numeric_str_cmp;
use clap::{App, Arg};
use external_sort::ext_sort;
use custom_str_cmp::custom_str_cmp;
use external_sort::ext_sort;
use fnv::FnvHasher;
use itertools::Itertools;
use numeric_str_cmp::{numeric_str_cmp, NumInfo, NumInfoParseSettings};
@ -33,6 +33,7 @@ use smallvec::SmallVec;
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
@ -1109,10 +1110,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
exec(files, settings)
}
fn file_to_lines_iter<'a>(
file: &str,
settings: &'a GlobalSettings,
) -> Option<impl Iterator<Item = Line> + 'a> {
fn file_to_lines_iter(
file: impl AsRef<OsStr>,
settings: &'_ GlobalSettings,
) -> Option<impl Iterator<Item = Line> + '_> {
let (reader, _) = match open(file) {
Some(x) => x,
None => return None,
@ -1177,7 +1178,7 @@ fn exec(files: Vec<String>, settings: GlobalSettings) -> i32 {
let mut lines = vec![];
// This is duplicated from fn file_to_lines_iter, but using that function directly results in a performance regression.
for (file, _) in files.iter().map(|file| open(file)).flatten() {
for (file, _) in files.iter().map(open).flatten() {
let buf_reader = BufReader::new(file);
for line in buf_reader.split(if settings.zero_terminated {
b'\0'
@ -1501,7 +1502,8 @@ fn print_sorted<T: Iterator<Item = Line>>(iter: T, settings: &GlobalSettings) {
}
// from cat.rs
fn open(path: &str) -> Option<(Box<dyn Read>, bool)> {
fn open(path: impl AsRef<OsStr>) -> Option<(Box<dyn Read>, bool)> {
let path = path.as_ref();
if path == "-" {
let stdin = stdin();
return Some((Box::new(stdin) as Box<dyn Read>, is_stdin_interactive()));
@ -1510,7 +1512,7 @@ fn open(path: &str) -> Option<(Box<dyn Read>, bool)> {
match File::open(Path::new(path)) {
Ok(f) => Some((Box::new(f) as Box<dyn Read>, false)),
Err(e) => {
show_error!("{0}: {1}", path, e.to_string());
show_error!("{0:?}: {1}", path, e.to_string());
None
}
}