StringBuilder rewrote
This commit is contained in:
@@ -1,42 +1,109 @@
|
||||
#include "StringBuilder.h"
|
||||
|
||||
StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){
|
||||
return Autoarr_create(int8,max_blocks_count,max_block_length);
|
||||
define_Autoarr(string)
|
||||
|
||||
#define MAXLENGTH 32768
|
||||
#define BL_C 32
|
||||
#define BL_L 1024
|
||||
|
||||
|
||||
void complete_buf(StringBuilder* b){
|
||||
if(!b->compl_bufs)
|
||||
b->compl_bufs=Autoarr_create(string,BL_C,BL_L);
|
||||
uint32 len=Autoarr_length(b->curr_buf);
|
||||
if(!len) return;
|
||||
string str={.length=len, .ptr=malloc(len)};
|
||||
uint32 i=0;
|
||||
Autoarr_foreach(b->curr_buf, c, ({
|
||||
str.ptr[i++]=c;
|
||||
}));
|
||||
Autoarr_add(b->compl_bufs,str);
|
||||
Autoarr_free(b->curr_buf);
|
||||
b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
|
||||
}
|
||||
|
||||
void try_complete_buf(StringBuilder* b){
|
||||
if(b->curr_buf->blocks_count==BL_C)
|
||||
complete_buf(b);
|
||||
}
|
||||
|
||||
|
||||
StringBuilder* StringBuilder_create(){
|
||||
StringBuilder* b=malloc(sizeof(StringBuilder));
|
||||
b->compl_bufs=NULL;
|
||||
b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
|
||||
return b;
|
||||
}
|
||||
|
||||
void StringBuilder_free(StringBuilder* b){
|
||||
Autoarr_free(b);
|
||||
if(b->compl_bufs) Autoarr_free(b->compl_bufs);
|
||||
Autoarr_free(b->curr_buf);
|
||||
free(b);
|
||||
}
|
||||
|
||||
void StringBuilder_pop(StringBuilder* b){
|
||||
Autoarr_pop(b);
|
||||
string StringBuilder_build(StringBuilder* b){
|
||||
complete_buf(b);
|
||||
uint32 len=0;
|
||||
Autoarr_foreach(b->compl_bufs, cs, ({
|
||||
len+=cs.length;
|
||||
}));
|
||||
string str= { .length=len, .ptr=malloc(len+1) };
|
||||
str.ptr[len]='\0';
|
||||
uint32 i=0;
|
||||
Autoarr_foreach(b->compl_bufs, cs, ({
|
||||
for(uint32 n=0;n<cs.length;n++)
|
||||
str.ptr[i++]=cs.ptr[n];
|
||||
free(cs.ptr);
|
||||
}));
|
||||
StringBuilder_free(b);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_rmchar(StringBuilder* b){
|
||||
if(b->curr_buf->block_length!=0)
|
||||
Autoarr_pop(b->curr_buf)
|
||||
else {
|
||||
if(!b->compl_bufs) throw(ERR_NULLPTR);
|
||||
string* lastcb=Autoarr_getptr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1));
|
||||
lastcb->length--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_append_char(StringBuilder* b, char c){
|
||||
Autoarr_add(b,c);
|
||||
try_complete_buf(b);
|
||||
Autoarr_add(b->curr_buf,c);
|
||||
}
|
||||
|
||||
|
||||
void StringBuilder_append_string(StringBuilder* b, string s){
|
||||
complete_buf(b);
|
||||
Autoarr_add(b->compl_bufs, string_copy(s));
|
||||
}
|
||||
|
||||
void StringBuilder_append_cptr(StringBuilder* b, char* s){
|
||||
char c;
|
||||
while((c=*s++))
|
||||
Autoarr_add(b,c);
|
||||
string str={
|
||||
.ptr=s,
|
||||
.length=cptr_length(s)
|
||||
};
|
||||
StringBuilder_append_string(b,str);
|
||||
}
|
||||
|
||||
void StringBuilder_append_string(StringBuilder* b, string s){
|
||||
while(s.length>0){
|
||||
Autoarr_add(b,*s.ptr++);
|
||||
s.length--;
|
||||
}
|
||||
void curr_buf_add_string(StringBuilder* b, string s){
|
||||
for(uint32 i=0; i<s.length; i++)
|
||||
Autoarr_add(b->curr_buf,s.ptr[i]);
|
||||
}
|
||||
|
||||
void StringBuilder_append_int64(StringBuilder* b, int64 a){
|
||||
try_complete_buf(b);
|
||||
uint8 i=0;
|
||||
if(a==0){
|
||||
Autoarr_add(b,'0');
|
||||
Autoarr_add(b->curr_buf,'0');
|
||||
return;
|
||||
}
|
||||
else if(a<0){
|
||||
Autoarr_add(b,'-');
|
||||
Autoarr_add(b->curr_buf,'-');
|
||||
a=-a;
|
||||
}
|
||||
char buf[24];
|
||||
@@ -45,14 +112,15 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){
|
||||
a/=10;
|
||||
}
|
||||
string rev=string_reverse((string){buf,i});
|
||||
StringBuilder_append_string(b,rev);
|
||||
curr_buf_add_string(b,rev);
|
||||
free(rev.ptr);
|
||||
}
|
||||
|
||||
void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
|
||||
try_complete_buf(b);
|
||||
uint8 i=0;
|
||||
if(a==0){
|
||||
Autoarr_add(b,'0');
|
||||
Autoarr_add(b->curr_buf,'0');
|
||||
return;
|
||||
}
|
||||
char buf[24];
|
||||
@@ -61,24 +129,17 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
|
||||
a/=10;
|
||||
}
|
||||
string rev=string_reverse((string){buf,i});
|
||||
StringBuilder_append_string(b,rev);
|
||||
curr_buf_add_string(b,rev);
|
||||
free(rev.ptr);
|
||||
}
|
||||
|
||||
void StringBuilder_append_double(StringBuilder* b, double a){
|
||||
void StringBuilder_append_float64(StringBuilder* b, double a){
|
||||
try_complete_buf(b);
|
||||
char buf[32];
|
||||
IFWIN(
|
||||
sprintf_s(buf,32,"%lf",a),
|
||||
sprintf(buf,"%lf",a)
|
||||
);
|
||||
StringBuilder_append_cptr(b,buf);
|
||||
}
|
||||
|
||||
char* StringBuilder_build(StringBuilder* b){
|
||||
uint32 len=Autoarr_length(b);
|
||||
char* str=malloc(len+1);
|
||||
str[len]=0;
|
||||
for(uint32 i=0;i<len;i++)
|
||||
str[i]=Autoarr_get(b,i);
|
||||
return str;
|
||||
curr_buf_add_string(b, (string){.ptr=buf, .length=cptr_length(buf)});
|
||||
}
|
||||
|
||||
@@ -7,18 +7,28 @@ extern "C" {
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
#include "../String/string.h"
|
||||
|
||||
typedef Autoarr(int8) StringBuilder;
|
||||
declare_Autoarr(string)
|
||||
|
||||
StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
|
||||
typedef struct StringBuilder{
|
||||
Autoarr(string)* compl_bufs;
|
||||
Autoarr(int8)* curr_buf;
|
||||
} StringBuilder;
|
||||
|
||||
StringBuilder* StringBuilder_create(void);
|
||||
void StringBuilder_free(StringBuilder* b);
|
||||
void StringBuilder_pop(StringBuilder* b);
|
||||
// Joins all strings from compl_bufs.
|
||||
// Returns zero-terminated string.
|
||||
// No need to call string_extract()!
|
||||
// Frees StringBuilder.
|
||||
string StringBuilder_build(StringBuilder* b);
|
||||
// removes last char
|
||||
void StringBuilder_rmchar(StringBuilder* b);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cptr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, string s);
|
||||
void StringBuilder_append_int64(StringBuilder* b, int64 a);
|
||||
void StringBuilder_append_uint64(StringBuilder* b, uint64 a);
|
||||
void StringBuilder_append_double(StringBuilder* b, double a);
|
||||
char* StringBuilder_build(StringBuilder* b);
|
||||
void StringBuilder_append_float64(StringBuilder* b, double a);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "../String/string.h"
|
||||
|
||||
// copies str content to new char pointer value (adding '\0' at the end)
|
||||
char* string_cpToCptr(string str){
|
||||
char* string_extract(string str){
|
||||
if(str.length==0) return NULL;
|
||||
char* cptr=malloc(str.length*sizeof(char)+1);
|
||||
cptr[str.length]=0;
|
||||
@@ -10,15 +10,15 @@ char* string_cpToCptr(string str){
|
||||
return cptr;
|
||||
}
|
||||
|
||||
// copies cptr content (excluding '\0' at the end) to new string
|
||||
string string_cpFromCharPtr(char* cptr){
|
||||
if(!cptr) return stringNull;
|
||||
string str;
|
||||
str.length=cptr_length(cptr)-1;
|
||||
str.ptr=malloc(str.length);
|
||||
for(uint32 i=0;i<str.length;i++)
|
||||
str.ptr[i]=cptr[i];
|
||||
return str;
|
||||
// copies src.ptr content to new string
|
||||
string string_copy(string src){
|
||||
if(!src.ptr) return src;
|
||||
string nstr;
|
||||
nstr.length=src.length;
|
||||
nstr.ptr=malloc(nstr.length);
|
||||
for(uint32 i=0;i<nstr.length;i++)
|
||||
nstr.ptr[i]=src.ptr[i];
|
||||
return nstr;
|
||||
}
|
||||
|
||||
// compares two strings, NullPtr-friendly
|
||||
|
||||
@@ -16,10 +16,10 @@ typedef struct string{
|
||||
static const string stringNull={NULL,0};
|
||||
|
||||
// copies str content to new char pointer value (adding '\0' at the end)
|
||||
char* string_cpToCptr(string str);
|
||||
char* string_extract(string str);
|
||||
|
||||
// copies cptr content (excluding '\0' at the end) to new string
|
||||
string string_cpFromCharPtr(char* cptr);
|
||||
// copies src.ptr content to new string
|
||||
string string_copy(string src);
|
||||
|
||||
// compares two strings, NullPtr-friendly
|
||||
bool string_compare(string str0, string str1);
|
||||
|
||||
Reference in New Issue
Block a user