add: dev workflow shenanigans

some shit
This commit is contained in:
2026-08-29 13:57:59 -04:00
parent c4d4f0b713
commit 5fc2f3e668
10 changed files with 325 additions and 88 deletions
+153 -87
View File
@@ -19,24 +19,26 @@
#include <wchar.h>
#include <wctype.h>
enum {
F_LINES = 1 << 0, /* -l: count '\n' */
F_WORDS = 1 << 1, /* -w: whitespace-separated tokens */
F_CHARS = 1 << 2, /* -m: multibyte characters */
F_BYTES = 1 << 3, /* -c: bytes */
enum
{
F_LINES = 1 << 0, /* -l: count '\n' */
F_WORDS = 1 << 1, /* -w: whitespace-separated tokens */
F_CHARS = 1 << 2, /* -m: multibyte characters */
F_BYTES = 1 << 3, /* -c: bytes */
};
static int flags = 0;
typedef struct {
typedef struct
{
long long lines;
long long words;
long long chars;
long long bytes;
int ok; /* read succeeded */
int ok; /* read succeeded */
} counts_t;
static unsigned char ws_tab[256]; /* ws_tab[c] = 1 if c is whitespace */
static unsigned char ws_tab[256]; /* ws_tab[c] = 1 if c is whitespace */
static void init_ws_tab(void)
{
@@ -46,18 +48,17 @@ static void init_ws_tab(void)
static void usage(FILE *out)
{
fprintf(out,
"usage: fastwc [-lwc] [-m] [file...]\n"
"\n"
"Count lines, words, and bytes (default) or selected counts.\n"
"With no file, or when file is -, read standard input.\n"
"\n"
" -l count lines\n"
" -w count words\n"
" -c count bytes\n"
" -m count characters\n"
" --help display this help and exit\n"
" --version output version information and exit\n");
fprintf(out, "usage: fastwc [-lwc] [-m] [file...]\n"
"\n"
"Count lines, words, and bytes (default) or selected counts.\n"
"With no file, or when file is -, read standard input.\n"
"\n"
" -l count lines\n"
" -w count words\n"
" -c count bytes\n"
" -m count characters\n"
" --help display this help and exit\n"
" --version output version information and exit\n");
}
/*
@@ -75,7 +76,8 @@ static long long count_newlines(const unsigned char *s, size_t n)
long long k = 0;
size_t i = 0;
for (; i + 8 <= n; i += 8) {
for (; i + 8 <= n; i += 8)
{
uint64_t x;
memcpy(&x, s + i, 8);
x = (x ^ nl) & cl;
@@ -99,7 +101,8 @@ static long long count_words(const unsigned char *s, size_t n, int *prev_ws)
size_t i = 0;
int prev = *prev_ws;
for (; i + 8 <= n; i += 8) {
for (; i + 8 <= n; i += 8)
{
uint8_t m = 0;
m |= (uint8_t)ws_tab[s[i + 0]] << 0;
m |= (uint8_t)ws_tab[s[i + 1]] << 1;
@@ -117,7 +120,8 @@ static long long count_words(const unsigned char *s, size_t n, int *prev_ws)
prev = (m >> 7) & 1;
}
for (; i < n; i++) {
for (; i < n; i++)
{
int ws = ws_tab[s[i]];
if (prev && !ws)
w++;
@@ -142,27 +146,34 @@ static void count_stream_mb(FILE *fp, counts_t *c)
memset(&st, 0, sizeof st);
while ((nread = fread(buf + pend, 1, sizeof buf - pend, fp)) > 0) {
while ((nread = fread(buf + pend, 1, sizeof buf - pend, fp)) > 0)
{
size_t n = nread + pend;
size_t i = 0;
c->bytes += (long long)nread;
while (i < n) {
while (i < n)
{
wchar_t wc;
size_t r;
if (buf[i] < 0x80) {
if (buf[i] < 0x80)
{
wc = buf[i];
r = 1;
} else {
}
else
{
r = mbrtowc(&wc, (const char *)buf + i, n - i, &st);
if (r == (size_t)-2) { /* incomplete: carry over */
if (r == (size_t)-2)
{ /* incomplete: carry over */
pend = n - i;
memmove(buf, buf + i, pend);
break;
}
if (r == (size_t)-1) { /* invalid sequence */
if (r == (size_t)-1)
{ /* invalid sequence */
memset(&st, 0, sizeof st);
wc = L'\xfffd';
r = 1;
@@ -171,9 +182,12 @@ static void count_stream_mb(FILE *fp, counts_t *c)
if (wc == L'\n')
c->lines++;
if (iswspace(wc)) {
if (iswspace(wc))
{
prev_ws = 1;
} else if (prev_ws) {
}
else if (prev_ws)
{
c->words++;
prev_ws = 0;
}
@@ -192,16 +206,21 @@ static void count_stream_mb(FILE *fp, counts_t *c)
/* Fast path (-l/-w/-c): one pass, per-chunk memchr + table counting. */
static void count_stream(FILE *fp, counts_t *c)
{
static unsigned char buf[1 << 17]; /* 128 KiB */
static unsigned char buf[1 << 17]; /* 128 KiB */
size_t nread;
int prev_ws = 1; /* start of file: as if preceded by whitespace */
int prev_ws = 1; /* start of file: as if preceded by whitespace */
if (flags & F_CHARS) {
if (flags & F_CHARS)
{
count_stream_mb(fp, c);
return;
}
while ((nread = fread(buf, 1, sizeof buf, fp)) > 0) {
for (;;)
{
nread = fread(buf, 1, sizeof buf, fp); /* NOLINT: EOF-state FP */
if (nread == 0)
break;
c->bytes += (long long)nread;
if (flags & F_LINES)
c->lines += count_newlines(buf, nread);
@@ -217,38 +236,53 @@ static void count_file(const char *path, counts_t *c)
{
FILE *fp;
if (strcmp(path, "-") == 0) {
fp = stdin;
} else {
fp = fopen(path, "rb");
if (fp == NULL) {
fprintf(stderr, "fastwc: %s: %s\n", path, strerror(errno));
c->ok = 0;
return;
}
if (strcmp(path, "-") == 0)
{
count_stream(stdin, c);
if (ferror(stdin))
fprintf(stderr, "fastwc: standard input: read error: %s\n",
strerror(errno));
return;
}
fp = fopen(path, "rb");
if (fp == NULL)
{
fprintf(stderr, "fastwc: %s: %s\n", path, strerror(errno));
c->ok = 0;
return;
}
count_stream(fp, c);
if (ferror(fp))
fprintf(stderr, "fastwc: %s: read error: %s\n",
strcmp(path, "-") == 0 ? "standard input" : path,
strerror(errno));
fprintf(stderr, "fastwc: %s: read error: %s\n", path, strerror(errno));
if (fp != stdin)
fclose(fp);
fclose(fp);
}
static int col_width(long long v)
{
int w = 1;
while (v >= 10) {
while (v >= 10)
{
v /= 10;
w++;
}
return w;
}
static void widen(int *width, long long v, int enabled)
{
int w;
if (!enabled)
return;
w = col_width(v);
if (w > *width)
*width = w;
}
int main(int argc, char **argv)
{
counts_t *rows;
@@ -258,29 +292,43 @@ int main(int argc, char **argv)
init_ws_tab();
for (a = 1; a < argc; a++) {
for (a = 1; a < argc; a++)
{
const char *arg = argv[a];
if (arg[0] != '-' || arg[1] == '\0')
break; /* first file argument */
if (strcmp(arg, "--") == 0) {
break; /* first file argument */
if (strcmp(arg, "--") == 0)
{
a++;
break;
}
if (strcmp(arg, "--help") == 0) {
if (strcmp(arg, "--help") == 0)
{
usage(stdout);
return 0;
}
if (strcmp(arg, "--version") == 0) {
if (strcmp(arg, "--version") == 0)
{
printf("fastwc 0.1.0\n");
return 0;
}
for (const char *p = arg + 1; *p; p++) {
switch (*p) {
case 'l': flags |= F_LINES; break;
case 'w': flags |= F_WORDS; break;
case 'c': flags |= F_BYTES; break;
case 'm': flags |= F_CHARS; break;
for (const char *p = arg + 1; *p; p++)
{
switch (*p)
{
case 'l':
flags |= F_LINES;
break;
case 'w':
flags |= F_WORDS;
break;
case 'c':
flags |= F_BYTES;
break;
case 'm':
flags |= F_CHARS;
break;
default:
fprintf(stderr, "fastwc: invalid option -- '%c'\n", *p);
usage(stderr);
@@ -290,25 +338,30 @@ int main(int argc, char **argv)
}
if (flags == 0)
flags = F_LINES | F_WORDS | F_BYTES; /* wc default: -l -w -c */
flags = F_LINES | F_WORDS | F_BYTES; /* wc default: -l -w -c */
if (flags & F_CHARS)
setlocale(LC_CTYPE, "");
nfiles = argc - a;
if (nfiles == 0) {
if (nfiles == 0)
{
rows = calloc(1, sizeof *rows);
rows[0].ok = 1;
count_stream(stdin, &rows[0]);
if (!rows[0].ok) {
if (!rows[0].ok)
{
fprintf(stderr, "fastwc: standard input: read error: %s\n",
strerror(errno));
failed = 1;
}
nfiles = 1;
} else {
}
else
{
rows = calloc((size_t)nfiles, sizeof *rows);
for (i = 0; i < nfiles; i++) {
for (i = 0; i < nfiles; i++)
{
rows[i].ok = 1;
count_file(argv[a + i], &rows[i]);
if (!rows[i].ok)
@@ -320,36 +373,49 @@ int main(int argc, char **argv)
int wl = 1, ww = 1, wm = 1, wb = 1;
long long tl = 0, tw = 0, tm = 0, tb = 0;
for (i = 0; i < nfiles; i++) {
for (i = 0; i < nfiles; i++)
{
counts_t *r = &rows[i];
int x;
tl += r->lines; tw += r->words; tm += r->chars; tb += r->bytes;
if ((flags & F_LINES) && (x = col_width(r->lines)) > wl) wl = x;
if ((flags & F_WORDS) && (x = col_width(r->words)) > ww) ww = x;
if ((flags & F_CHARS) && (x = col_width(r->chars)) > wm) wm = x;
if ((flags & F_BYTES) && (x = col_width(r->bytes)) > wb) wb = x;
tl += r->lines;
tw += r->words;
tm += r->chars;
tb += r->bytes;
widen(&wl, r->lines, flags & F_LINES);
widen(&ww, r->words, flags & F_WORDS);
widen(&wm, r->chars, flags & F_CHARS);
widen(&wb, r->bytes, flags & F_BYTES);
}
if ((flags & F_LINES) && col_width(tl) > wl) wl = col_width(tl);
if ((flags & F_WORDS) && col_width(tw) > ww) ww = col_width(tw);
if ((flags & F_CHARS) && col_width(tm) > wm) wm = col_width(tm);
if ((flags & F_BYTES) && col_width(tb) > wb) wb = col_width(tb);
widen(&wl, tl, flags & F_LINES);
widen(&ww, tw, flags & F_WORDS);
widen(&wm, tm, flags & F_CHARS);
widen(&wb, tb, flags & F_BYTES);
for (i = 0; i < nfiles; i++) {
for (i = 0; i < nfiles; i++)
{
counts_t *r = &rows[i];
if (flags & F_LINES) printf("%*lld ", wl, r->lines);
if (flags & F_WORDS) printf("%*lld ", ww, r->words);
if (flags & F_CHARS) printf("%*lld ", wm, r->chars);
if (flags & F_BYTES) printf("%*lld ", wb, r->bytes);
if (flags & F_LINES)
printf("%*lld ", wl, r->lines);
if (flags & F_WORDS)
printf("%*lld ", ww, r->words);
if (flags & F_CHARS)
printf("%*lld ", wm, r->chars);
if (flags & F_BYTES)
printf("%*lld ", wb, r->bytes);
if (argc - a > 0)
printf("%s", argv[a + i]);
printf("\n");
}
if (argc - a > 1) {
if (flags & F_LINES) printf("%*lld ", wl, tl);
if (flags & F_WORDS) printf("%*lld ", ww, tw);
if (flags & F_CHARS) printf("%*lld ", wm, tm);
if (flags & F_BYTES) printf("%*lld ", wb, tb);
if (argc - a > 1)
{
if (flags & F_LINES)
printf("%*lld ", wl, tl);
if (flags & F_WORDS)
printf("%*lld ", ww, tw);
if (flags & F_CHARS)
printf("%*lld ", wm, tm);
if (flags & F_BYTES)
printf("%*lld ", wb, tb);
printf("total\n");
}