some code moved to String directory

This commit is contained in:
2022-04-19 20:50:44 +03:00
parent 5fd395d7eb
commit 2434a126da
14 changed files with 93 additions and 80 deletions

84
String/StringBuilder.c Normal file
View File

@@ -0,0 +1,84 @@
#include "StringBuilder.h"
StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){
return Autoarr_create(int8,max_blocks_count,max_block_length);
}
void StringBuilder_free(StringBuilder* b){
Autoarr_free(b);
}
void StringBuilder_pop(StringBuilder* b){
Autoarr_pop(b);
}
void StringBuilder_append_char(StringBuilder* b, char c){
Autoarr_add(b,c);
}
void StringBuilder_append_cptr(StringBuilder* b, char* s){
char c;
while((c=*s++))
Autoarr_add(b,c);
}
void StringBuilder_append_string(StringBuilder* b, string s){
while(s.length>0){
Autoarr_add(b,*s.ptr++);
s.length--;
}
}
void StringBuilder_append_int64(StringBuilder* b, int64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
return;
}
else if(a<0){
Autoarr_add(b,'-');
a=-a;
}
char buf[24];
while(a!=0){
buf[i++]='0'+a%10;
a/=10;
}
string rev=string_reverse((string){buf,i});
StringBuilder_append_string(b,rev);
free(rev.ptr);
}
void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
return;
}
char buf[24];
while(a!=0){
buf[i++]='0'+a%10;
a/=10;
}
string rev=string_reverse((string){buf,i});
StringBuilder_append_string(b,rev);
free(rev.ptr);
}
void StringBuilder_append_double(StringBuilder* b, double a){
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;
}

25
String/StringBuilder.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../Autoarr/Autoarr.h"
#include "../String/string.h"
typedef Autoarr(int8) StringBuilder;
StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
void StringBuilder_free(StringBuilder* b);
void StringBuilder_pop(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);
#if __cplusplus
}
#endif

42
String/string.c Normal file
View File

@@ -0,0 +1,42 @@
#include "../String/string.h"
// copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCptr(string str){
if(str.length==0) return NULL;
char* cptr=malloc(str.length*sizeof(char)+1);
cptr[str.length]=0;
while(str.length-->0)
cptr[str.length]=str.ptr[str.length];
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;
}
// compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1){
if(str0.length!=str1.length) return false;
if(!str0.ptr) return str1.ptr ? false : true;
else if(!str1.ptr) return false;
while(str0.length-->0)
if(*str0.ptr++ != *str1.ptr++)
return false;
return true;
}
// creates new string which is reversed variant of <s>
string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1];
return r;
}

32
String/string.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
// my fixed length string struct
// doesn't store '\0' at the end
typedef struct string{
char* ptr; // char pointer
uint32 length; // amount of chars in ptr value
} 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);
// copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr);
// compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1);
// creates new string which is reversed variant of <s>
string string_reverse(string s);
#if __cplusplus
}
#endif