From 5a26e1f481fe85f08093a414e27c1056b85c6715 Mon Sep 17 00:00:00 2001 From: Clement Sibille Date: Thu, 9 May 2024 08:58:14 +0900 Subject: Initial commit --- src/lisiblestd/string.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/lisiblestd/string.c (limited to 'src/lisiblestd/string.c') 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 + +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; +} -- cgit v1.2.3