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
This commit is contained in:
NunoSempere 2023-09-09 16:21:03 +02:00
parent aec062db2e
commit 496cd811a1
3 changed files with 9 additions and 15 deletions

View File

@ -11,7 +11,7 @@
## Comparison with wc. ## 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. 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.

BIN
ww

Binary file not shown.

20
ww.c
View File

@ -4,25 +4,19 @@
int wc(FILE* fp) int wc(FILE* fp)
{ {
char c[1]; char c[1];
int seen_word = 0, seen_sep_after_word = 0, num_words = 0; int word = 0, num_words = 0;
int fn = fileno(fp); int fn = fileno(fp);
while (read(fn, c, sizeof(c)) > 0) { while (read(fn, c, sizeof(c)) > 0) {
if (*c == '\n' || *c == ' ' || *c == '\t') { if (*c != ' ' && *c != '\n' && *c != '\t') {
if (seen_word) { word = 1;
seen_sep_after_word = 1;
}
} else { } else {
seen_word = 1; if (word) {
}
// 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++; num_words++;
seen_sep_after_word = seen_word = 0; word = 0;
} }
} }
num_words+=seen_word; }
num_words+=word;
printf("%i\n", num_words); printf("%i\n", num_words);
return 0; return 0;
} }