From 2b23cd283cc8a25689cf6400d86309a2a1f0ffad Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Fri, 5 Mar 2021 06:38:08 -0500 Subject: fixed #3, benchmarks showed that buffered writing wasn't a bad idea --- src/main.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b26289c..fe1d0eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; use std::env::args; -use std::io::{stdin, BufRead, BufReader}; +use std::io::{prelude::*, stdin, stdout, BufReader, BufWriter}; const HELP_MSG: &str = "Title-Case Tool\n\ -h | --help\t\t\tPrints this message\n\ @@ -46,11 +46,18 @@ fn main() { // gather exceptions let exceptions: HashSet<&'static str> = EXCEPTIONS.iter().cloned().collect(); - // read input and correct lines + // read input, correct lines, write output + let mut writer = BufWriter::new(stdout()); for line in BufReader::new(stdin()).lines() { let line = line.expect("IO error"); - println!("{}", correct_line(&line, &ignorables, &exceptions)); + writeln!( + &mut writer, + "{}", + correct_line(&line, &ignorables, &exceptions) + ) + .expect("Failed to write output"); } + writer.flush().expect("Failed to write output"); } /// Corrects a single line (title) based on placement and exceptions. -- cgit v1.2.3