summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-14 16:58:01 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-14 16:58:01 -0700
commit06ebe0aa31569c017f43e513047b1e339f52b6e6 (patch)
treeda6690e4905708139e20a20edc6c7fd5c1a7c999
parentf10afce9821a81c8dd138b84a4f1ab56116af20b (diff)
Added string view
-rw-r--r--README.md2
-rw-r--r--src/string.c15
-rw-r--r--src/string.h25
-rw-r--r--src/threadpool.c1
4 files changed, 41 insertions, 2 deletions
diff --git a/README.md b/README.md
index d5130a3..0a2ff64 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@
- [x] threadpool
- [x] vec2sw
- [x] wav
-- [ ] Add strng_view
+- [x] Add strng_view
- want start, end, max_len, char*
- [x] Make status return values consistent. i64, 0 success, -1 fail, >0 info if necessary
- [x] Deque enqueue (both)
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 <ctype.h>
#include <string.h>
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 <bits/pthreadtypes.h>
#include <pthread.h>
-#include <stdatomic.h>
#include <string.h>
typedef struct {