string -> StringFragment, throw_wrongchar() fixed
This commit is contained in:
78
StringFragment/StringBuilder.c
Normal file
78
StringFragment/StringBuilder.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#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_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, StringFragment s){
|
||||
s.ptr+=s.offset;
|
||||
while(s.length-->0)
|
||||
Autoarr_add(b,*s.ptr++);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
StringFragment rev=StringFragment_reverse((StringFragment){buf,0,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;
|
||||
}
|
||||
StringFragment rev=StringFragment_reverse((StringFragment){buf,0,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);
|
||||
}
|
||||
|
||||
StringFragment StringBuilder_build(StringBuilder* b){
|
||||
StringFragment str={
|
||||
.offset=0,
|
||||
.length=Autoarr_length(b)
|
||||
};
|
||||
str.ptr=malloc(str.length+1);
|
||||
for(uint32 i=0; i<str.length; i++)
|
||||
str.ptr[i]=Autoarr_get(b,i);
|
||||
str.ptr[str.length]='\0';
|
||||
return str;
|
||||
}
|
||||
24
StringFragment/StringBuilder.h
Normal file
24
StringFragment/StringBuilder.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
#include "StringFragment.h"
|
||||
|
||||
typedef Autoarr(int8) StringBuilder;
|
||||
|
||||
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cptr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, StringFragment 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);
|
||||
//returns StringFragment with '\0' at the end
|
||||
StringFragment StringBuilder_build(StringBuilder* b);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
42
StringFragment/StringFragment.c
Normal file
42
StringFragment/StringFragment.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "StringFragment.h"
|
||||
|
||||
//copies <length> characters from <ptr+offset> to new StringFragment (adding '\0' at the end)
|
||||
StringFragment StringFragment_extract(StringFragment str){
|
||||
if(str.length==0) return stringNull;
|
||||
StringFragment extr={
|
||||
.offset=0,
|
||||
.length=str.length,
|
||||
.ptr=malloc(str.length+1)
|
||||
};
|
||||
str.ptr+=str.offset;
|
||||
for(uint32 i=0; i<str.length; i++)
|
||||
extr.ptr[i]=str.ptr[i];
|
||||
extr.ptr[str.length]='\0';
|
||||
return extr;
|
||||
}
|
||||
|
||||
//compares two strings, NullPtr-friendly
|
||||
bool StringFragment_compare(StringFragment str0, StringFragment str1){
|
||||
if(str0.length!=str1.length) return false;
|
||||
if(!str0.ptr) return str1.ptr ? false : true;
|
||||
else if(!str1.ptr) return false;
|
||||
str0.ptr+=str0.offset;
|
||||
str1.ptr+=str1.offset;
|
||||
while(str0.length-->0)
|
||||
if(*str0.ptr++ != *str1.ptr++)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
//creates new StringFragment which is reversed variant of <s>
|
||||
StringFragment StringFragment_reverse(StringFragment s){
|
||||
if(s.length==0) return s;
|
||||
StringFragment r={
|
||||
.offset=0,
|
||||
.length=s.length,
|
||||
.ptr=malloc(s.length)
|
||||
};
|
||||
for(uint32 i=0; i<s.length; i++)
|
||||
r.ptr[i+s.offset]=s.ptr[s.length-i-1];
|
||||
return r;
|
||||
}
|
||||
29
StringFragment/StringFragment.h
Normal file
29
StringFragment/StringFragment.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
//part of string
|
||||
typedef struct StringFragment{
|
||||
char* ptr; //may be without '\0' at the end
|
||||
uint32 offset;
|
||||
uint32 length;
|
||||
} StringFragment;
|
||||
|
||||
static const StringFragment stringNull={NULL,0,0};
|
||||
|
||||
//copies <length> characters from <ptr+offset> to new StringFragment (adding '\0' at the end)
|
||||
StringFragment StringFragment_extract(StringFragment str);
|
||||
|
||||
//compares two strings, NullPtr-friendly
|
||||
bool StringFragment_compare(StringFragment str0, StringFragment str1);
|
||||
|
||||
//creates new StringFragment which is reversed variant of <s>
|
||||
StringFragment StringFragment_reverse(StringFragment s);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user