From 5f8d472483a22717908c0ffdda692b898fe4991a Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 5 Apr 2022 02:33:19 +0100 Subject: [PATCH] Added ignore-invalid-args: (default feature flag) Invalid utf8 arguments are ignored from the input arguments (as they are from the stdin lines.) If disabled, the invalid arg is kept and invalid utf8 characters are replaced with the unicode "invalid character" character instead. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for reverse's current commit: Curse − 凶 --- Cargo.lock | 2 +- Cargo.toml | 8 ++++++-- src/main.rs | 13 +++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) 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())) + } } }