added word count

This commit is contained in:
2026-07-03 10:53:16 -05:00
parent e1849a0655
commit b973b6df40
2 changed files with 40 additions and 1 deletions
+33 -1
View File
@@ -56,6 +56,38 @@ read_file_into_mem(const char* path)
return buffer;
}
static size_t
word_count(const struct SPP_Stream s, const char* path)
{
size_t words = 0;
bool in_word = false;
while (!spp_eof(s)) {
char c = **s.cursor;
// Treat common ASCII whitespace as separators
bool is_ws = (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
c == '\v' || c == '\f');
if (is_ws) {
in_word = false;
(*s.cursor)++;
} else {
if (!in_word) {
words++;
printf("%s - Word Count: %lld\r", path, words);
fflush(stdout);
in_word = true;
}
(*s.cursor)++;
}
}
printf("%s - Word Count: %lld\n", path, words);
fflush(stdout);
return words;
}
static size_t
line_count(const struct SPP_Stream s, const char* path)
{
@@ -145,7 +177,7 @@ main(int argc, char* argv[])
const char* buffer = read_file_into_mem(path);
struct SPP_Stream stream = { .cursor = &buffer };
line_count(stream, path);
word_count(stream, path);
return EXIT_SUCCESS;
}
+7
View File
@@ -32,6 +32,13 @@ struct SPP_Stream
};
typedef struct SPP_Stream SPP_Stream;
struct SPP_Token
{
const char* buf;
size_t len;
};
typedef struct SPP_Stream SPP_Stream;
extern bool
spp_is(struct SPP_Stream s, const char* list);