summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/string.c29
-rw-r--r--src/string.h4
2 files changed, 28 insertions, 5 deletions
diff --git a/src/string.c b/src/string.c
index be96c63..9328911 100644
--- a/src/string.c
+++ b/src/string.c
@@ -18,8 +18,31 @@ strng_t *strng_new(smrt_arena_t *arena, u64 size) {
strng_t *strng_from(smrt_arena_t *arena, char *c) {
u64 size = strlen(c);
- return strng_new(arena, size);
+ strng_t *s = strng_new(arena, size);
+
+ if (!s) {
+ return NULL;
+ }
+
+ memcpy((u8*)s+STRNG_BASE_POS, c, size);
+ s->len = size;
+
+ return s;
}
- b32 strng_set(strng_t *string, char *c);
- void strng_clear(strng_t *string);
+b32 strng_set(strng_t *string, char *c) {
+ u64 size = strlen(c);
+
+ if (string->alloc_size < size) {
+ return false;
+ }
+
+ memcpy((u8*)string+STRNG_BASE_POS, c, size);
+ string->len = size;
+
+ return true;
+}
+
+void strng_clear(strng_t *string) {
+ string->len = 0;
+}
diff --git a/src/string.h b/src/string.h
index 1b5f136..b8554ff 100644
--- a/src/string.h
+++ b/src/string.h
@@ -3,7 +3,7 @@
#include "common.h"
#include "smrt_arena.h"
-#define STRNG_BASE_POS (sizeof(ez_arena_t))
+#define STRNG_BASE_POS (sizeof(strng_t))
typedef struct {
u64 alloc_size;
@@ -15,4 +15,4 @@ strng_t *strng_from(smrt_arena_t *arena, char *c);
b32 strng_set(strng_t *string, char *c);
void strng_clear(strng_t *string);
-#define STRNG_FMT(s) (int)s->len, (s+STRNG_BASE_POS)
+#define STRNG_FMT(s) (int)s->len, (char *)((u8*)s+STRNG_BASE_POS)