Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 71:75de5903355c | 72:4532ce6d9eb8 |
|---|---|
| 1 #include "dowa.h" | 1 #include "dowa.h" |
| 2 | 2 |
| 3 char *Dowa_Int32ToString(uint32 int32, char *buffer) | 3 #ifndef MAX_STR_BUFFER |
| 4 #define MAX_STR_BUFFER 1024 | |
| 5 #endif | |
| 6 | |
| 7 char *Dowa_String_Slice(char *from, size_t start, size_t end, Dowa_Arena *p_arena) | |
| 4 { | 8 { |
| 5 sprintf(buffer, "%d", int32); | 9 char *buffer = p_arena == NULL ? |
| 10 malloc(sizeof(char) * end-start+1) : Dowa_Arena_Allocate(p_arena, end-start+1); | |
| 11 if (!buffer) | |
| 12 return NULL; | |
| 13 | |
| 14 for (int32 i = 0; i < (end - start); i++) | |
| 15 buffer[i] = from[start + i]; | |
| 16 | |
| 17 buffer[end - start] = '\0'; | |
| 6 return buffer; | 18 return buffer; |
| 7 } | 19 } |
| 8 | 20 |
| 9 const char *Dowa_String_Slice(const char *from, size_t start, size_t end) | 21 char **Dowa_String_Split(char *from, char *token, int32 from_length, int32 token_length, Dowa_Arena *p_arena) |
| 10 { | 22 { |
| 11 static char buffer[1024] = {0}; | 23 if (!from || from[0] == '\n') |
| 12 size_t buffer_pos = 0; | 24 return NULL; |
| 13 for (int i = start; start < strlen(from) || start < end; i++) | 25 |
| 26 int32 *token_pos_arr = NULL; | |
| 27 for (int32 i = 0; i < from_length; i++) | |
| 14 { | 28 { |
| 15 buffer[buffer_pos++] = from[i]; | 29 if (from[i] == token[0]) |
| 30 { | |
| 31 int32 curr_token_pointer = 0; | |
| 32 while (curr_token_pointer < token_length && (i + curr_token_pointer) < from_length) | |
| 33 { | |
| 34 if (from[i + curr_token_pointer] != token[curr_token_pointer]) | |
| 35 break; | |
| 36 curr_token_pointer++; | |
| 37 } | |
| 38 | |
| 39 if (curr_token_pointer == token_length) | |
| 40 { | |
| 41 Dowa_Array_Push(token_pos_arr, i); | |
| 42 } | |
| 43 } | |
| 16 } | 44 } |
| 17 return buffer; | 45 |
| 46 char **splitted_strings = NULL; | |
| 47 int32 start = 0; | |
| 48 int32 num_tokens = Dowa_Array_Length(token_pos_arr); | |
| 49 | |
| 50 for (int32 i = 0; i <= num_tokens; i++) | |
| 51 { | |
| 52 int32 end = (i < num_tokens) ? token_pos_arr[i] : from_length; | |
| 53 | |
| 54 char *val = Dowa_String_Slice(from, start, end, p_arena); | |
| 55 | |
| 56 if (p_arena) | |
| 57 { | |
| 58 Dowa_Array_Push_Arena(splitted_strings, val, p_arena); | |
| 59 } | |
| 60 else | |
| 61 { | |
| 62 Dowa_Array_Push(splitted_strings, val); | |
| 63 } | |
| 64 | |
| 65 if (i < num_tokens) { | |
| 66 start = token_pos_arr[i] + token_length; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 Dowa_Array_Free(token_pos_arr); | |
| 71 return splitted_strings; | |
| 18 } | 72 } |
| 19 | 73 |
| 20 int32 Dowa_String_Pos_Find(const char *from, const char *value, const size_t from_length, const size_t value_length) | 74 int32 Dowa_String_Pos_Find(const char *from, const char *value, const size_t from_length, const size_t value_length) |
| 21 { | 75 { |
| 22 if (value == NULL || from == NULL) | 76 if (value == NULL || from == NULL) |
| 55 } | 109 } |
| 56 } | 110 } |
| 57 return NULL; | 111 return NULL; |
| 58 } | 112 } |
| 59 | 113 |
| 60 int32 Dowa_String_Pos_Find_Char(const char *from, int c, int32 from_len) | 114 int32 Dowa_String_Pos_Find_Char(const char *from, int c, int32 from_length) |
| 61 { | 115 { |
| 62 if (!from || from_len == 0) | 116 if (!from || from_length == 0) |
| 63 return -1; | 117 return -1; |
| 64 | 118 |
| 65 for (int32 i = 0; i < from_len; i++) | 119 for (int32 i = 0; i < from_length; i++) |
| 66 { | 120 { |
| 67 if ((int)from[i] == c) | 121 if ((int)from[i] == c) |
| 68 return i; | 122 return i; |
| 69 } | 123 } |
| 70 return -1; | 124 return -1; |
| 71 } | 125 } |
| 72 | 126 |
| 73 char *Dowa_String_Find_Char(const char *from, int c, int32 from_len) | 127 char *Dowa_String_Find_Char(const char *from, int c, int32 from_length) |
| 74 { | 128 { |
| 75 if (!from || from_len == 0) | 129 if (!from || from_length == 0) |
| 76 return NULL; | 130 return NULL; |
| 77 | 131 |
| 78 for (int32 i = 0; i < from_len; i++) | 132 for (int32 i = 0; i < from_length; i++) |
| 79 { | 133 { |
| 80 if ((int)from[i] == c) | 134 if ((int)from[i] == c) |
| 81 return &from[i]; | 135 return &from[i]; |
| 82 } | 136 } |
| 83 return NULL ; | 137 return NULL ; |
| 84 } | 138 } |
| 85 | 139 |
| 86 // char **Dowa_String_Split(char *from, char *split_token, int32 from_length, int32 split_token_length) | 140 char *Dowa_String_Copy_Arena(char *from, Dowa_Arena *p_arena) |
| 87 // { | 141 { |
| 88 // char *current_from = from; | 142 char *buffer = Dowa_Arena_Allocate(p_arena, sizeof(char*) * strlen(from) + 1); |
| 89 // int32 moved = 0; | 143 if (buffer); |
| 90 // while (1) | 144 memcpy(buffer, from, strlen(from) + 1); |
| 91 // { | 145 return buffer; |
| 92 // int32 next_token = Dowa_String_Pos_Find( | 146 } |
| 93 // current_from, split_token, from_length - moved, split_token_length | 147 |
| 94 // ); | |
| 95 // | |
| 96 // if (next_token == -1) | |
| 97 // { | |
| 98 // break; | |
| 99 // } | |
| 100 // char *curr = malloc(sizeof(char) * next_token); | |
| 101 // while (i < next_token) | |
| 102 // curr[i++] = from[i]; | |
| 103 // } | |
| 104 // } |