move more strbuilder functions to cpp

This commit is contained in:
Edward R. Gonzalez 2025-02-04 14:53:26 -05:00
parent b6d95e8d70
commit 5c17983a73
2 changed files with 85 additions and 87 deletions

View File

@ -95,4 +95,83 @@ bool strbuilder_make_space_for(StrBuilder* str, char const* to_append, ssize add
} }
} }
bool strbuilder_append_c_str_len(StrBuilder* str, char const* c_str_to_append, ssize append_length)
{
GEN_ASSERT(str != nullptr);
if ( rcast(sptr, c_str_to_append) > 0)
{
ssize curr_len = strbuilder_length(* str);
if ( ! strbuilder_make_space_for(str, c_str_to_append, append_length))
return false;
StrBuilderHeader* header = strbuilder_get_header(* str);
char* Data = * str;
mem_copy( Data + curr_len, c_str_to_append, append_length);
Data[curr_len + append_length] = '\0';
header->Length = curr_len + append_length;
}
return c_str_to_append != nullptr;
}
void strbuilder_trim(StrBuilder str, char const* cut_set)
{
ssize len = 0;
char* start_pos = str;
char* end_pos = scast(char*, str) + strbuilder_length(str) - 1;
while (start_pos <= end_pos && char_first_occurence(cut_set, *start_pos))
start_pos++;
while (end_pos > start_pos && char_first_occurence(cut_set, *end_pos))
end_pos--;
len = scast(ssize, (start_pos > end_pos) ? 0 : ((end_pos - start_pos) + 1));
if (str != start_pos)
mem_move(str, start_pos, len);
str[len] = '\0';
strbuilder_get_header(str)->Length = len;
}
StrBuilder strbuilder_visualize_whitespace(StrBuilder const str)
{
StrBuilderHeader* header = (StrBuilderHeader*)(scast(char const*, str) - sizeof(StrBuilderHeader));
StrBuilder result = strbuilder_make_reserve(header->Allocator, strbuilder_length(str) * 2); // Assume worst case for space requirements.
for (char const* c = strbuilder_begin(str); c != strbuilder_end(str); c = strbuilder_next(str, c))
switch ( * c )
{
case ' ':
strbuilder_append_str(& result, txt("·"));
break;
case '\t':
strbuilder_append_str(& result, txt(""));
break;
case '\n':
strbuilder_append_str(& result, txt(""));
break;
case '\r':
strbuilder_append_str(& result, txt(""));
break;
case '\v':
strbuilder_append_str(& result, txt(""));
break;
case '\f':
strbuilder_append_str(& result, txt(""));
break;
default:
strbuilder_append_char(& result, * c);
break;
}
return result;
}
#pragma endregion StrBuilder #pragma endregion StrBuilder

View File

