mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
wc: refactor WordCount into its own module
Move the `WordCount` struct and its implementations into the `wordcount.rs`.
This commit is contained in:
parent
a74a729aa8
commit
50f4941d49
2 changed files with 49 additions and 46 deletions
|
@ -12,8 +12,10 @@ extern crate uucore;
|
|||
|
||||
mod count_bytes;
|
||||
mod countable;
|
||||
mod wordcount;
|
||||
use count_bytes::count_bytes_fast;
|
||||
use countable::WordCountable;
|
||||
use wordcount::{TitledWordCount, WordCount};
|
||||
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use thiserror::Error;
|
||||
|
@ -21,7 +23,6 @@ use thiserror::Error;
|
|||
use std::cmp::max;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Write};
|
||||
use std::ops::{Add, AddAssign};
|
||||
use std::path::Path;
|
||||
use std::str::from_utf8;
|
||||
|
||||
|
@ -82,51 +83,6 @@ impl Settings {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
struct WordCount {
|
||||
bytes: usize,
|
||||
chars: usize,
|
||||
lines: usize,
|
||||
words: usize,
|
||||
max_line_length: usize,
|
||||
}
|
||||
|
||||
impl Add for WordCount {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
bytes: self.bytes + other.bytes,
|
||||
chars: self.chars + other.chars,
|
||||
lines: self.lines + other.lines,
|
||||
words: self.words + other.words,
|
||||
max_line_length: max(self.max_line_length, other.max_line_length),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for WordCount {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
*self = *self + other
|
||||
}
|
||||
}
|
||||
|
||||
impl WordCount {
|
||||
fn with_title(self, title: &str) -> TitledWordCount {
|
||||
TitledWordCount { title, count: self }
|
||||
}
|
||||
}
|
||||
|
||||
/// This struct supplements the actual word count with a title that is displayed
|
||||
/// to the user at the end of the program.
|
||||
/// The reason we don't simply include title in the `WordCount` struct is that
|
||||
/// it would result in unneccesary copying of `String`.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
struct TitledWordCount<'a> {
|
||||
title: &'a str,
|
||||
count: WordCount,
|
||||
}
|
||||
|
||||
static ABOUT: &str = "Display newline, word, and byte counts for each FILE, and a total line if
|
||||
more than one FILE is specified.";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
|
47
src/uu/wc/src/wordcount.rs
Normal file
47
src/uu/wc/src/wordcount.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use std::cmp::max;
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct WordCount {
|
||||
pub bytes: usize,
|
||||
pub chars: usize,
|
||||
pub lines: usize,
|
||||
pub words: usize,
|
||||
pub max_line_length: usize,
|
||||
}
|
||||
|
||||
impl Add for WordCount {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
bytes: self.bytes + other.bytes,
|
||||
chars: self.chars + other.chars,
|
||||
lines: self.lines + other.lines,
|
||||
words: self.words + other.words,
|
||||
max_line_length: max(self.max_line_length, other.max_line_length),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for WordCount {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
*self = *self + other
|
||||
}
|
||||
}
|
||||
|
||||
impl WordCount {
|
||||
pub fn with_title(self, title: &str) -> TitledWordCount {
|
||||
TitledWordCount { title, count: self }
|
||||
}
|
||||
}
|
||||
|
||||
/// This struct supplements the actual word count with a title that is displayed
|
||||
/// to the user at the end of the program.
|
||||
/// The reason we don't simply include title in the `WordCount` struct is that
|
||||
/// it would result in unneccesary copying of `String`.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct TitledWordCount<'a> {
|
||||
pub title: &'a str,
|
||||
pub count: WordCount,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue