From 496cd811a1fb586b2834b79b629383fdb6a89974 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 9 Sep 2023 16:21:03 +0200 Subject: [PATCH] small readability reorder Thought a bit about whether having OR instead of AND changes performance, and concluded it probably didn't. In particular, think about when it jumps to the inner part of the if/else condition --- README.md | 2 +- ww | Bin 17080 -> 17080 bytes ww.c | 22 ++++++++-------------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2e0219e..f11ecde 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Comparison with wc. -The GNU utils version ([github](https://github.com/coreutils/coreutils/tree/master/src/wc.c), [savannah](http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/wc.c;hb=HEAD)) is a bit over 1K lines of C. It does many things and checks many possible failure modes. +The GNU utils version ([github](https://github.com/coreutils/coreutils/tree/master/src/wc.c), [savannah](http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/wc.c;hb=HEAD)) is a bit over 1K lines of C. It does many things and checks many possible failure modes. I think it detects whether it should be reading from stdin using some very wrapped fstat. The busybox version ([git.busybox.net](https://git.busybox.net/busybox/tree/coreutils/wc.c)) of wc is much shorter, at 257 lines, while striving to be [POSIX-compliant](https://pubs.opengroup.org/onlinepubs/9699919799/), meaning it has flags. diff --git a/ww b/ww index 2ad13470480214c7ac24673625fc8b0c9741eeb6..58c8de80ad2a3329b124da2ce61777e3a7bb9d40 100755 GIT binary patch delta 736 zcmXX^Ur19?82`?$(@oblH$~SUogs;{wJx@a55um^yfYulHhTz*V5mW)#k7~uLKfWC z1?}L24?RjoQO1Q>J><=Q;hRJRA%O+Ol?t;QM7U1hQ9ron`+eWQ*pGvoOL&y`zG`*EyM*6jh{6~eu zBa9>m<3oq!^pHhXgU{>$#yn$8Ru<%xo(###8zV*lQu;u+Yfe_flU&iv6>9h~$DtiO z!?6#ssBh~o@%RqL8T=bCrK}tKFzia{j&a@bT%VKE;kG?T0M@c7a8yPB=xT9wtBfL6 z`q>Y#If^~3sE}2&iMz%>$x=COeuW$#&dOFDl{fj;bGsh(A2_K7-_nz={bZc>x+-e# zV+sc9;_^uN8i%s-C27O%gBTc|e~cLkY`JJ2pif;j<`(gX6r(Gyy=95&0>h28vM~CR zFVU`i#U(bV&F!mLAmYlmc}L}O5cOuYq;7@_u`v9xVlTfdNc3{mVKL2S@6H{#e82Pke&6qW-?=9i!LbOA z+Hi8(zFlB6pzJal6c+dDO2Z5D=boAmpFYz3r6Rv`qGPQig_K)*gC57p?Ywe4uFU4m z6^!x09x?pY6s9;dGU^p4M@^#anPU)AUr3MWj0 zl)T7aSP^C4U8rc|HdUv_$UdSORPN)48ZHK$dIwAw?D*$kh16@1t4zhjNuN-UP#-vL z{e}EMy`}n2c9bYtFRt~U1Q{)R??UKj6bya?5M|?z7Me1lGI7#4uVHA@Op})CajkSE z7eT+4)q7r%Q|tj8A$@F>Q-%~8IR=CKu4qt{K8K2+jRDxG=6{r`{cz-K<5lv2tuyjL z7>vASE6Rsz^Hn|2(rR=`o1k6#CT*)5WS2c!mB8GmRKil-g1C6ATzY6(6TnqID+13& zB9zoL4en8z>nm-k`oo`Zmwjm`Tt{y}`pEYLq+fiHmv(zZ*|iDj?sn&%_G{F$%KehA zqI-_V_WmD~_JeP5htdxa4QWaRI4DG3I_hwSY&nKYZ-V9taOezUm36I*ZAveYp_=2| zOfEU4;~Cq8USJ$I3K*NfSX&trU#M$iYI@JzxJGQZ=D2+YLL~rn$ckDB*8uN~1RDG}u1q)NWBi@Wo_GX}@*X!{C)Yj8 jKyzL 0) { - if (*c == '\n' || *c == ' ' || *c == '\t') { - if (seen_word) { - seen_sep_after_word = 1; - } + if (*c != ' ' && *c != '\n' && *c != '\t') { + word = 1; } else { - seen_word = 1; - } - // exercise: what happens if you only track seen_sep, - // instead of seen_sep_after_word? - // test with: $ echo " hello world" | ./wc - if (seen_word && seen_sep_after_word) { - num_words++; - seen_sep_after_word = seen_word = 0; + if (word) { + num_words++; + word = 0; + } } } - num_words+=seen_word; + num_words+=word; printf("%i\n", num_words); return 0; }