Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wc default #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions wc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "wc"
name = "ccwc"
version = "0.1.0"
edition = "2021"

Expand Down
Empty file added wc/src/lib.rs
Empty file.
37 changes: 29 additions & 8 deletions wc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
use std::env;

struct Config {
option: String,
path: String,
}

fn main() {
let pattern: String = std::env::args().nth(1).expect("no pattern given");
let path: String = std::env::args().nth(2).expect("no path given");
let args: Vec<String> = env::args().collect();

let config: Config = parse_config(&args);

let option: &String = &config.option;
let path: &String = &config.path;

if pattern == "c" {
if option == "c" {
let number_of_bytes: usize =
count_number_of_bytes_in_file(&path).expect("could not count bytes in file");
return println!("{:?} {:?}", number_of_bytes, path);
} else if pattern == "l" {
} else if option == "l" {
let number_of_lines: usize =
count_number_of_lines_in_file(&path).expect("could not count lines in file");
return println!("{:?} {:?}", number_of_lines, path);
} else if pattern == "w" {
} else if option == "w" {
let number_of_words: usize =
count_number_of_words_in_file(&path).expect("could not count words in file");
return println!("{:?} {:?}", number_of_words, path);
} else if pattern == "m" {
} else if option == "m" {
let number_of_characters: usize =
count_number_of_characters_in_file(&path).expect("could not count characters in file");
return println!("{:?} {:?}", number_of_characters, path);
} else {
panic!("unknown pattern {:?}", pattern);
panic!("unknown option {:?}", option);
}
}

Expand Down Expand Up @@ -78,9 +89,19 @@ fn count_number_of_characters_in_file(path: &str) -> Result<usize, std::io::Erro
for line in reader.lines() {
let line = line?;
number_of_characters += line.char_indices().count();
number_of_characters += 1;
}

// number of characters will differ from wc command as Rust counts CRLF as LF
// TODO: test file for CRLF / LF before reading
Ok(number_of_characters)
}

fn parse_config(args: &[String]) -> Config {
let option: &String = &args[1];
let path: &String = &args[2];

Config {
option: option.clone(),
path: path.clone(),
}
}