@ -122,9 +122,12 @@ struct StrBuilder;
forceinline usize strbuilder_grow_formula(usize value); forceinline usize strbuilder_grow_formula(usize value);
GEN_API StrBuilder strbuilder_make_reserve (AllocatorInfo allocator, ssize capacity); GEN_API StrBuilder strbuilder_make_reserve (AllocatorInfo allocator, ssize capacity);
GEN_API StrBuilder strbuilder_make_length (AllocatorInfo allocator, char const* str, ssize length); GEN_API StrBuilder strbuilder_make_length (AllocatorInfo allocator, char const* str, ssize length);
GEN_API bool strbuilder_make_space_for(StrBuilder* str, char const* to_append, ssize add_len); GEN_API bool strbuilder_make_space_for (StrBuilder* str, char const* to_append, ssize add_len);
GEN_API bool strbuilder_append_c_str_len (StrBuilder* str, char const* c_str_to_append, ssize length);
GEN_API void strbuilder_trim (StrBuilder str, char const* cut_set);
GEN_API StrBuilder strbuilder_visualize_whitespace(StrBuilder const str);
StrBuilder strbuilder_make_c_str (AllocatorInfo allocator, char const* str); StrBuilder strbuilder_make_c_str (AllocatorInfo allocator, char const* str);
StrBuilder strbuilder_make_str (AllocatorInfo allocator, Str str); StrBuilder strbuilder_make_str (AllocatorInfo allocator, Str str);
@ -135,7 +138,6 @@ bool strbuilder_are_equal (StrBuilder const lhs, StrBuild
bool strbuilder_are_equal_str (StrBuilder const lhs, Str rhs); bool strbuilder_are_equal_str (StrBuilder const lhs, Str rhs);
bool strbuilder_append_char (StrBuilder* str, char c); bool strbuilder_append_char (StrBuilder* str, char c);
bool strbuilder_append_c_str (StrBuilder* str, char const* c_str_to_append); bool strbuilder_append_c_str (StrBuilder* str, char const* c_str_to_append);
bool strbuilder_append_c_str_len (StrBuilder* str, char const* c_str_to_append, ssize length);
bool strbuilder_append_str (StrBuilder* str, Str c_str_to_append); bool strbuilder_append_str (StrBuilder* str, Str c_str_to_append);
bool strbuilder_append_string (StrBuilder* str, StrBuilder const other); bool strbuilder_append_string (StrBuilder* str, StrBuilder const other);
bool strbuilder_append_fmt (StrBuilder* str, char const* fmt, ...); bool strbuilder_append_fmt (StrBuilder* str, char const* fmt, ...);
@ -154,9 +156,7 @@ b32 strbuilder_starts_with_string (StrBuilder const str, StrBuild
void strbuilder_skip_line (StrBuilder str); void strbuilder_skip_line (StrBuilder str);
void strbuilder_strip_space (StrBuilder str); void strbuilder_strip_space (StrBuilder str);
Str strbuilder_to_str (StrBuilder str); Str strbuilder_to_str (StrBuilder str);
void strbuilder_trim (StrBuilder str, char const* cut_set);
void strbuilder_trim_space (StrBuilder str); void strbuilder_trim_space (StrBuilder str);
StrBuilder strbuilder_visualize_whitespace(StrBuilder const str);
struct StrBuilderHeader { struct StrBuilderHeader {
AllocatorInfo Allocator; AllocatorInfo Allocator;
@ -358,29 +358,6 @@ bool strbuilder_append_c_str(StrBuilder* str, char const* c_str_to_append) {
return strbuilder_append_c_str_len(str, c_str_to_append, c_str_len(c_str_to_append)); return strbuilder_append_c_str_len(str, c_str_to_append, c_str_len(c_str_to_append));
} }
inline
bool strbuilder_append_c_str_len(StrBuilder* str, char const* c_str_to_append, ssize append_length)
{
GEN_ASSERT(str != nullptr);
if ( rcast(sptr, c_str_to_append) > 0)
{
ssize curr_len = strbuilder_length(* str);
if ( ! strbuilder_make_space_for(str, c_str_to_append, append_length))
return false;
StrBuilderHeader* header = strbuilder_get_header(* str);
char* Data = * str;
mem_copy( Data + curr_len, c_str_to_append, append_length);
Data[curr_len + append_length] = '\0';
header->Length = curr_len + append_length;
}
return c_str_to_append != nullptr;
}
forceinline forceinline
bool strbuilder_append_str(StrBuilder* str, Str c_str_to_append) { bool strbuilder_append_str(StrBuilder* str, Str c_str_to_append) {
GEN_ASSERT(str != nullptr); GEN_ASSERT(str != nullptr);
@ -589,69 +566,11 @@ Str strbuilder_to_str(StrBuilder str) {
return result; return result;
} }
inline
void strbuilder_trim(StrBuilder str, char const* cut_set)
{
ssize len = 0;
char* start_pos = str;
char* end_pos = scast(char*, str) + strbuilder_length(str) - 1;
while (start_pos <= end_pos && char_first_occurence(cut_set, *start_pos))
start_pos++;
while (end_pos > start_pos && char_first_occurence(cut_set, *end_pos))
end_pos--;
len = scast(ssize, (start_pos > end_pos) ? 0 : ((end_pos - start_pos) + 1));
if (str != start_pos)
mem_move(str, start_pos, len);
str[len] = '\0';
strbuilder_get_header(str)->Length = len;
}
forceinline forceinline
void strbuilder_trim_space(StrBuilder str) { void strbuilder_trim_space(StrBuilder str) {
strbuilder_trim(str, " \t\r\n\v\f"); strbuilder_trim(str, " \t\r\n\v\f");
} }
inline
StrBuilder strbuilder_visualize_whitespace(StrBuilder const str)
{
StrBuilderHeader* header = (StrBuilderHeader*)(scast(char const*, str) - sizeof(StrBuilderHeader));
StrBuilder result = strbuilder_make_reserve(header->Allocator, strbuilder_length(str) * 2); // Assume worst case for space requirements.
for (char const* c = strbuilder_begin(str); c != strbuilder_end(str); c = strbuilder_next(str, c))
switch ( * c )
{
case ' ':
strbuilder_append_str(& result, txt("·"));
break;
case '\t':
strbuilder_append_str(& result, txt(""));
break;
case '\n':
strbuilder_append_str(& result, txt(""));
break;
case '\r':
strbuilder_append_str(& result, txt(""));
break;
case '\v':
strbuilder_append_str(& result, txt(""));
break;
case '\f':
strbuilder_append_str(& result, txt(""));
break;
default:
strbuilder_append_char(& result, * c);
break;
}
return result;
}
#pragma endregion StrBuilder #pragma endregion StrBuilder
#if GEN_COMPILER_CPP #if GEN_COMPILER_CPP