stacinhtml

Static C in HTML - simple static site generator
git clone git://git.kocotian.pl/stacinhtml.git
Log | Files | Refs | README | LICENSE

commit 4bae6fb08b09fdf92a3acca6b6a5bae11d7c6a08
parent 3459306cb7ac979891f0b3749da3edcd7c885094
Author: kocotian <kocotian@kocotian.pl>
Date:   Tue, 13 Apr 2021 14:23:49 +0200

Arrays and Vectors, Strcmpc() function

Diffstat:
Mconfig.mk | 2+-
Mstr.c | 25+++++++++++++++++++++++++
Mstr.h | 19+++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/config.mk b/config.mk @@ -1,7 +1,7 @@ # basics MAJORVERSION = 0 SUBVERSION = 2 -PATCHLEVEL = 1 +PATCHLEVEL = 2 BUILDNAME = vanilla VERSION = ${MAJORVERSION}.${SUBVERSION}.${PATCHLEVEL}-${BUILDNAME} diff --git a/str.c b/str.c @@ -20,6 +20,8 @@ #include "str.h" +/* String functions */ + String toString(char *s) { @@ -35,6 +37,13 @@ Strcmp(String a, String b) return strncmp(a.data, b.data, a.len); } +int +Strcmpc(String s1, char *s2) +{ + if (s1.len != strlen(s2)) return -1; + return strncmp(s1.data, s2, s1.len); +} + ssize_t Strtok(String string, String *out, char c) { @@ -75,3 +84,19 @@ Strtrim(String str) --str.len; return str; } + +/* Array functions */ +int +_inArray(char *data, size_t len, void *val, size_t vlen) +{ + size_t i, n; + for (n = i = 0; i < len; ++i, data += vlen) + if (!memcmp(data, val, vlen)) ++n; + return (int)n; +} + +void * +_prepareArray(void *data, size_t siz) +{ + return memset(data, 0, siz); +} diff --git a/str.h b/str.h @@ -25,15 +25,34 @@ #include <sys/types.h> #include <string.h> +/* Types and pseudo-types */ typedef struct { char *data; size_t len; } String; +#define Array(TYPE) struct { TYPE *data; size_t len; } + +/* String functions */ String toString(char *s); int Strcmp(String a, String b); +int Strcmpc(String s1, char *s2); ssize_t Strtok(String string, String *out, char c); String Striden(String string); String Strtrim(String str); +/* Array functions */ +int _inArray(char *data, size_t len, void *val, size_t vlen); +#define inArray(ARR, VAL) (_inArray( \ + (void *)(ARR).data, (ARR).len, &(VAL), sizeof (VAL))) +void *_prepareArray(void *data, size_t siz); +#define prepareArray(ARR) (_prepareArray(&(ARR), sizeof (ARR))) + +/* Vector - dynamic Array */ +#define newVector(ARR) ((ARR).data = malloc((ARR).len = 0)) +#define pushVector(ARR, VAL) (((ARR).data = \ + realloc((ARR).data, \ + ++((ARR).len) * (sizeof *((ARR).data)))), \ + (ARR).data[(ARR).len - 1] = (VAL)) + #endif