diff --git a/Cargo.lock b/Cargo.lock index a6c97d3..76b1a97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "reverse" -version = "0.2.1" +version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 7bedebe..44b3726 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "reverse" -version = "0.2.1" +version = "0.3.0" authors = ["Avril "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["output-lines", "buffer-output", "ignore-output-errors"] +default = ["output-lines", "buffer-output", "ignore-output-errors", "ignore-invalid-args"] # Output as lines instead of `["2", "1", "0"]` output-lines = [] @@ -22,10 +22,14 @@ buffer-output = [] # Disable this if you are writing to a faulty device or expect some output operations to stdout to fail. ignore-output-errors = [] +# Ignore invalid arguments instead of removing invalid UTF8 characters if they exist in the argument +ignore-invalid-args = [] + [profile.release] opt-level = 3 lto = "fat" codegen-units = 1 strip=true +#panic="abort" # XXX: This doesn't remove panic handler, for some reason... [dependencies] diff --git a/src/main.rs b/src/main.rs index 6089abf..f62c4b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,20 +30,25 @@ fn binsearch<'a, V: ?Sized, T: PartialEq + 'a>(slice: &'a [T], find: &V) -> O fn collect_input() -> Box + 'static> { - //! TODO: Use non-panicking functions for both reading lines and reading args, just skip invalid lines/args + // TODO: Use non-panicking functions for both reading lines and reading args, just skip invalid lines/args + if std::env::args_os().len() <= 1 { use std::io::{ self, BufRead, }; // No args, collect stdin lines - Box::new(io::BufReader::new(io::stdin() - .lock()) + Box::new(io::stdin() + .lock() .lines() .filter_map(Result::ok)) } else { // Has arguments, return them - Box::new(std::env::args().skip(1)) + if cfg!(feature="ignore-invalid-args") { + Box::new(std::env::args_os().skip(1).filter_map(|os| os.into_string().ok())) + } else { + Box::new(std::env::args_os().skip(1).map(|os| os.to_string_lossy().into_owned())) + } } }