stew

a monorepo of some sort
Log | Files | Refs

commit de5ab1c3387cb82026a13e230d3a5e3969f0921e
parent bd847631dcf10e95b70d7a357818a20b4115ed97
Author: Renev Pavel <an2qzavok@gmail.com>
Date:   Wed, 17 May 2023 09:39:20 +0000

tablist: start switching over to slices

Diffstat:
Msrc/tablist/tablist.c | 112++++++++-----------------------------------------------------------------------
Msrc/tablist/tablist.h | 10++++++----
Msrc/tablist/test.c | 2++
3 files changed, 19 insertions(+), 105 deletions(-)

diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c @@ -1,8 +1,11 @@ #include <u.h> #include <libc.h> +#include "../util/util.h" #include "tablist.h" +Type TLnodeType = {.nbytes = sizeof(TLnode *), }; + struct TLrow { int indent; TLnode *root; @@ -18,132 +21,41 @@ TLnode * allocTLnode(char *name, char *value) { TLnode *node = mallocz(sizeof(TLnode), 1); + node->children = allocslice(&TLnodeType, 0, 1); node->name = name; node->value = value; return node; } -int +void freeTLnode(TLnode *node) { - int i; free(node->name); free(node->value); - for(i = 0; i< node->count; i++) { - freeTLnode(node->children[node->count - 1]); - } + freeslice(node->children); free(node); - return 0; } int TLnodeappend(TLnode *parent, TLnode *child) { - parent->children = - realloc(parent->children, sizeof(TLnode *) * (parent->count + 1)); - parent->children[parent->count] = child; - parent->count++; - return 0; -} - -int -_tlparseline(struct TLrow *row, char *s) -{ - char *p; - TLnode **np = &row->root; - while (*s == '\t') { - row->indent++; - s++; - }; - if (*s == '\0') { - werrstr("empty line"); - return -1; - } - for (;;) { - if (*s == ' ') { - werrstr("unexpected space"); - return -1; - } - if (*np != nil) { - TLnodeappend(*np, nil); - np = &((*np)->children[(*np)->count - 1]); - } - if (*s == '\\') { - *np = allocTLnode(nil, strdup(s + 1)); - break; - } - p = strchr(s, ' '); - if (p != nil) *p = '\0'; - *np = allocTLnode(strdup(s), nil); - if (p == nil) break; - else s = p + 1; - } + sliceappendp(parent->children, 1, &child); return 0; } -TLnode * -_tlparse(char *data, usize count) -{ - struct TLparsecontext pc = { - .root = allocTLnode(nil, nil), - .n = 0, - }; - pc.stack[0] = pc.root; - int rown = 0; - char *e, *p = data; - while (p < (data + count)) { - struct TLrow row = {0, nil}; - rown++; - if (*p == '\n') { - p++; - pc.n = 0; - continue; - } - e = memchr(p, '\n', count - (p - data)); - if (e == nil) { - werrstr("%d, newline not found", rown); - free(pc.root); - return nil; - } - *e = '\0'; - if (_tlparseline(&row, p) < 0) { - werrstr("%d: %r", rown); - return nil; - } - p = e + 1; - if (row.indent == pc.n + 1) { - pc.n++; - } - if (pc.n >= row.indent) { - pc.n = row.indent; - TLnodeappend(pc.stack[pc.n], row.root); - pc.stack[pc.n + 1] = row.root; - while(pc.stack[pc.n + 1]->count != 0) { - pc.stack[pc.n + 1] = pc.stack[pc.n + 1]->children[0]; - } - } else { - werrstr("%d: too much indent", rown); - // TODO: clean up - return nil; - } - } - return pc.root; -} - TLdecoder * initTLdecoder(char *file) { TLdecoder *tlp; tlp = mallocz(sizeof(TLdecoder), 1); + tlp->nodes = allocslice(&TLnodeType, 0, 1); tlp->file = file; - // tlp->root = allocTLnode(strdup("root"), nil); return tlp; } int TLdecode(TLdecoder *tlp, char *buf, usize count) { - tlp->root = _tlparse(buf, count); return 0; } @@ -168,7 +80,6 @@ TLencode(TLencoder *w, char *buf, usize count) }; usize i; - int j; char c; for (i = 0; i < count;) { @@ -195,8 +106,7 @@ TLencode(TLencoder *w, char *buf, usize count) break; case Next: - if (w->cs[w->n] < w->np->count) { - assert(w->np->children != nil); + if (w->cs[w->n] < w->np->children->len) { w->ns[w->n + 1] = w->np->children[w->cs[w->n]]; w->cs[w->n + 1] = 0; w->n++; @@ -220,7 +130,7 @@ TLencode(TLencoder *w, char *buf, usize count) buf[i++] = c; } else { - if (w->np->count == 1) w->state = Space; + if (w->np->children->len == 1) w->state = Space; else w->state = Newline; } break; @@ -253,7 +163,7 @@ TLencode(TLencoder *w, char *buf, usize count) case Indent: if (w->i++ < w->n - 1) { - if (w->ns[w->i]->count >1) buf[i++] = '\t'; + if (w->ns[w->i]->children->len > 1) buf[i++] = '\t'; } else { w->state = Select; diff --git a/src/tablist/tablist.h b/src/tablist/tablist.h @@ -5,18 +5,20 @@ enum { TLbufsize = 1024, }; +extern Type TLnodeType; + typedef struct TLnode TLnode; struct TLnode { char *name; char *value; - TLnode **children; - int count; + Slice *children; }; typedef struct TLdecoder TLdecoder; struct TLdecoder { char *file, *buf; - TLnode *root, *stack[TLstackdepth]; + TLnode *stack[TLstackdepth]; + Slice *nodes; int sp, row, col; }; @@ -27,7 +29,7 @@ struct TLencoder { }; TLnode * allocTLnode(char *, char *); -int freeTLnode(TLnode *); +void freeTLnode(TLnode *); int TLnodeappend(TLnode *, TLnode *); TLdecoder * initTLdecoder(char *); diff --git a/src/tablist/test.c b/src/tablist/test.c @@ -1,7 +1,9 @@ #include <u.h> #include <libc.h> +#include "../util/util.h" #include "tablist.h" + void test(char *path) {