aboutsummaryrefslogtreecommitdiff
path: root/src/un_strings.c
diff options
context:
space:
mode:
authorbonmas14 <bonmas14@gmail.com>2025-09-25 14:17:07 +0300
committerbonmas14 <bonmas14@gmail.com>2025-09-25 14:17:07 +0300
commit166877cf15f6afa89c9f8a61e267d485868d0ee1 (patch)
tree9f080314edc4fb293f91227cdc1c01cfec8d7bae /src/un_strings.c
parent8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (diff)
downloadungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.tar.gz
ungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.zip
+disgrace and rework of ungrateful.h
Diffstat (limited to 'src/un_strings.c')
-rw-r--r--src/un_strings.c53
1 files changed, 25 insertions, 28 deletions
diff --git a/src/un_strings.c b/src/un_strings.c
index ff74c39..cf4fee8 100644
--- a/src/un_strings.c
+++ b/src/un_strings.c
@@ -1,19 +1,11 @@
-u64 un_string_get_length(u8 *cstring) {
- u8* start = cstring;
-
- while (*start != '\0') {
- start++;
- }
-
- return start - cstring;
-}
-
-String un_string_from_cstring(u8* cstring) {
+String un_string_from_cstring(char *cstring) {
String result;
- u64 length = un_string_get_length(cstring);
+ u64 length;
+
+ length = strlen(cstring);
assert(length < LLONG_MAX);
result.size = length;
- result.data = cstring;
+ result.data = (u8*)cstring;
return result;
}
@@ -23,7 +15,8 @@ u8* un_string_to_cstring(String string, Allocator alloc) {
assert(string.size > 0);
mem = (u8*)un_memory_alloc(string.size + 1, alloc);
- un_memory_copy(mem, string.data, (u64)string.size);
+ assert(mem != NULL);
+ memcpy(mem, string.data, (u64)string.size);
return mem;
}
@@ -33,7 +26,8 @@ String un_string_copy(String source, Allocator alloc) {
String result;
mem = (u8*)un_memory_alloc(source.size, alloc);
- un_memory_copy(mem, source.data, (u64)source.size);
+ assert(mem != NULL);
+ memcpy(mem, source.data, (u64)source.size);
result.size = source.size;
result.data = mem;
@@ -48,7 +42,7 @@ s32 un_string_compare(String left, String right) {
if (left.size == 0) return -1;
if (right.size == 0) return 1;
- result = un_memory_compare(left.data, right.data, left.size);
+ result = memcmp(left.data, right.data, left.size);
if (result == 0) {
if (left.size > right.size) return 1;
@@ -67,15 +61,16 @@ String un_string_concat(String left, String right, Allocator alloc) {
}
u8* data = (u8*)un_memory_alloc(left.size + right.size, alloc);
+ assert(data != NULL);
if (left.size > 0) {
assert(left.data != NULL);
- un_memory_copy(data, left.data, left.size);
+ memcpy(data, left.data, left.size);
}
if (right.size > 0) {
assert(right.data != NULL);
- un_memory_copy((data + left.size), right.data, right.size);
+ memcpy((data + left.size), right.data, right.size);
}
result.size = left.size + right.size;
@@ -118,7 +113,7 @@ List un_string_split(String input, String pattern, Allocator alloc) {
}
for (i = 0, matches = 1; i < (input.size - (pattern.size - 1)); i++) {
- if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) {
+ if (memcmp(input.data + i, pattern.data, pattern.size) != 0) {
continue;
}
@@ -130,7 +125,7 @@ List un_string_split(String input, String pattern, Allocator alloc) {
start = 0;
for (i = 0; i < (input.size - (pattern.size - 1)); i++) {
- if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) {
+ if (memcmp(input.data + i, pattern.data, pattern.size) != 0) {
continue;
}
@@ -142,14 +137,16 @@ List un_string_split(String input, String pattern, Allocator alloc) {
}
buffer = (u8*)un_memory_alloc(size, alloc);
- un_memory_copy(buffer, input.data + start, size);
+ assert(buffer != NULL);
+
+ memcpy(buffer, input.data + start, size);
string = CLITERAL(String) { .size = size, .data = buffer };
un_list_append(&splits, &string);
start = i + pattern.size;
}
if (start != input.size - pattern.size) {
- if (un_memory_compare(input.data + start, pattern.data, pattern.size) == 0) {
+ if (memcmp(input.data + start, pattern.data, pattern.size) == 0) {
return splits;
}
@@ -159,7 +156,8 @@ List un_string_split(String input, String pattern, Allocator alloc) {
}
buffer = (u8*)un_memory_alloc(size, alloc);
- un_memory_copy(buffer, input.data + start, size);
+ assert(buffer != NULL);
+ memcpy(buffer, input.data + start, size);
string = CLITERAL(String) { .size = size, .data = buffer };
un_list_append(&splits, &string);
}
@@ -174,11 +172,10 @@ String un_string_join(List string_list, String separator, Allocator alloc) {
UN_CLEAR(cont);
- talloc = un_allocator_get_temporary();
+ talloc = un_alloc_temp_get();
assert(string_list.element_size == sizeof(String));
-
for (i = 0; i < string_list.count; i++) {
temp = *(String *)un_list_get(&string_list, i);
@@ -267,7 +264,7 @@ static String format_u64(u64 value) {
output.size = size;
output.data = buffer;
- return un_string_copy(output, un_allocator_get_temporary());
+ return un_string_copy(output, un_alloc_temp_get());
}
static String format_s64(s64 value) {
@@ -309,7 +306,7 @@ static String format_s64(s64 value) {
output.size = size;
output.data = buffer;
- return un_string_copy(output, un_allocator_get_temporary());
+ return un_string_copy(output, un_alloc_temp_get());
}
String un_string_format(Allocator alloc, String buffer, ...) {
@@ -327,7 +324,7 @@ String un_string_vformat(Allocator alloc, String buffer, va_list args) {
u64 i, j;
u32 b;
- Allocator talloc = un_allocator_get_temporary();
+ Allocator talloc = un_alloc_temp_get();
// @todo @bug: this would fail on realloc, because we need to realloc
output = un_list_create(UN_KB(1), sizeof(u8), talloc);