skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit c9e549afa60dc3c4660e1de42a79f810299c2ade
parent 9945737a8f0612b8980d9da3db3e6a4ab8a247f4
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Fri, 25 Oct 2019 10:56:25 +0000

 Better bigkv

Diffstat:
Mpackage/deps.mak | 2+-
Msrc/include/skalibs/bigkv.h | 7++++---
Msrc/include/skalibs/bytestr.h | 2+-
Asrc/libdatastruct/bigkv_fill.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/libdatastruct/bigkv_free.c | 1+
Dsrc/libdatastruct/bigkv_init.c | 63---------------------------------------------------------------
6 files changed, 81 insertions(+), 68 deletions(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -120,8 +120,8 @@ src/libdatastruct/avltreen_delete.o src/libdatastruct/avltreen_delete.lo: src/li src/libdatastruct/avltreen_init.o src/libdatastruct/avltreen_init.lo: src/libdatastruct/avltreen_init.c src/include/skalibs/avlnode.h src/include/skalibs/avltreen.h src/include/skalibs/genset.h src/libdatastruct/avltreen_insert.o src/libdatastruct/avltreen_insert.lo: src/libdatastruct/avltreen_insert.c src/include/skalibs/avltreen.h src/libdatastruct/avltreen_newnode.o src/libdatastruct/avltreen_newnode.lo: src/libdatastruct/avltreen_newnode.c src/include/skalibs/avlnode.h src/include/skalibs/avltreen.h src/include/skalibs/genset.h +src/libdatastruct/bigkv_fill.o src/libdatastruct/bigkv_fill.lo: src/libdatastruct/bigkv_fill.c src/include/skalibs/avltree.h src/include/skalibs/bigkv.h src/include/skalibs/bytestr.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h src/libdatastruct/bigkv_free.o src/libdatastruct/bigkv_free.lo: src/libdatastruct/bigkv_free.c src/include/skalibs/avltree.h src/include/skalibs/bigkv.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h -src/libdatastruct/bigkv_init.o src/libdatastruct/bigkv_init.lo: src/libdatastruct/bigkv_init.c src/include/skalibs/avltree.h src/include/skalibs/bigkv.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h src/libdatastruct/bigkv_search.o src/libdatastruct/bigkv_search.lo: src/libdatastruct/bigkv_search.c src/include/skalibs/avltree.h src/include/skalibs/bigkv.h src/include/skalibs/genalloc.h src/libdatastruct/bigkv_zero.o src/libdatastruct/bigkv_zero.lo: src/libdatastruct/bigkv_zero.c src/include/skalibs/bigkv.h src/libdatastruct/genqdyn_clean.o src/libdatastruct/genqdyn_clean.lo: src/libdatastruct/genqdyn_clean.c src/libdatastruct/genqdyn-internal.h diff --git a/src/include/skalibs/bigkv.h b/src/include/skalibs/bigkv.h @@ -32,9 +32,10 @@ extern bigkv_t const bigkv_zero ; #define bigkv_len(b) avltree_len(&(b)->map) -extern int bigkv_init (bigkv_t *, char const *const *, char, char const *, char const *, uint32_t) ; -#define bigkv_init_argv(b, argv) bigkv_init(b, (argv), '=', "--", "--", 0) -#define bigkv_init_envp(b, envp) bigkv_init(b, (envp), '=', 0, 0, 0) +#define bigkv_init(b) (*(b) = bigkv_zero) +extern int bigkv_fill (bigkv_t *, char const *const *, char, char const *, char const *, uint32_t) ; +#define bigkv_fill_argv(b, argv) bigkv_fill(b, (argv), '=', "--", "--", 0) +#define bigkv_fill_envp(b, envp) bigkv_fill(b, (envp), '=', 0, 0, 0) extern char const *bigkv_search (bigkv_t const *, char const *) ; extern void bigkv_free (bigkv_t *) ; diff --git a/src/include/skalibs/bytestr.h b/src/include/skalibs/bytestr.h @@ -46,7 +46,7 @@ extern void case_uppers (char *) ; extern void case_upperb (char *, size_t) ; #define case_diffb(a, n, b) case_diffn(a, (b), n) #define case_equals(a, b) (!strcasecmp(a, b)) -#define case_equalb(a, n, b) (!strcasecmp(a, (b), n)) +#define case_equalb(a, n, b) (!strncasecmp(a, (b), n)) #define case_starts(s, t) case_startb(s, strlen(s), t) extern int case_startb (char const *, size_t, char const *) gccattr_pure ; extern size_t case_str (char const *, char const *) gccattr_pure ; diff --git a/src/libdatastruct/bigkv_fill.c b/src/libdatastruct/bigkv_fill.c @@ -0,0 +1,74 @@ +/* ISC license. */ + +#include <stdint.h> +#include <string.h> +#include <errno.h> + +#include <skalibs/bytestr.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/avltree.h> +#include <skalibs/bigkv.h> + +static void *bigkv_dtok (uint32_t d, void *p) +{ + bigkv_t *b = p ; + return b->storage.s + genalloc_s(bigkv_node_t, &b->nodes)[d].k ; +} + +static int bigkv_cmp (void const *a, void const *b, void *p) +{ + (void)p ; + return strcmp((char const *)a, (char const *)b) ; +} + +int bigkv_fill (bigkv_t *b, char const *const *argv, char delim, char const *prefix, char const *stop, uint32_t options) +{ + int i = 0 ; + size_t prefixlen = prefix ? strlen(prefix) : 0 ; + avltree_init(&b->map, 3, 3, 8, &bigkv_dtok, &bigkv_cmp, b) ; + for (; argv[i] && !(stop && !strcmp(argv[i], stop)) ; i++) + { + bigkv_node_t node = { .k = b->storage.len } ; + char const *s = argv[i] ; + size_t len = strlen(s) ; + size_t pos ; + int isdup ; + uint32_t d ; + if (prefixlen) + { + if (!strncmp(s, prefix, prefixlen)) return i+1 ; + s += prefixlen ; + len -= prefixlen ; + } + pos = byte_chr(s, len, delim) ; + if (!stralloc_catb(&b->storage, s, pos+1)) goto err ; + b->storage.s[pos] = 0 ; + isdup = avltree_search(&b->map, s, &d) ; + if (isdup) + { + if (options & BIGKV_OPTIONS_NODUP) goto invalid ; + b->storage.len = node.k ; + } + if (pos < len) + { + node.v = b->storage.len ; + if (!stralloc_catb(&b->storage, s + pos + 1, len - pos)) goto err ; + } + else node.v = b->storage.len - 1 ; + if (isdup) genalloc_s(bigkv_node_t, &b->nodes)[d].v = node.v ; + else + { + d = genalloc_len(bigkv_node_t, &b->nodes) ; + if (!genalloc_append(bigkv_node_t, &b->nodes, &node)) goto err ; + if (!avltree_insert(&b->map, d)) goto err ; + } + } + return i ; + + invalid: + errno = EINVAL ; + err: + bigkv_free(b) ; + return -1 ; +} diff --git a/src/libdatastruct/bigkv_free.c b/src/libdatastruct/bigkv_free.c @@ -10,4 +10,5 @@ void bigkv_free (bigkv_t *b) avltree_free(&b->map) ; genalloc_free(bigkv_node_t, &b->nodes) ; stralloc_free(&b->storage) ; + *b = bigkv_zero ; } diff --git a/src/libdatastruct/bigkv_init.c b/src/libdatastruct/bigkv_init.c @@ -1,63 +0,0 @@ -/* ISC license. */ - -#include <stdint.h> -#include <string.h> -#include <errno.h> - -#include <skalibs/stralloc.h> -#include <skalibs/genalloc.h> -#include <skalibs/avltree.h> -#include <skalibs/bigkv.h> - -static void *bigkv_dtok (uint32_t d, void *p) -{ - bigkv_t *b = p ; - return b->storage.s + genalloc_s(bigkv_node_t, &b->nodes)[d].k ; -} - -static int bigkv_cmp (void const *a, void const *b, void *p) -{ - (void)p ; - return strcmp((char const *)a, (char const *)b) ; -} - -int bigkv_init (bigkv_t *b, char const *const *argv, char delim, char const *prefix, char const *stop, uint32_t options) -{ - size_t prefixlen = prefix ? strlen(prefix) : 0 ; - avltree_init(&b->map, 3, 3, 8, &bigkv_dtok, &bigkv_cmp, b) ; - for (; *argv && !(stop && !strcmp(*argv, stop)) ; argv++) - { - bigkv_node_t node = { .k = b->storage.len } ; - uint32_t d ; - char const *s = *argv ; - char *p ; - if (prefixlen) - { - if (!strncmp(s, prefix, prefixlen)) break ; - s += prefixlen ; - } - if (avltree_search(&b->map, s, &d)) - { - if (options & BIGKV_OPTIONS_NODUP) goto invalid ; - else avltree_delete(&b->map, s) ; - } - if (!stralloc_cats(&b->storage, s) || !stralloc_0(&b->storage)) goto err ; - p = strchr(s, delim) ; - if (p) - { - node.v = node.k + (p - s) + 1 ; - b->storage.s[node.k + (p - s)] = 0 ; - } - else node.v = b->storage.len - 1 ; - d = genalloc_len(bigkv_node_t, &b->nodes) ; - if (!genalloc_append(bigkv_node_t, &b->nodes, &node)) goto err ; - if (!avltree_insert(&b->map, d)) goto err ; - } - return 1 ; - - invalid: - errno = EINVAL ; - err: - bigkv_free(b) ; - return 0 ; -}