From b6d95e8d70662480047c313bf3d8fe883da4deee Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 4 Feb 2025 14:17:15 -0500 Subject: [PATCH] move strbuilder_make_space_for to strings.cpp (not inlined) --- base/dependencies/strings.cpp | 37 +++++++++++++++++++++++++++++ base/dependencies/strings.hpp | 44 +++-------------------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/base/dependencies/strings.cpp b/base/dependencies/strings.cpp index 336671a..a95ed9a 100644 --- a/base/dependencies/strings.cpp +++ b/base/dependencies/strings.cpp @@ -58,4 +58,41 @@ StrBuilder strbuilder_make_reserve( AllocatorInfo allocator, ssize capacity ) return result; } +bool strbuilder_make_space_for(StrBuilder* str, char const* to_append, ssize add_len) +{ + ssize available = strbuilder_avail_space(* str); + + if (available >= add_len) { + return true; + } + else + { + ssize new_len, old_size, new_size; + void* ptr; + void* new_ptr; + + AllocatorInfo allocator = strbuilder_get_header(* str)->Allocator; + StrBuilderHeader* header = nullptr; + + new_len = strbuilder_grow_formula(strbuilder_length(* str) + add_len); + ptr = strbuilder_get_header(* str); + old_size = size_of(StrBuilderHeader) + strbuilder_length(* str) + 1; + new_size = size_of(StrBuilderHeader) + new_len + 1; + + new_ptr = resize(allocator, ptr, old_size, new_size); + + if (new_ptr == nullptr) + return false; + + header = rcast(StrBuilderHeader*, new_ptr); + header->Allocator = allocator; + header->Capacity = new_len; + + char** Data = rcast(char**, str); + * Data = rcast(char*, header + 1); + + return true; + } +} + #pragma endregion StrBuilder diff --git a/base/dependencies/strings.hpp b/base/dependencies/strings.hpp index 039ffe2..f6e0790 100644 --- a/base/dependencies/strings.hpp +++ b/base/dependencies/strings.hpp @@ -122,8 +122,9 @@ struct StrBuilder; forceinline usize strbuilder_grow_formula(usize value); -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_reserve (AllocatorInfo allocator, ssize capacity); +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); StrBuilder strbuilder_make_c_str (AllocatorInfo allocator, char const* str); StrBuilder strbuilder_make_str (AllocatorInfo allocator, Str str); @@ -132,7 +133,6 @@ StrBuilder strbuilder_fmt_buf (AllocatorInfo allocator, char StrBuilder strbuilder_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); bool strbuilder_are_equal (StrBuilder const lhs, StrBuilder const rhs); bool strbuilder_are_equal_str (StrBuilder const lhs, Str rhs); -bool strbuilder_make_space_for (StrBuilder* str, char const* to_append, ssize add_len); 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_len (StrBuilder* str, char const* c_str_to_append, ssize length); @@ -522,44 +522,6 @@ ssize strbuilder_length(StrBuilder const str) return header->Length; } -inline -bool strbuilder_make_space_for(StrBuilder* str, char const* to_append, ssize add_len) -{ - ssize available = strbuilder_avail_space(* str); - - if (available >= add_len) { - return true; - } - else - { - ssize new_len, old_size, new_size; - void* ptr; - void* new_ptr; - - AllocatorInfo allocator = strbuilder_get_header(* str)->Allocator; - StrBuilderHeader* header = nullptr; - - new_len = strbuilder_grow_formula(strbuilder_length(* str) + add_len); - ptr = strbuilder_get_header(* str); - old_size = size_of(StrBuilderHeader) + strbuilder_length(* str) + 1; - new_size = size_of(StrBuilderHeader) + new_len + 1; - - new_ptr = resize(allocator, ptr, old_size, new_size); - - if (new_ptr == nullptr) - return false; - - header = rcast(StrBuilderHeader*, new_ptr); - header->Allocator = allocator; - header->Capacity = new_len; - - char** Data = rcast(char**, str); - * Data = rcast(char*, header + 1); - - return true; - } -} - forceinline b32 strbuilder_starts_with_str(StrBuilder const str, Str substring) { if (substring.Len > strbuilder_length(str))