new StringBuilder functions

This commit is contained in:
2025-11-10 06:55:26 +05:00
parent 1406189511
commit 57c5942fcc
7 changed files with 82 additions and 45 deletions

View File

@@ -7,12 +7,19 @@ List_ List_alloc_size(u32 initial_size){
return List_construct_size(malloc(allocated_size), 0, allocated_size);
}
void* List_expand_size(List_* ptr, u32 expansion_size){
List_ List_copy(const List_ src){
List_ copy = List_alloc_size(src.allocated_size);
if(copy.data != NULL)
memcpy(copy.data, src.data, src.size);
return copy;
}
void* List_expand_size(List_* ptr, u32 size_to_add){
u32 occupied_size = ptr->size;
u32 expanded_alloc_size = ptr->allocated_size;
if(expanded_alloc_size == 0)
expanded_alloc_size = 64;
ptr->size += expansion_size;
ptr->size += size_to_add;
while(ptr->size > expanded_alloc_size){
expanded_alloc_size *= 2;
}

View File

@@ -5,16 +5,24 @@ void StringBuilder_destroy(StringBuilder* b){
return;
free(b->buffer.data);
b->buffer = List_construct_size(NULL, 0, 0);
}
str StringBuilder_getStr(StringBuilder* b){
if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0')
if(b->buffer.size == 0 || ((char*)b->buffer.data)[b->buffer.size - 1] != '\0'){
List_push(&b->buffer, u8, '\0');
str result = str_construct((char*)b->buffer.data, b->buffer.size - 1, true);
// '\0' at the end doesn't increase buffer.size
b->buffer.size -= 1;
}
str result = str_construct((char*)b->buffer.data, b->buffer.size, true);
return result;
}
bool StringBuilder_equals(const StringBuilder* a, const StringBuilder* b){
str a_str = str_construct((char*)a->buffer.data, a->buffer.size, false);
str b_str = str_construct((char*)b->buffer.data, b->buffer.size, false);
return str_equals(a_str, b_str);
}
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){
if(count < b->buffer.size){
b->buffer.size -= count;

View File

@@ -1,19 +1,21 @@
#include "tlibc/string/str.h"
#include "tlibc/string/StringBuilder.h"
str str_copy(str src){
if(src.data == NULL || src.size == 0)
return src;
str str_copy(const str self){
if(self.data == NULL || self.size == 0)
return self;
str nstr = str_construct((char*)malloc(src.size + 1), src.size, true);
memcpy(nstr.data, src.data, src.size);
nstr.data[nstr.size] = '\0';
return nstr;
str copy = str_construct((char*)malloc(self.size + 1), self.size, true);
memcpy(copy.data, self.data, self.size);
copy.data[copy.size] = '\0';
return copy;
}
bool str_equals(str s0, str s1){
if(s0.size != s1.size)
bool str_equals(const str self, const str other){
if(self.size != other.size)
return false;
if(self.data == other.data)
return true;
/*
BENCHMARK:
str_equals64: 2.967s
@@ -21,7 +23,7 @@ bool str_equals(str s0, str s1){
strncmp: 1.611s
memcmp: 0.710s
*/
return memcmp(s0.data, s1.data, s0.size) == 0;
return memcmp(self.data, other.data, self.size) == 0;
}
str str_reverse(str s){
@@ -34,7 +36,7 @@ str str_reverse(str s){
return r;
}
i32 str_seek(str src, str fragment, u32 startIndex){
i32 str_seek(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0)
return -1;
@@ -49,7 +51,7 @@ i32 str_seek(str src, str fragment, u32 startIndex){
return -1;
}
i32 str_seekReverse(str src, str fragment, u32 startIndex){
i32 str_seekReverse(const str src, const str fragment, u32 startIndex){
if(src.size == 0 || fragment.size == 0)
return -1;
@@ -66,7 +68,7 @@ i32 str_seekReverse(str src, str fragment, u32 startIndex){
return -1;
}
i32 str_seekChar(str src, char c, u32 startIndex){
i32 str_seekChar(const str src, char c, u32 startIndex){
for(u32 i = startIndex; i < src.size; i++){
if(src.data[i] == c)
return i;
@@ -74,7 +76,7 @@ i32 str_seekChar(str src, char c, u32 startIndex){
return -1;
}
i32 str_seekCharReverse(str src, char c, u32 startIndex){
i32 str_seekCharReverse(const str src, char c, u32 startIndex){
if(startIndex > src.size - 1)
startIndex = src.size - 1;
for(u32 i = startIndex; i != (u32)-1; i--){
@@ -84,24 +86,27 @@ i32 str_seekCharReverse(str src, char c, u32 startIndex){
return -1;
}
bool str_startsWith(str src, str fragment){
bool str_startsWith(const str src, const str fragment){
if(src.size < fragment.size)
return false;
src.size = fragment.size;
return str_equals(src, fragment);
str src_fragment = str_null;
src_fragment.data = src.data;
src_fragment.size = fragment.size;
return str_equals(src_fragment, fragment);
}
bool str_endsWith(str src, str fragment){
bool str_endsWith(const str src, const str fragment){
if(src.size < fragment.size)
return false;
src.data = (char*)(src.data + src.size - fragment.size);
src.size = fragment.size;
return str_equals(src, fragment);
str src_fragment = str_null;
src_fragment.data = (char*)(src.data + src.size - fragment.size);
src_fragment.size = fragment.size;
return str_equals(src_fragment, fragment);
}
u32 str_hash32(str s){
u32 str_hash32(const str s){
u8* ubuf = (u8*)s.data;
u32 hash=0;
for (u32 i = 0; i < s.size; i++)
@@ -109,7 +114,7 @@ u32 str_hash32(str s){
return hash;
}
str str_toUpper(str src){
str str_toUpper(const str src){
str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){
if(isAlphabeticalLower(r.data[i]))
@@ -118,7 +123,7 @@ str str_toUpper(str src){
return r;
}
str str_toLower(str src){
str str_toLower(const str src){
str r = str_copy(src);
for (u32 i = 0; i < r.size; i++){
if(isAlphabeticalUpper(r.data[i]))