1010use clap:: Parser ;
1111use gettextrs:: { bind_textdomain_codeset, setlocale, textdomain, LocaleCategory } ;
1212use plib:: PROJECT_NAME ;
13- use std:: ffi:: OsStr ;
14- use std:: io:: { self , Read } ;
15- use std:: path:: PathBuf ;
13+ use std:: {
14+ ffi:: OsStr ,
15+ io:: { self , Read } ,
16+ ops:: AddAssign ,
17+ path:: PathBuf ,
18+ } ;
1619
1720/// wc - word, line, and byte or character count
1821#[ derive( Parser ) ]
@@ -38,36 +41,30 @@ struct Args {
3841 files : Vec < PathBuf > ,
3942}
4043
44+ #[ derive( Default ) ]
4145struct CountInfo {
4246 words : usize ,
4347 chars : usize ,
4448 nl : usize ,
4549}
4650
47- impl CountInfo {
48- fn new ( ) -> CountInfo {
49- CountInfo {
50- words : 0 ,
51- chars : 0 ,
52- nl : 0 ,
53- }
54- }
55-
56- fn accum ( & mut self , count : & CountInfo ) {
57- self . words = self . words + count. words ;
58- self . chars = self . chars + count. chars ;
59- self . nl = self . nl + count. nl ;
51+ impl AddAssign for CountInfo {
52+ fn add_assign ( & mut self , rhs : Self ) {
53+ self . words += rhs. words ;
54+ self . chars += rhs. chars ;
55+ self . nl += rhs. nl ;
6056 }
6157}
6258
59+ /// is_space
6360const fn create_table ( ) -> [ bool ; 256 ] {
6461 let mut table = [ false ; 256 ] ;
65- table[ 9 ] = true ;
66- table[ 10 ] = true ;
67- table[ 11 ] = true ;
68- table[ 12 ] = true ;
69- table[ 13 ] = true ;
70- table[ 32 ] = true ;
62+ table[ b'\t' as usize ] = true ;
63+ table[ b'\n' as usize ] = true ;
64+ table[ 11 /* \v */ ] = true ;
65+ table[ 12 /* \f */ ] = true ;
66+ table[ '\r' as usize ] = true ;
67+ table[ ' ' as usize ] = true ;
7168 table
7269}
7370
@@ -191,11 +188,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
191188 bind_textdomain_codeset ( PROJECT_NAME , "UTF-8" ) ?;
192189
193190 let mut exit_code = 0 ;
194- let mut totals = CountInfo :: new ( ) ;
191+ let mut totals = CountInfo :: default ( ) ;
195192
196193 // input via stdin
197194 if args. files . is_empty ( ) {
198- let mut count = CountInfo :: new ( ) ;
195+ let mut count = CountInfo :: default ( ) ;
199196
200197 if let Err ( e) = wc_file ( & args, chars_mode, & PathBuf :: new ( ) , & mut count) {
201198 exit_code = 1 ;
@@ -205,14 +202,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
205202 // input files
206203 } else {
207204 for filename in & args. files {
208- let mut count = CountInfo :: new ( ) ;
205+ let mut count = CountInfo :: default ( ) ;
209206
210207 if let Err ( e) = wc_file ( & args, chars_mode, filename, & mut count) {
211208 exit_code = 1 ;
212209 eprintln ! ( "{}: {}" , filename. display( ) , e) ;
213210 }
214211
215- totals. accum ( & count) ;
212+ totals += count;
216213 }
217214 }
218215
0 commit comments