我试图通过将wc unix工具克隆到rust来完成一个编码挑战。我在最后一步有一个挑战,那就是支持命令行选项-m,它输出文件中的字符数。如果当前的语言环境不支持多字节字符,它将匹配-c选项。
以下是挑战描述的链接:https://codingchallenges.fyi/challenges/challenge-wc/
实现的选项可能由于区域设置问题而输出的数字小于预期的数字。预期的输出是339292 test.txt
,而我得到的是327900
。
我已经试着把我的头绕在unicode上,但我仍然不明白。https://learn.microsoft.com/en-us/globalization/locale/locale和https://tonsky.me/blog/unicode/
测试文件可在https://github.com/andwati/wc-rs/blob/main/test.txt上获得
这是我的实现。我对Rust很陌生,所以代码可能不是惯用的。
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io::{self, BufReader};
fn number_of_bytes(file_path: &str) -> io::Result<()> {
let f = File::open(file_path)?;
let mut reader = BufReader::new(f);
let mut buffer = Vec::new();
// read the whole file
reader.read_to_end(&mut buffer)?;
let total_bytes = buffer.len();
println!("{} {}", total_bytes, file_path);
Ok(())
}
fn number_of_lines(file_path: &str) -> io::Result<()> {
let f = File::open(file_path)?;
let reader = BufReader::new(f);
let line_count = reader.lines().count();
println!("{} {}", line_count, file_path);
Ok(())
}
fn number_of_words(file_path: &str) {
let f = File::open(file_path).expect("Error opening the file");
let reader = BufReader::new(f);
let mut word_count: u32 = 0;
for line in reader.lines() {
let curr: String = line.expect("Error reading content of the file");
// let words: Vec<&str> = curr.split(" ").collect();
let words: Vec<&str> = curr.split_whitespace().collect();
let filtered_words: Vec<&str> = words.into_iter().filter(|word| word.len() > 0).collect();
word_count += filtered_words.len() as u32
}
println!("{}", word_count);
}
fn number_of_characters(file_path: &str) {
let mut file = File::open(file_path).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
print!("{}", s.trim_end().chars().count());
}
fn main() {
let args: Vec<String> = env::args().collect();
let file_path = &args[2];
if args.len() > 1 && args[1] == "-c" {
number_of_bytes(file_path).unwrap();
} else if args.len() > 1 && args[1] == "-l" {
number_of_lines(file_path).unwrap();
} else if args.len() > 1 && args[1] == "-w" {
number_of_words(&file_path);
} else if args.len() > 1 && args[1] == "-m" {
number_of_characters(file_path);
} else {
eprintln!("Usage: wc-tool -c <filepath>");
std::process::exit(1);
}
}
字符串
我试着把我的头绕在unicode上,但我还是不明白。https://learn.microsoft.com/en-us/globalization/locale/locale和https://tonsky.me/blog/unicode/。
1条答案
按热度按时间s5a0g9ez1#
我能够通过实现函数来获得准确的读数,
字符串