From 5d66c96a190a396a1535c89bed4e33c2a005fe8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Thu, 24 Mar 2022 01:04:02 +0300 Subject: Initial commit Basically it works, just needs some polishing and maybe a couple of features that I could actually use. Also probably better docs. Not sure if it will be of use to anybody besides me. --- include/slice.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 include/slice.h (limited to 'include/slice.h') diff --git a/include/slice.h b/include/slice.h new file mode 100644 index 0000000..add82d5 --- /dev/null +++ b/include/slice.h @@ -0,0 +1,37 @@ +#ifndef CMONKEY_SLICE_H +#define CMONKEY_SLICE_H + +#include "sds/sds.h" + +#include +#include + +/* A slice of a C string */ +struct slice { + const char *str; + size_t start; + size_t end; +}; + +/* Create a new slice from an existing string indicating its bounds */ +struct slice slice_new(const char *str, size_t start, size_t end); + +/* Create a new slice from a string literal */ +#define slice_whole(s) (struct slice){ s, 0, strlen(s), } + +/* Set a slice to a new string and bounds */ +void slice_set(struct slice *, const char *str, size_t start, size_t end); + +/* Get the length of the slice */ +size_t slice_len(const struct slice *); + +/* Returns 0 if equal, 1 if a > b, -1 if a < b */ +int slice_cmp(const struct slice *restrict a, const struct slice *restrict b); + +/* Copy the slice from src to dst; dst should already be allocated */ +void slice_cpy(struct slice *dst, const struct slice *src); + +/* Concatenate the slice to an SDS string */ +sds slice_string(const struct slice *, sds str); + +#endif -- cgit v1.2.3