Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions wordcount/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::ascii::AsciiExt;
use std::collections;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::io;
use std::io::{BufferedReader, File, IoResult};
use std::io::{BufferedReader, BufferedWriter, File, IoResult};

pub mod config;
pub mod btree_map;
Expand All @@ -27,7 +27,7 @@ fn do_work(cfg: &config::Config) -> IoResult<()> {
}
}
let mut writer = match cfg.output {
Some(ref x) => { Box::new(try!(File::create(&Path::new(x.as_slice())))) as Box<Writer> }
Some(ref x) => { Box::new(BufferedWriter::new(try!(File::create(&Path::new(x.as_slice()))))) as Box<Writer> }
None => { Box::new(io::stdout()) as Box<Writer> }
};

Expand All @@ -40,16 +40,14 @@ fn do_work(cfg: &config::Config) -> IoResult<()> {
// let re = Regex::new(r"[a-zA-Z0-9_]+").unwrap();
for reader in readers.iter_mut() {
for line in reader.lines() {
for caps in re.captures_iter(line.unwrap().as_slice()) {
if let Some(cap) = caps.at(0) {
let word = match cfg.ignore_case {
true => cap.to_ascii_lowercase(),
false => cap.to_string(),
};
match map.entry(word) {
Occupied(mut view) => { *view.get_mut() += 1; }
Vacant(view) => { view.insert(1); }
}
let line = line.unwrap();
for (start, end) in re.find_iter(&line[]) {
let word = &line[start..end];
let word = if cfg.ignore_case { word.to_ascii_lowercase() }
else { String::from_str(word) };
match map.entry(word) {
Occupied(mut view) => { *view.get_mut() += 1; }
Vacant(view) => { view.insert(1); }
}
}
}
Expand All @@ -60,8 +58,7 @@ fn do_work(cfg: &config::Config) -> IoResult<()> {
words.sort();
for word in words.iter() {
if let Some(count) = map.get(*word) {
let line = format!("{}\t{}\n", count, word);
try!(writer.write(line.as_bytes()));
try!(writeln!(writer, "{}\t{}", count, word));
}
}
Ok(())
Expand Down