Mercurial
diff dowa/d_string.c @ 72:4532ce6d9eb8
[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 29 Dec 2025 07:50:07 -0800 |
| parents | 75de5903355c |
| children | 655ea0b661fd |
line wrap: on
line diff
--- a/dowa/d_string.c Sun Dec 28 20:34:22 2025 -0800 +++ b/dowa/d_string.c Mon Dec 29 07:50:07 2025 -0800 @@ -1,20 +1,74 @@ #include "dowa.h" -char *Dowa_Int32ToString(uint32 int32, char *buffer) +#ifndef MAX_STR_BUFFER + #define MAX_STR_BUFFER 1024 +#endif + +char *Dowa_String_Slice(char *from, size_t start, size_t end, Dowa_Arena *p_arena) { - sprintf(buffer, "%d", int32); + char *buffer = p_arena == NULL ? + malloc(sizeof(char) * end-start+1) : Dowa_Arena_Allocate(p_arena, end-start+1); + if (!buffer) + return NULL; + + for (int32 i = 0; i < (end - start); i++) + buffer[i] = from[start + i]; + + buffer[end - start] = '\0'; return buffer; } -const char *Dowa_String_Slice(const char *from, size_t start, size_t end) -{ - static char buffer[1024] = {0}; - size_t buffer_pos = 0; - for (int i = start; start < strlen(from) || start < end; i++) +char **Dowa_String_Split(char *from, char *token, int32 from_length, int32 token_length, Dowa_Arena *p_arena) +{ + if (!from || from[0] == '\n') + return NULL; + + int32 *token_pos_arr = NULL; + for (int32 i = 0; i < from_length; i++) { - buffer[buffer_pos++] = from[i]; + if (from[i] == token[0]) + { + int32 curr_token_pointer = 0; + while (curr_token_pointer < token_length && (i + curr_token_pointer) < from_length) + { + if (from[i + curr_token_pointer] != token[curr_token_pointer]) + break; + curr_token_pointer++; + } + + if (curr_token_pointer == token_length) + { + Dowa_Array_Push(token_pos_arr, i); + } + } } - return buffer; + + char **splitted_strings = NULL; + int32 start = 0; + int32 num_tokens = Dowa_Array_Length(token_pos_arr); + + for (int32 i = 0; i <= num_tokens; i++) + { + int32 end = (i < num_tokens) ? token_pos_arr[i] : from_length; + + char *val = Dowa_String_Slice(from, start, end, p_arena); + + if (p_arena) + { + Dowa_Array_Push_Arena(splitted_strings, val, p_arena); + } + else + { + Dowa_Array_Push(splitted_strings, val); + } + + if (i < num_tokens) { + start = token_pos_arr[i] + token_length; + } + } + + Dowa_Array_Free(token_pos_arr); + return splitted_strings; } int32 Dowa_String_Pos_Find(const char *from, const char *value, const size_t from_length, const size_t value_length) @@ -57,12 +111,12 @@ return NULL; } -int32 Dowa_String_Pos_Find_Char(const char *from, int c, int32 from_len) +int32 Dowa_String_Pos_Find_Char(const char *from, int c, int32 from_length) { - if (!from || from_len == 0) + if (!from || from_length == 0) return -1; - for (int32 i = 0; i < from_len; i++) + for (int32 i = 0; i < from_length; i++) { if ((int)from[i] == c) return i; @@ -70,12 +124,12 @@ return -1; } -char *Dowa_String_Find_Char(const char *from, int c, int32 from_len) +char *Dowa_String_Find_Char(const char *from, int c, int32 from_length) { - if (!from || from_len == 0) + if (!from || from_length == 0) return NULL; - for (int32 i = 0; i < from_len; i++) + for (int32 i = 0; i < from_length; i++) { if ((int)from[i] == c) return &from[i]; @@ -83,22 +137,11 @@ return NULL ; } -// char **Dowa_String_Split(char *from, char *split_token, int32 from_length, int32 split_token_length) -// { -// char *current_from = from; -// int32 moved = 0; -// while (1) -// { -// int32 next_token = Dowa_String_Pos_Find( -// current_from, split_token, from_length - moved, split_token_length -// ); -// -// if (next_token == -1) -// { -// break; -// } -// char *curr = malloc(sizeof(char) * next_token); -// while (i < next_token) -// curr[i++] = from[i]; -// } -// } +char *Dowa_String_Copy_Arena(char *from, Dowa_Arena *p_arena) +{ + char *buffer = Dowa_Arena_Allocate(p_arena, sizeof(char*) * strlen(from) + 1); + if (buffer); + memcpy(buffer, from, strlen(from) + 1); + return buffer; +} +