From cd3626642755359eecf64d05e3f9db3d8b00ed24 Mon Sep 17 00:00:00 2001 From: wrmr Date: Fri, 8 Nov 2024 23:30:42 -0500 Subject: use strv_t even more --- buf.c | 8 ++++---- buf.h | 3 ++- doc.c | 5 +++-- doc.h | 2 +- net.c | 4 ++-- parse.c | 11 +++++------ 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/buf.c b/buf.c index fb3af82..5575bbd 100644 --- a/buf.c +++ b/buf.c @@ -25,10 +25,10 @@ void buf_grow(buf_t *b, size_t n) { } } -void buf_cat(buf_t *b, const char *src, size_t n) { - buf_grow(b, n); - memcpy(&b->buf[b->sz], src, n); - b->sz += n; +void buf_cat(buf_t *b, strv_t s) { + buf_grow(b, s.n); + memcpy(&b->buf[b->sz], s.s, s.n); + b->sz += s.n; } void buf_catc(buf_t *b, char c) { diff --git a/buf.h b/buf.h index 59c521b..e7fb6b7 100644 --- a/buf.h +++ b/buf.h @@ -2,6 +2,7 @@ #define BUF_H #include +#include "strv.h" typedef struct buf { size_t sz, cap; @@ -12,7 +13,7 @@ void buf_init(buf_t *, size_t); void buf_grow(buf_t *, size_t); void buf_free(buf_t *); -void buf_cat(buf_t *b, const char *src, size_t n); +void buf_cat(buf_t *b, strv_t s); void buf_catc(buf_t *b, char c); #endif diff --git a/doc.c b/doc.c index d43ff30..aa18369 100644 --- a/doc.c +++ b/doc.c @@ -51,8 +51,9 @@ void doc_add_text(struct doc *d, strv_t s) { dl->len += s.n; } -unsigned short doc_add_link(struct doc *d, const char *url) { - buf_cat(&d->lnk, url, strlen(url) + 1); +unsigned short doc_add_link(struct doc *d, strv_t url) { + buf_cat(&d->lnk, url); + buf_catc(&d->lnk, 0); return d->linkc++; } diff --git a/doc.h b/doc.h index a0b1031..2a9fe58 100644 --- a/doc.h +++ b/doc.h @@ -40,6 +40,6 @@ int doc_line_next(struct doc *d, size_t *ofs); const char *doc_get_link(struct doc *d, unsigned short lnk); void doc_set_link(struct doc *d, unsigned short lnk); -unsigned short doc_add_link(struct doc *d, const char *url); +unsigned short doc_add_link(struct doc *d, strv_t url); #endif diff --git a/net.c b/net.c index b3f0655..0d6604e 100644 --- a/net.c +++ b/net.c @@ -132,7 +132,7 @@ static int fetch_file(const struct addr *adr, struct buf *buf, enum doc_type *do buf_init(buf, 1024); char b[256]; while (fgets(b, sizeof b, f)) { - buf_cat(buf, b, strlen(b)); + buf_cat(buf, strv(b)); } buf_catc(buf, 0); fclose(f); @@ -184,7 +184,7 @@ static int fetch_gopher(const struct addr *adr, struct buf *buf, enum doc_type * return -1; } if (!n) break; - buf_cat(buf, inbuf, n); + buf_cat(buf, (strv_t) { inbuf, n }); } close(s); return 0; diff --git a/parse.c b/parse.c index b62ea8a..df416ac 100644 --- a/parse.c +++ b/parse.c @@ -10,16 +10,16 @@ int isurlch(char c) { } void parse_plain_url(struct doc *d, struct doc_line *l, size_t i) { - char url[l->len + 1]; size_t start = i - 1; while (start > 0 && isalpha(l->txt[start])) start--; if (!isalpha(l->txt[start])) start++; size_t end = i + 3; while (end < l->len && isurlch(l->txt[end])) end++; if (end == i + 3) return; - size_t urln = end - start; - memcpy(url, &l->txt[start], urln); - url[urln] = 0; + strv_t url = { + &l->txt[start], + end - start + }; l->link = doc_add_link(d, url); } @@ -75,8 +75,7 @@ int parse_gophermap_line(struct doc *d, strv_t ln) { if (urln < sizeof url) url[urln++] = '/'; if (urln < sizeof url) url[urln++] = bits.item_type; urln = scatss(url, urln, sizeof url, bits.sel); - url[urln] = 0; - doc_set_link(d, doc_add_link(d, url)); + doc_set_link(d, doc_add_link(d, (strv_t) { url, urln })); case 'i': doc_add_text(d, bits.dstr); doc_new_line(d); -- cgit 1.4.1-2-gfad0