Skip to content

Commit

Permalink
Optimize aneri_file::file_get_line_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Oct 6, 2024
1 parent 5c75174 commit de4fa5b
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions crates/file/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ pub fn file_append(path: PathBuf, data: String) -> bool {
pub fn file_get_line_count(path: PathBuf) -> Option<usize> {
let mut file = File::open(path).ok().map(BufReader::new)?;
let mut lines = 0_usize;
let mut temp_string = String::new();
loop {
match file.read_line(&mut temp_string) {
Ok(0) => break,
Ok(_) => (),
Err(_) => return None,
let mut buffer = [0; 8192];
while let Ok(bytes_read) = file.read(&mut buffer) {
if bytes_read == 0 {
break;
}
lines += 1;
temp_string.clear();
lines += buffer[..bytes_read].iter().filter(|&&b| b == b'\n').count();
}
Some(lines)
}
Expand Down

0 comments on commit de4fa5b

Please sign in to comment.