summaryrefslogtreecommitdiffstats
path: root/src/lisiblestd/string.c
diff options
context:
space:
mode:
authorClement Sibille <clements+git@lisible.xyz>2024-05-09 08:58:14 +0900
committerClement Sibille <clements+git@lisible.xyz>2024-05-09 08:58:14 +0900
commit5a26e1f481fe85f08093a414e27c1056b85c6715 (patch)
tree20a2384554cfcb437a4b604e02b4a9978507cafc /src/lisiblestd/string.c
Initial commit
Diffstat (limited to 'src/lisiblestd/string.c')
-rw-r--r--src/lisiblestd/string.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/lisiblestd/string.c b/src/lisiblestd/string.c
new file mode 100644
index 0000000..3de3272
--- /dev/null
+++ b/src/lisiblestd/string.c
@@ -0,0 +1,33 @@
+#include "string.h"
+#include "assert.h"
+#include "src/lisiblestd/memory.h"
+
+#include <string.h>
+
+bool String_from_str(Allocator *allocator, String *string, const char *str) {
+ LSTD_ASSERT(allocator != NULL);
+ LSTD_ASSERT(string != NULL);
+ LSTD_ASSERT(str != NULL);
+
+ usize length = strlen(str);
+ char *value = Allocator_allocate_array(allocator, length + 1, sizeof(char));
+ if (!value) {
+ return false;
+ }
+ value[length] = '\0';
+
+ string->value = value;
+ string->length = length;
+ return true;
+}
+
+void String_destroy(Allocator *allocator, String *string) {
+ LSTD_ASSERT(allocator != NULL);
+ LSTD_ASSERT(string != NULL);
+ Allocator_free(allocator, string->value);
+}
+
+usize String_length(const String *string) {
+ LSTD_ASSERT(string != NULL);
+ return string->length;
+}
Go back to lisible.xyz