summary refs log tree commit diff
diff options
context:
space:
mode:
authorwrmr2024-11-08 23:57:30 -0500
committerwrmr2024-11-08 23:57:30 -0500
commit9ac1ca968081bcb826cf9ee70666bc44983d087f (patch)
tree1d130f91c53c41f5a6f18d357fb4fa96c3088883
parentcd3626642755359eecf64d05e3f9db3d8b00ed24 (diff)
simplify strv_head interface, rename to strv_split
-rw-r--r--parse.c27
-rw-r--r--strv.c16
-rw-r--r--strv.h2
3 files changed, 19 insertions, 26 deletions
diff --git a/parse.c b/parse.c
index df416ac..8ea90f8 100644
--- a/parse.c
+++ b/parse.c
@@ -25,10 +25,8 @@ void parse_plain_url(struct doc *d, struct doc_line *l, size_t i) {
 
 int parse_plain(struct doc *d, const buf_t *b) {
 	doc_init(d);
-	strv_t buf = (strv_t) { b->buf, b->sz };
-	size_t i = 0;
-	while (i < buf.n) {
-		strv_t ln = strv_head(buf, '\n', &i);
+	strv_t ln, buf = (strv_t) { b->buf, b->sz };
+	while (strv_split(&buf, '\n', &ln)) {
 		struct doc_line *l = doc_line_at(d, d->latest);
 		for (size_t j = 1; j + 2 < ln.n; j++) {
 			if (ln.s[j] == ':' && ln.s[j + 1] == '/' && ln.s[j + 2] == '/') {
@@ -59,12 +57,11 @@ int parse_gophermap_line(struct doc *d, strv_t ln) {
 		strv_t host;
 		strv_t port;
 	} bits;
-	size_t i = 0;
-	bits.item_type = ln.s[i++];
-	bits.dstr = strv_head(ln, '\t', &i); 
-	bits.sel = strv_head(ln, '\t', &i); 
-	bits.host = strv_head(ln, '\t', &i); 
-	bits.port = strv_head(ln, '\t', &i); 
+	bits.item_type = *(ln.s++); ln.n++;
+	strv_split(&ln, '\t', &bits.dstr);
+	strv_split(&ln, '\t', &bits.sel);
+	strv_split(&ln, '\t', &bits.host);
+	strv_split(&ln, '\t', &bits.port);
 	switch (bits.item_type) {
 	case '.':
 		if (ln.n == 1) return 1;
@@ -86,14 +83,10 @@ int parse_gophermap_line(struct doc *d, strv_t ln) {
 
 int parse_gophermap(struct doc *d, const buf_t *b) {
 	doc_init(d);
-	size_t i = 0;
-	strv_t bufss = { b->buf, b->sz };
-	while (i < b->sz) {
-		strv_t ln = strv_head(bufss, '\n', &i);
+	strv_t ln, buf = { b->buf, b->sz };
+	while (strv_split(&buf, '\n', &ln)) {
 		if (ln.n > 0 && ln.s[ln.n - 1] == '\r') ln.n--;
-		if (parse_gophermap_line(d, ln)) {
-			break;
-		}
+		if (parse_gophermap_line(d, ln)) break;
 	}
 	return 0;
 }
diff --git a/strv.c b/strv.c
index a3e9f6a..0d37a6f 100644
--- a/strv.c
+++ b/strv.c
@@ -6,13 +6,13 @@ strv_t strv(const char *s) {
 	return (strv_t) { s, strlen(s) };
 }
 
-strv_t strv_head(strv_t ss, int chr, size_t *i) {
-	size_t j = i ? *i : 0;
-	char *c = memchr(&ss.s[j], chr, ss.n - j);
-	strv_t r = {
-		&ss.s[j],
-		c ? c - &ss.s[j] : ss.n - j
+int strv_split(strv_t *src, int chr, strv_t *dest) {
+	char *c = memchr(src->s, chr, src->n);
+	*dest = (strv_t) {
+		src->s,
+		c ? c - src->s : src->n 
 	};
-	if (i) *i = j + r.n + !!c;
-	return r;
+	src->s = c ? c + 1 : &src->s[src->n];
+	src->n -= dest->n + !!c;
+	return dest->n > 0;
 }
diff --git a/strv.h b/strv.h
index 03bbb8e..1718711 100644
--- a/strv.h
+++ b/strv.h
@@ -9,6 +9,6 @@ typedef struct {
 } strv_t;
 
 strv_t strv(const char *s);
-strv_t strv_head(strv_t ss, int chr, size_t *i);
+int strv_split(strv_t *src, int chr, strv_t *dest);
 
 #endif