summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorCarson Fleming <[email protected]>2026-01-25 03:49:51 -0500
committerCarson Fleming <[email protected]>2026-01-25 03:49:51 -0500
commitaecec4204b5830fcb97ab93a56a6a29018519ca2 (patch)
tree0088101660e2160b6ab13d96f537d6f66e4f31b0 /array.c
parenteaba8182af646204294546a10a84e8f8165ba589 (diff)
downloadsafec-aecec4204b5830fcb97ab93a56a6a29018519ca2.tar.gz
more intuitive interface
Diffstat (limited to 'array.c')
-rw-r--r--array.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/array.c b/array.c
index c7df5b5..d8765c1 100644
--- a/array.c
+++ b/array.c
@@ -1,29 +1,39 @@
#include "array.h"
#include "crash.h"
+#include <string.h>
+
#define CRASH_IF_OOB(array, idx) {\
if (idx >= array->length)\
crash("Array access out of bounds: %ld >= %ld\n", idx, array->length);\
}
-void array_set(array_t* array, size_t idx, void* val) {
- CRASH_IF_OOB(array, idx);
- char* data_ptr = (char*)array->__data + idx*array->elemsz;
- for (size_t i = 0; i < array->elemsz; i++) {
- data_ptr[i] = *((char*)val + i);
- }
-}
-
-void* array_get(const array_t* array, size_t idx) {
+void* array_at(const array_t* array, size_t idx) {
CRASH_IF_OOB(array, idx);
return (char*)array->__data + idx*array->elemsz;
}
-void str_set(str_t* str, size_t idx, char val) {
+char* str_at(const str_t* str, size_t idx) {
CRASH_IF_OOB(str, idx);
- str->__data[idx] = val;
+ return str->__data + idx;
}
-char str_get(const str_t* str, size_t idx) {
- CRASH_IF_OOB(str, idx);
- return str->__data[idx];
+str_t str_slice(const str_t* str, size_t start, size_t length) {
+ CRASH_IF_OOB(str, start + length - 1);
+ str_t slice = {.length = length, .__data = str->__data + start};
+ return slice;
+}
+
+void str_c_str(
+ char* dst,
+ size_t dst_size,
+ const str_t* src
+) {
+ if (src->length >= dst_size)
+ crash(
+ "String is too long to hold in C string buffer: %ld >= %ld\n",
+ src->length,
+ dst_size);
+
+ memmove(dst, src->__data, src->length);
+ dst[src->length] = 0;
}