improve string's vec use

This commit is contained in:
sfja 2025-03-19 21:20:09 +01:00
parent 1434772b35
commit a99992d9d4
2 changed files with 13 additions and 7 deletions

View File

@ -52,14 +52,17 @@ StrSlice str_split_next(StrSplitter* splitter)
return slice; return slice;
} }
DEFINE_VEC_IMPL(char, String, string_data, )
int string_construct(String* string) int string_construct(String* string)
{ {
int res = string_data_construct(string); int res = string_data_construct(string);
if (res != 0) { if (res != 0)
return res; return res;
} res = string_data_push(string, '\0');
// NOTE: Vec is assumed to be initialized with atleast 1 allocated byte. if (res != 0)
string->data[0] = '\0'; return res;
string->size -= 1;
return 0; return 0;
} }
@ -82,7 +85,8 @@ int string_push(String* string, char value)
int string_push_str(String* string, const char* str) int string_push_str(String* string, const char* str)
{ {
for (size_t i = 0; i < strlen(str); ++i) { size_t len = strlen(str);
for (size_t i = 0; i < len; ++i) {
int res = string_data_push(string, str[i]); int res = string_data_push(string, str[i]);
if (res != 0) if (res != 0)
return res; return res;
@ -120,6 +124,8 @@ char* string_copy(const String* string)
return copy; return copy;
} }
DEFINE_VEC_IMPL(char*, RawStrVec, rawstr_vec, )
#define STR_HASH_SALT_SIZE 32 #define STR_HASH_SALT_SIZE 32
#define STR_HASH_HASH_SIZE 32 #define STR_HASH_HASH_SIZE 32
#define STR_HASH_STR_LEN 128 #define STR_HASH_STR_LEN 128

View File

@ -25,7 +25,7 @@ typedef struct {
StrSplitter str_splitter(const char* text, size_t text_len, const char* split); StrSplitter str_splitter(const char* text, size_t text_len, const char* split);
StrSlice str_split_next(StrSplitter* splitter); StrSlice str_split_next(StrSplitter* splitter);
DEFINE_VEC(char, String, string_data) DECLARE_VEC_TYPE(char, String, string_data, )
int string_construct(String* string); int string_construct(String* string);
void string_destroy(String* string); void string_destroy(String* string);
@ -36,7 +36,7 @@ char* string_copy(const String* string);
#define string_pushf(STRING, ...) string_push_fmt_va(STRING, __VA_ARGS__) #define string_pushf(STRING, ...) string_push_fmt_va(STRING, __VA_ARGS__)
DEFINE_VEC(char*, RawStrVec, rawstr_vec) DECLARE_VEC_TYPE(char*, RawStrVec, rawstr_vec, )
#define MAX_HASH_INPUT_LEN 256 - 1 #define MAX_HASH_INPUT_LEN 256 - 1