Added testing features for buffering output, and ignoring output errors.

Fortune for reverse's current commit: Curse − 凶
master
Avril 3 years ago
parent 47df7a1e91
commit 5a9edf3f5c
Signed by: flanchan
GPG Key ID: 284488987C31F630

5
.gitignore vendored

@ -1 +1,6 @@
/target /target
perf.*
*.png
flamegraph.svg
vgcore.*

@ -15,6 +15,12 @@ output-lines = []
# Print each string escaped # Print each string escaped
output-quoted = [] output-quoted = []
# Buffer output to conserve syscalls, useful for very large inputs (can cause higher memory usage, but generally speeds output up considerably)
buffer-output = []
# Do not attempt to handle output errors
ignore-output-errors = []
[profile.release] [profile.release]
opt-level = 3 opt-level = 3
lto = "fat" lto = "fat"

@ -17,27 +17,72 @@ fn binsearch<'a, V: ?Sized, T: PartialEq<V> + 'a>(slice: &'a [T], find: &V) -> O
{ {
match slice { match slice {
[ref a, pivot @ .., ref b] => { [ref a, pivot @ .., ref b] => {
match (a==find, b==find) { match (a==find, b==find) {
(true, _) => Some(a), (true, _) => Some(a),
(_, true) => Some(b), (_, true) => Some(b),
_ => binsearch(pivot, find), _ => binsearch(pivot, find),
} }
}, },
[ref a] if a == find => Some(a), [ref a] if a == find => Some(a),
_ => None, _ => None,
} }
} }
fn collect_input() -> Box<dyn Iterator<Item=String> + 'static>
{
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())
.lines()
.filter_map(Result::ok))
} else {
// Has arguments, return them
Box::new(std::env::args().skip(1))
}
}
#[inline(always)]
fn handle_fmt_err<T>(res: std::io::Result<T>)
{
#[cfg(not(feature="ignore-output-errors"))]
res.expect("[!] failed to write");
let _ = res;
}
fn main() { fn main() {
let mut args: Vec<String> = std::env::args().skip(1).collect(); let mut args: Vec<String> = collect_input().collect();
reverse(&mut args[..]); reverse(&mut args[..]);
//eprintln!("{:?}", binsearch(&args[..], "1")); // It works! //eprintln!("{:?}", binsearch(&args[..], "1")); // It works!
#[cfg(feature="output-lines")] #[cfg(feature="output-lines")]
{ {
#[allow(unused_imports)]
use std::io::{
Write, BufWriter,
};
#[cfg(feature="buffer-output")]
let mut out = BufWriter::new(std::io::stdout().lock());
#[cfg(not(feature="buffer-output"))]
let mut out = std::io::stdout().lock();
for x in args.iter() { for x in args.iter() {
#[cfg(feature="output-quoted")] println!("{:?}", x); handle_fmt_err({
#[cfg(not(feature="output-quoted"))] println!("{}", x); if cfg!(feature="output-quoted") {
writeln!(&mut out, "{:?}", x)
}
else {
//writeln!(&mut out, "{}", x)
out.write(x.as_bytes())
.and_then(|_| out.write(b"\n"))
.map(|_| {})
}
});
} }
//#[cfg(feature="buffer-output")]
handle_fmt_err(out.flush());
} }
#[cfg(not(feature="output-lines"))] #[cfg(not(feature="output-lines"))]
println!("{:?}", args); println!("{:?}", args);

Loading…
Cancel
Save