added word count
This commit is contained in:
@@ -56,6 +56,38 @@ read_file_into_mem(const char* path)
|
|||||||
return buffer;
|
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
|
static size_t
|
||||||
line_count(const struct SPP_Stream s, const char* path)
|
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);
|
const char* buffer = read_file_into_mem(path);
|
||||||
|
|
||||||
struct SPP_Stream stream = { .cursor = &buffer };
|
struct SPP_Stream stream = { .cursor = &buffer };
|
||||||
line_count(stream, path);
|
word_count(stream, path);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ struct SPP_Stream
|
|||||||
};
|
};
|
||||||
typedef struct SPP_Stream 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
|
extern bool
|
||||||
spp_is(struct SPP_Stream s, const char* list);
|
spp_is(struct SPP_Stream s, const char* list);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user