Commit d984aad
Changed files (1)
datatypes
datatypes/string.h
@@ -0,0 +1,45 @@
+#ifndef NOTLIBC_HEADER_DATATYPES_STRING_INCLUDED
+#define NOTLIBC_HEADER_DATATYPES_STRING_INCLUDED
+
+#include "../platform/platform.h"
+#include "../memory/arena.h"
+#include "platform/common/compiler.h"
+
+STRUCT( String,
+ u32 len;
+ u8 data[];
+)
+
+Status String_concat( String *s1, String *s2, String **out, Arena *a );
+Status String_concat_multiple( String **strings, usize count, String **out, Arena *a );
+
+Status String_duplicate( String *s1, String **out, Arena *a );
+
+/* On typical platforms that already have/need nul-termination we keep the nul-termination alongside the explicit length notation.
+ * Hacky, but no one wants to copy strings/paths for syscalls.
+ * NOTE: on these platforms len MUST NOT include the NUL-terminator.
+ * It is the indexable length of the string. */
+
+#if PLATFORM == PLATFORM_LINUX || PLATFORM == PLATFORM_WINDOWS
+
+#define STRING( varname, string ) \
+ struct { \
+ u64 len; \
+ u8 data[sizeof( string )]; \
+ } varname = { .len = sizeof(str)-1, .data = str }
+
+#else
+ #error String not implemented for current platform.
+
+/* For bare metal, when we get there: */
+
+#define STATIC( varname, str ) \
+ const struct { \
+ u64 len; \
+ u8 data[sizeof( str ) - 1] __attribute__((nonstring)); \
+ } varname = { .len = sizeof(str) - 1, .data = str }
+
+#endif
+
+
+#endif