summary refs log tree commit diff
path: root/parse.c
diff options
context:
space:
mode:
authorwrmr2024-11-08 23:57:30 -0500
committerwrmr2024-11-08 23:57:30 -0500
commit9ac1ca968081bcb826cf9ee70666bc44983d087f (patch)
tree1d130f91c53c41f5a6f18d357fb4fa96c3088883 /parse.c
parentcd3626642755359eecf64d05e3f9db3d8b00ed24 (diff)
simplify strv_head interface, rename to strv_split
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c27
1 files changed, 10 insertions, 17 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;
 }