be

based editor
git clone git://git.kocotian.pl/be.git
Log | Files | Refs | README | LICENSE

commit 57c7bae412c408c87ea8d9327c8ab26d8bda4715
parent c3bfebc9792317308e786c589d52fc1fb727294a
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat, 17 Apr 2021 21:40:20 +0200

libStr

Diffstat:
MMakefile | 2+-
Astr.c | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astr.h | 40++++++++++++++++++++++++++++++++++++++++
3 files changed, 125 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ include config.mk -SRC = util.c +SRC = util.c str.c OBJ = ${SRC:.c=.o} .c.o: diff --git a/str.c b/str.c @@ -0,0 +1,84 @@ +/* See COPYRIGHT file for copyright and license details */ + +#include "str.h" + +/* String functions */ + +String +toString(char *s) +{ + String ret; + ret.len = strlen(ret.data = s); + return ret; +} + +int +Strcmp(String a, String b) +{ + if (a.len != b.len) return -1; + 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) +{ + char *tmpptr = string.data; + + if (!string.len) return *out = string, 0; + if (*(tmpptr) != c) + while ((unsigned)(tmpptr - string.data) <= string.len) { + if (*(++tmpptr) == c) break; + } + string.len = ++tmpptr - string.data; + *out = string; + return (tmpptr - string.data); +} + +String +Striden(String str) +{ + size_t i; + for (i = 0; ((str.data[i] >= 'a' && str.data[i] <= 'z') + || (str.data[i] >= 'A' && str.data[i] <= 'Z') + || (str.data[i] && str.data[i] >= '0' && str.data[i] <= '9')) + && (i < str.len); ++i); + str.len = i; + return str; +} + +String +Strtrim(String str) +{ + size_t i = 0; + while (isspace(*(str.data)) && i < str.len) { + ++str.data; + --str.len; + ++i; + } + while (isspace(*(str.data + (str.len - 1)))) + --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 @@ -0,0 +1,40 @@ +/* See COPYRIGHT file for copyright and license details */ + +#ifndef _STR_H +#define _STR_H + +#include <ctype.h> +#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