From 06ebe0aa31569c017f43e513047b1e339f52b6e6 Mon Sep 17 00:00:00 2001 From: steven-na Date: Fri, 14 Aug 2026 16:58:01 -0700 Subject: Added string view --- src/string.c | 15 +++++++++++++++ src/string.h | 25 +++++++++++++++++++++++++ src/threadpool.c | 1 - 3 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/string.c b/src/string.c index c808642..38d7824 100644 --- a/src/string.c +++ b/src/string.c @@ -3,6 +3,7 @@ #include "log.h" #include "string.h" +#include #include strng_t *strng_new(smrt_arena_t *arena, u64 size) { @@ -81,3 +82,17 @@ void strng_clear(strng_t *string) { memset((u8*)string+STRNG_BASE_POS, 0, string->len); string->len = 0; } + +strng_view_t sv_from_chars(char const* c) { u64 l = strlen(c); + return (strng_view_t){ + .start=0, + .end=l, + .max=l, + .string=c, }; } + +void sv_trim_left(strng_view_t *sv) { while (sv->start<= sv->end && isspace(*(sv->string+sv->start)) ) sv->start++; } +void sv_trim_right(strng_view_t *sv) { while (sv->end >=sv->start && isspace(*( sv->string+sv->end)) ) sv->end--; } +void sv_trim(strng_view_t *sv) { sv_trim_left(sv); + sv_trim_right(sv); } + +void sv_reset(strng_view_t *sv) { sv->end=sv->max; sv->start=0; } diff --git a/src/string.h b/src/string.h index 5a89dec..999add8 100644 --- a/src/string.h +++ b/src/string.h @@ -18,3 +18,28 @@ strng_t * strng_dup(smrt_arena_t *arena, strng_t const *src); void strng_clear(strng_t *string); #define STRNG_FMT(s) (i32)s->len, (char *)((u8*)s+STRNG_BASE_POS) + +typedef struct { + u64 start; + u64 end; + u64 max; +char const *string; +} strng_view_t; + +static inline strng_view_t sv_from(strng_t *string) { + return (strng_view_t){ + .string=(char*)((u8*)string+STRNG_BASE_POS), + .start=0, + .end=string->len, + .max=string->len, + }; +} +strng_view_t sv_from_chars(char const* c); + +void sv_trim_left(strng_view_t *sv); +void sv_trim_right(strng_view_t *sv); +void sv_trim(strng_view_t *sv); +void sv_reset(strng_view_t *sv); + +#define SV_FMT(sv) (i32)((sv)->end - (sv)->start), (char *)((u8*)((sv)->string) + (sv)->start) + diff --git a/src/threadpool.c b/src/threadpool.c index 56fcc3b..a1aa4c3 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -6,7 +6,6 @@ #include #include -#include #include typedef struct { -- cgit v1.2.3