From 41f32f4f8c3168c83824c4f1c260c9a238ac46b6 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 12 Apr 2022 22:59:29 +0300 Subject: [PATCH 1/4] string -> StringFragment, throw_wrongchar() fixed --- Autoarr/StringBuilder.h | 22 --- DtsodParser/DtsodV24_deserialize.c | 141 +++++++++++--------- DtsodParser/DtsodV24_serialize.c | 9 +- {Autoarr => StringFragment}/StringBuilder.c | 27 ++-- StringFragment/StringBuilder.h | 24 ++++ StringFragment/StringFragment.c | 42 ++++++ StringFragment/StringFragment.h | 29 ++++ base/base.h | 2 +- base/cptr.c | 36 +++++ base/cptr.h | 23 ++++ base/errors.c | 2 +- base/mystr.c | 77 ----------- base/mystr.h | 44 ------ tests/main.c | 2 +- tests/test_string.c | 24 +++- 15 files changed, 268 insertions(+), 236 deletions(-) delete mode 100644 Autoarr/StringBuilder.h rename {Autoarr => StringFragment}/StringBuilder.c (70%) create mode 100644 StringFragment/StringBuilder.h create mode 100644 StringFragment/StringFragment.c create mode 100644 StringFragment/StringFragment.h create mode 100644 base/cptr.c create mode 100644 base/cptr.h delete mode 100644 base/mystr.c delete mode 100644 base/mystr.h diff --git a/Autoarr/StringBuilder.h b/Autoarr/StringBuilder.h deleted file mode 100644 index 0b52429..0000000 --- a/Autoarr/StringBuilder.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "Autoarr.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, 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 \ No newline at end of file diff --git a/DtsodParser/DtsodV24_deserialize.c b/DtsodParser/DtsodV24_deserialize.c index 7e3a354..686f8c5 100644 --- a/DtsodParser/DtsodV24_deserialize.c +++ b/DtsodParser/DtsodV24_deserialize.c @@ -1,24 +1,40 @@ #include "DtsodV24.h" -#include "../Autoarr/StringBuilder.h" +#include "../StringFragment/StringBuilder.h" #define ARR_BC 8 #define ARR_BL 16 #define STRB_BC 64 #define STRB_BL 1024 -Maybe ERROR_WRONGCHAR(char c, char* text){ - char errBuf[]="unexpected at:\n \"" - "00000000000000000000000000000000\""; - errBuf[12]=c; - for(uint8 i=0; i<32; i++) - // writes 16 chars before and 15 after the wrongchar - errBuf[i+22]=*(text - 16 + i); - safethrow(cptr_copy(errBuf)); +Maybe ERROR_WRONGCHAR(const char c, char* text, char* text_first, const char* srcfile, int line, const char* funcname){ + char errBuf[33]; + errBuf[32]='\0'; + char* errText=text-16; + if(errText at:\n" + " \"%s\"\n" + "\\___[%s:%d] %s()", + c,errBuf, srcfile,line,funcname), + sprintf(errmsg, "unexpected <%c> at:\n" + " \"%s\"\n" + " \\___[%s:%d] %s()", + c,errBuf, srcfile,line,funcname) + ); + safethrow(cptr_copy(errmsg)); } -#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text) +#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text, shared->sh_text_first, __FILE__,__LINE__,__func__) typedef struct DeserializeSharedData{ + const char* sh_text_first; char* sh_text; bool sh_partOfDollarList; bool sh_readingList; @@ -39,7 +55,7 @@ Maybe __SkipComment(DeserializeSharedData* shared) { Maybe __ReadName(DeserializeSharedData* shared){ char c; - string nameStr={text,0}; + StringFragment nameStr={text,0}; text--; while ((c=*++text)) switch (c){ case ' ': case '\t': @@ -65,7 +81,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ if((*++text)!=';') safethrow_wrongchar(c); case ':': - return SUCCESS(UniPtr(CharPtr,string_cpToCptr(nameStr))); + return SUCCESS(UniPtr(CharPtr,StringFragment_extract(nameStr).ptr)); case '$': if(nameStr.length!=0) safethrow_wrongchar(c); @@ -100,9 +116,9 @@ Maybe __ReadString(DeserializeSharedData* shared){ StringBuilder_append_char(b,c); } else { - char* str=StringBuilder_build(b); + StringFragment str=StringBuilder_build(b); Autoarr_clear(b); - return SUCCESS(UniPtr(CharPtr,str)); + return SUCCESS(UniPtr(CharPtr,str.ptr)); } } else { @@ -127,68 +143,62 @@ Maybe __ReadList(DeserializeSharedData* shared){ }; #define ReadList() __ReadList(shared) -Maybe __ParseValue(DeserializeSharedData* shared, string str){ - //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCptr(str)); - const string nullStr={"null",4}; - const string trueStr={"true",4}; - const string falseStr={"false",5}; - switch(*str.ptr){ - case 'n': - if(string_compare(str,nullStr)) - return SUCCESS(UniNull); - else safethrow_wrongchar(*str.ptr); - break; - case 't': - if(string_compare(str,trueStr)) +Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ + //printf("\e[94m<\e[96m%s\e[94m>\n",StringFragment_extract(str)); + const StringFragment trueStr= {"true" ,0,4}; + const StringFragment falseStr={"false",0,5}; + switch(str.ptr[str.length-1]){ + //Bool + case 'e': { + if(StringFragment_compare(str,trueStr)) return SUCCESS(UniTrue); - else safethrow_wrongchar(*str.ptr); - break; - case 'f': - if(string_compare(str,falseStr)) + else if(StringFragment_compare(str,falseStr)) return SUCCESS(UniFalse); else safethrow_wrongchar(*str.ptr); - break; - default: - switch(str.ptr[str.length-1]){ - case 'f': { - char* _c=string_cpToCptr(str); - Unitype rez=Uni(Float64,strtod(_c,NULL)); - free(_c); - return SUCCESS(rez); - } - case 'u': { - uint64 lu=0; - char* _c=string_cpToCptr(str); - sscanf(_c,"%lu",&lu); - free(_c); - return SUCCESS(Uni(UInt64,lu)); - } - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - int64 li=0; - char* _c=string_cpToCptr(str); - if(sscanf(_c,"%li",&li)!=1){ - char err[64]; - IFWIN( - sprintf_s(err,64,"can't parse to int: <%s>",_c), - sprintf(err,"can't parse to int: <%s>",_c) - ); - safethrow(err); - } - free(_c); - return SUCCESS(Uni(Int64,li)); - } - default: - safethrow_wrongchar(str.ptr[str.length-1]); + } + //Float64 + case 'f': { + char* _c=StringFragment_extract(str).ptr; + Unitype rez=Uni(Float64,strtod(_c,NULL)); + free(_c); + return SUCCESS(rez); + } + //UInt64 + case 'u': { + uint64 lu=0; + char* _c=StringFragment_extract(str).ptr; + sscanf(_c,"%lu",&lu); + free(_c); + return SUCCESS(Uni(UInt64,lu)); + } + //Int64 + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': { + int64 li=0; + char* _c=StringFragment_extract(str).ptr; + if(sscanf(_c,"%li",&li)!=1){ + char err[64]; + IFWIN( + sprintf_s(err,64,"can't parse to int: <%s>",_c), + sprintf(err,"can't parse to int: <%s>",_c) + ); + safethrow(err); } + free(_c); + return SUCCESS(Uni(Int64,li)); + } + //unknown type + default: + safethrow_wrongchar(str.ptr[str.length-1]); } + safethrow(ERR_ENDOFSTR); }; #define ParseValue(str) __ParseValue(shared, str) Maybe __ReadValue(DeserializeSharedData* shared){ char c; - string valueStr={text+1,0}; + StringFragment valueStr={text+1,0}; Unitype value; while ((c=*++text)) switch (c){ case ' ': case '\t': @@ -244,6 +254,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){ Maybe __deserialize(char** _text, bool _calledRecursively) { DeserializeSharedData _shared={ + .sh_text_first=*_text, .sh_text=*_text, .sh_partOfDollarList=false, .sh_readingList=false, diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index 5b7c455..bf87775 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -1,5 +1,5 @@ #include "DtsodV24.h" -#include "../Autoarr/StringBuilder.h" +#include "../StringFragment/StringBuilder.h" //65536 max length! #define STRB_BC 64 @@ -51,8 +51,7 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){ StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); break; case Null: - if(!u.VoidPtr) StringBuilder_append_cptr(b, "null"); - else throw("Null-type pointer is not 0"); + throw("Null isn't supported in DtsodV24"); break; case AutoarrUnitypePtr: addc('['); @@ -97,7 +96,7 @@ void __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){ char* DtsodV24_serialize(Hashtable* dtsod){ StringBuilder sb=StringBuilder_create(STRB_BC,STRB_BL); __serialize(&sb,0,dtsod); - char* str=StringBuilder_build(&sb); + StringFragment str=StringBuilder_build(&sb); Autoarr_clear((&sb)); - return str; + return str.ptr; } diff --git a/Autoarr/StringBuilder.c b/StringFragment/StringBuilder.c similarity index 70% rename from Autoarr/StringBuilder.c rename to StringFragment/StringBuilder.c index 6a12265..4066c5a 100644 --- a/Autoarr/StringBuilder.c +++ b/StringFragment/StringBuilder.c @@ -14,11 +14,10 @@ void StringBuilder_append_cptr(StringBuilder* b, char* s){ Autoarr_add(b,c); } -void StringBuilder_append_string(StringBuilder* b, string s){ - while(s.length>0){ +void StringBuilder_append_string(StringBuilder* b, StringFragment s){ + s.ptr+=s.offset; + while(s.length-->0) Autoarr_add(b,*s.ptr++); - s.length--; - } } void StringBuilder_append_int64(StringBuilder* b, int64 a){ @@ -36,7 +35,7 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){ buf[i++]='0'+a%10; a/=10; } - string rev=string_reverse((string){buf,i}); + StringFragment rev=StringFragment_reverse((StringFragment){buf,0,i}); StringBuilder_append_string(b,rev); free(rev.ptr); } @@ -52,8 +51,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ buf[i++]='0'+a%10; a/=10; } - string rev=string_reverse((string){buf,i}); - printf("\e[95mrev:%s\n",string_cpToCptr(rev)); + StringFragment rev=StringFragment_reverse((StringFragment){buf,0,i}); StringBuilder_append_string(b,rev); free(rev.ptr); } @@ -67,11 +65,14 @@ void StringBuilder_append_double(StringBuilder* b, double 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 characters from 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; i0) + if(*str0.ptr++ != *str1.ptr++) + return false; + return true; +} + +//creates new StringFragment which is reversed variant of +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 characters from 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 +StringFragment StringFragment_reverse(StringFragment s); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/base/base.h b/base/base.h index ad651e6..b23b92d 100644 --- a/base/base.h +++ b/base/base.h @@ -7,7 +7,7 @@ extern "C" { #include "std.h" #include "types.h" #include "errors.h" -#include "mystr.h" +#include "cptr.h" // executes codeblock and prints execution time #ifdef CLOCK_REALTIME //non-standard high-precision clock diff --git a/base/cptr.c b/base/cptr.c new file mode 100644 index 0000000..0646b15 --- /dev/null +++ b/base/cptr.c @@ -0,0 +1,36 @@ +#include "base.h" + +//returns length of string (without \0) +uint32 cptr_length(char* str){ + uint32 len=0; + while(*(str++)) len++; + return len; +} + +//allocates new char[] and copies src there +char* cptr_copy(char* src){ + uint32 len=cptr_length(src)+1; + char* dst=malloc(len); + while(len-->0) + dst[len]=src[len]; + return dst; +} + +//compares two char buffers, NullPtr-friendly +bool cptr_compare(char* key0, char* key1){ + if(!key0) return key1 ? false : true; + if(!key1) return false; + while(*key0&&*key1) + if(*key0++ != *key1++) + return false; + return true; +} + +//multiplies char n times +char* char_multiply(char c, uint32 n){ + char* rez=malloc(n+1); + rez[n]=0; + while(n-->0) + rez[n]=c; + return rez; +} \ No newline at end of file diff --git a/base/cptr.h b/base/cptr.h new file mode 100644 index 0000000..46f6679 --- /dev/null +++ b/base/cptr.h @@ -0,0 +1,23 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "types.h" + +//returns length of string (without \0) +uint32 cptr_length(char* str); + +//allocates new char[] and copies src there +char* cptr_copy(char* src); + +//compares two char buffers, NullPtr-friendly +bool cptr_compare(char* key0, char* key1); + +//multiplies char n times +char* char_multiply(char c, uint32 n); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/base/errors.c b/base/errors.c index d10d5d1..f56687a 100644 --- a/base/errors.c +++ b/base/errors.c @@ -1,6 +1,6 @@ #include "std.h" #include "errors.h" -#include "mystr.h" +#include "cptr.h" char* errname(err_t err){ switch(err){ diff --git a/base/mystr.c b/base/mystr.c deleted file mode 100644 index 5503b46..0000000 --- a/base/mystr.c +++ /dev/null @@ -1,77 +0,0 @@ -#include "base.h" - -//returns length of string (including \0) -uint32 cptr_length(char* str){ - uint32 len=0; - while(*(str++)) len++; - return ++len; -} - -//allocates new char[] and copies src there -char* cptr_copy(char* src){ - uint32 len=cptr_length(src); - char* dst=malloc(len*sizeof(char)); - while(len-->0) - dst[len]=src[len]; - return dst; -} - -//compares two char buffers, NullPtr-friendly -bool cptr_compare(char* key0, char* key1){ - if(!key0) return key1 ? false : true; - if(!key1) return false; - while(*key0&&*key1) - if(*key0++ != *key1++) - return false; - return true; -} - -//multiplies char n times -char* char_multiply(char c, uint32 n){ - char* rez=malloc(n+1); - rez[n]=0; - while(n-->0) - rez[n]=c; - return rez; -} - -//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;i0) - if(*str0.ptr++ != *str1.ptr++) - return false; - return true; -} - -//creates new string which is reversed variant of -string string_reverse(string s){ - if(s.length==0) return s; - string r={malloc(s.length), s.length}; - for(uint32 i=0; i -string string_reverse(string s); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index 4b3149d..6b3b54b 100644 --- a/tests/main.c +++ b/tests/main.c @@ -13,7 +13,7 @@ void test_all(){ int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); - optime("test_all",1,{test_all();}); + optime("test_all",1,test_all()); printf("\e[0m\n"); return 0; } diff --git a/tests/test_string.c b/tests/test_string.c index e9e249f..5783047 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -1,16 +1,26 @@ #include "tests.h" -#include "../base/mystr.h" +#include "../StringFragment/StringFragment.h" void test_string(){ optime(__func__,1,({ printf("\e[96m-------------[test_string]-------------\n"); char c[]="0123456789abcdef"; - string s=string_cpFromCharPtr(c); - printf("\e[92m\"%s\" \e[94m-> string_cpFromCharPtr()\n",c); - if(s.length!=16) throw("string created with incorrect length"); - char* p=string_cpToCptr(s); - printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p); - free(p); + + StringFragment s={.ptr=cptr_copy(c),.offset=0,.length=cptr_length(c)}; + printf("\e[92m\"%s\" \e[94m-> StringFragment\n",c); + if(s.length!=16) throw("StringFragment created with incorrect length"); + + StringFragment extr=StringFragment_extract(s); + printf("\e[94mStringFragment_extract() -> \e[92m\"%s\"\n",extr.ptr); + if(extr.length!=16) throw("StringFragment extracted with incorrect length"); + free(extr.ptr); + + s.offset+=2; s.length-=4; + printf("\e[94mStringFragment offset+=2 length-=4\n"); + extr=StringFragment_extract(s); + printf("\e[94mStringFragment_extract() -> \e[92m\"%s\"\n",extr.ptr); + if(extr.length!=12) throw("StringFragment extracted with incorrect length"); free(s.ptr); + free(extr.ptr); })); } \ No newline at end of file From 662cb7fc400c6c3cc763e5d79a58f770335baba9 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 12 Apr 2022 23:03:20 +0300 Subject: [PATCH 2/4] comments with space --- Autoarr/Autoarr.c | 2 +- Autoarr/Autoarr.h | 2 +- DtsodParser/DtsodV24.c | 10 +++++----- DtsodParser/DtsodV24.h | 14 +++++++------- DtsodParser/DtsodV24_deserialize.c | 27 ++++++++++++++------------- DtsodParser/DtsodV24_exported.c | 18 +++++++++--------- DtsodParser/DtsodV24_serialize.c | 2 +- Hashtable/Hashtable.c | 2 +- Hashtable/Hashtable.h | 16 ++++++++-------- Hashtable/KeyValuePair.c | 4 ++-- Hashtable/KeyValuePair.h | 4 ++-- Hashtable/hash.h | 4 ++-- SearchTree/SearchTree.h | 2 +- StringFragment/StringBuilder.h | 2 +- StringFragment/StringFragment.c | 6 +++--- StringFragment/StringFragment.h | 10 +++++----- base/base.h | 4 ++-- base/cptr.c | 8 ++++---- base/cptr.h | 8 ++++---- base/errors.h | 2 +- base/types.c | 2 +- base/types.h | 2 +- 22 files changed, 76 insertions(+), 75 deletions(-) diff --git a/Autoarr/Autoarr.c b/Autoarr/Autoarr.c index e22daaf..d6ee333 100644 --- a/Autoarr/Autoarr.c +++ b/Autoarr/Autoarr.c @@ -12,7 +12,7 @@ define_Autoarr(float) define_Autoarr(double) define_Autoarr(Unitype) -//right func to clear array of unitype values +// right func to clear array of unitype values void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){ for(uint32 blockI=0;blockIblocks_count-1;blockI++) for(uint32 elemI=0;elemImax_block_length;elemI++) diff --git a/Autoarr/Autoarr.h b/Autoarr/Autoarr.h index 5f12633..1ecacd3 100644 --- a/Autoarr/Autoarr.h +++ b/Autoarr/Autoarr.h @@ -19,7 +19,7 @@ declare_Autoarr(float) declare_Autoarr(double) declare_Autoarr(Unitype) -//right func to clear array of unitype values +// right func to clear array of unitype values void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); #define Autoarr_foreach(ar,elem,codeblock)({\ diff --git a/DtsodParser/DtsodV24.c b/DtsodParser/DtsodV24.c index 21b0984..f5ce5b4 100644 --- a/DtsodParser/DtsodV24.c +++ b/DtsodParser/DtsodV24.c @@ -1,24 +1,24 @@ #include "DtsodV24.h" -//returns UniNull if key not found +// returns UniNull if key not found Unitype DtsodV24_get(Hashtable* dtsod, char* key){ return Hashtable_get(dtsod, key); } -//adds or sets value +// adds or sets value void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){ Unitype* val=Hashtable_getptr(dtsod, key); if(val) *val=value; else Hashtable_add(dtsod, key, value); } -//checks for dtsod contains value or dont +// checks for dtsod contains value or dont bool DtsodV24_contains(Hashtable* dtsod, char* key){ Unitype* val=Hashtable_getptr(dtsod, key); return val!=NULL; } -//replaces value with UniNull if key exists in dtsod +// replaces value with UniNull if key exists in dtsod bool DtsodV24_remove(Hashtable* dtsod, char* key){ Unitype* val=Hashtable_getptr(dtsod, key); if (!val) return false; @@ -26,7 +26,7 @@ bool DtsodV24_remove(Hashtable* dtsod, char* key){ return true; } -//frees memory including memory of elements (hashtables, autoarrs, etc.) +// frees memory including memory of elements (hashtables, autoarrs, etc.) void DtsodV24_free(Hashtable* dtsod){ Hashtable_free(dtsod); } \ No newline at end of file diff --git a/DtsodParser/DtsodV24.h b/DtsodParser/DtsodV24.h index 26a116c..b36b610 100644 --- a/DtsodParser/DtsodV24.h +++ b/DtsodParser/DtsodV24.h @@ -6,25 +6,25 @@ extern "C" { #include "../Hashtable/Hashtable.h" -//parses text to binary values +// parses text to binary values Maybe DtsodV24_deserialize(char* text); -//creates text representation of dtsod +// creates text representation of dtsod char* DtsodV24_serialize(Hashtable* dtsod); -//returns value or UniNull if key not found +// returns value or UniNull if key not found Unitype DtsodV24_get(Hashtable* dtsod, char* key); -//adds or sets value +// adds or sets value void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value); -//checks for dtsod contains value or dont +// checks for dtsod contains value or dont bool DtsodV24_contains(Hashtable* dtsod, char* key); -//replaces value with UniNull if key exists in dtsod +// replaces value with UniNull if key exists in dtsod bool DtsodV24_remove(Hashtable* dtsod, char* key); -//frees memory including memory of elements (hashtables, autoarrs, etc.) +// frees memory including memory of elements (hashtables, autoarrs, etc.) void DtsodV24_free(Hashtable* dtsod); #if __cplusplus diff --git a/DtsodParser/DtsodV24_deserialize.c b/DtsodParser/DtsodV24_deserialize.c index 686f8c5..980d4d2 100644 --- a/DtsodParser/DtsodV24_deserialize.c +++ b/DtsodParser/DtsodV24_deserialize.c @@ -6,13 +6,14 @@ #define STRB_BC 64 #define STRB_BL 1024 +// special func for throwing error messages about wrong characters in deserializing text Maybe ERROR_WRONGCHAR(const char c, char* text, char* text_first, const char* srcfile, int line, const char* funcname){ char errBuf[33]; errBuf[32]='\0'; char* errText=text-16; if(errText without quotes +// returns part of without quotes Maybe __ReadString(DeserializeSharedData* shared){ char c; bool prevIsBackslash=false; @@ -111,7 +112,7 @@ Maybe __ReadString(DeserializeSharedData* shared){ while ((c=*++text)){ if(c=='"') { if(prevIsBackslash) { - //replacing <\"> with <"> + // replacing <\"> with <"> Autoarr_remove(b); StringBuilder_append_char(b,c); } @@ -144,11 +145,11 @@ Maybe __ReadList(DeserializeSharedData* shared){ #define ReadList() __ReadList(shared) Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ - //printf("\e[94m<\e[96m%s\e[94m>\n",StringFragment_extract(str)); + // printf("\e[94m<\e[96m%s\e[94m>\n",StringFragment_extract(str)); const StringFragment trueStr= {"true" ,0,4}; const StringFragment falseStr={"false",0,5}; switch(str.ptr[str.length-1]){ - //Bool + // Bool case 'e': { if(StringFragment_compare(str,trueStr)) return SUCCESS(UniTrue); @@ -156,14 +157,14 @@ Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ return SUCCESS(UniFalse); else safethrow_wrongchar(*str.ptr); } - //Float64 + // Float64 case 'f': { char* _c=StringFragment_extract(str).ptr; Unitype rez=Uni(Float64,strtod(_c,NULL)); free(_c); return SUCCESS(rez); } - //UInt64 + // UInt64 case 'u': { uint64 lu=0; char* _c=StringFragment_extract(str).ptr; @@ -171,7 +172,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ free(_c); return SUCCESS(Uni(UInt64,lu)); } - //Int64 + // Int64 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int64 li=0; @@ -187,7 +188,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ free(_c); return SUCCESS(Uni(Int64,li)); } - //unknown type + // unknown type default: safethrow_wrongchar(str.ptr[str.length-1]); } @@ -217,7 +218,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){ try(SkipComment(),_); if(valueStr.length!=0) safethrow_wrongchar(_c); - valueStr.ptr=text+1; //skips '\n' + valueStr.ptr=text+1; // skips '\n' break; case '"': if(valueStr.length!=0) safethrow_wrongchar(c); @@ -233,7 +234,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){ break; case '{': if(valueStr.length!=0) safethrow_wrongchar(c); - ++text; //skips '{' + ++text; // skips '{' try(__deserialize(&text,true), val) return SUCCESS(val.value); case ';': @@ -267,7 +268,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { text--; while((c=*++text)){ try(ReadName(), maybeName) - if(!maybeName.value.VoidPtr) //end of file or '}' in recursive call + if(!maybeName.value.VoidPtr) // end of file or '}' in recursive call goto END; char* nameCPtr=maybeName.value.VoidPtr; try(ReadValue(), val) diff --git a/DtsodParser/DtsodV24_exported.c b/DtsodParser/DtsodV24_exported.c index 3ba9f31..1ffb67b 100644 --- a/DtsodParser/DtsodV24_exported.c +++ b/DtsodParser/DtsodV24_exported.c @@ -1,8 +1,8 @@ -// +// // I planned to export functions from DtsodV24.h, // but C# P/Invoke can't get returned values for some reason. // Following functions return values by pointer, which looks in C# like out parameter -// +// #if __cplusplus extern "C" { @@ -10,39 +10,39 @@ extern "C" { #include "DtsodV24.h" -//parses text to binary values +// parses text to binary values EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output, char** errmsg){ Maybe r=DtsodV24_deserialize(text); *errmsg= r.errmsg ? r.errmsg : NULL; *output=r.value.VoidPtr; } -//creates text representation of dtsod +// creates text representation of dtsod EXPORT void CALL kerep_DtsodV24_serialize(Hashtable* dtsod, char** output){ *output=DtsodV24_serialize(dtsod); } -//returns value or UniNull if key not found +// returns value or UniNull if key not found EXPORT void CALL kerep_DtsodV24_get(Hashtable* dtsod, char* key, Unitype* output){ *output=DtsodV24_get(dtsod, key); } -//adds or sets the value +// adds or sets the value EXPORT void CALL kerep_DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){ DtsodV24_addOrSet(dtsod, key, value); } -//checks for dtsod contains value or dont +// checks for dtsod contains value or dont EXPORT void CALL kerep_DtsodV24_contains(Hashtable* dtsod, char* key, bool* output){ *output=DtsodV24_contains(dtsod, key); } -//replaces value with UniNull if key exists in dtsod +// replaces value with UniNull if key exists in dtsod EXPORT void CALL kerep_DtsodV24_remove(Hashtable* dtsod, char* key, bool* output){ *output=DtsodV24_remove(dtsod, key); } -//replaces value with UniNull if key exists in dtsod +// replaces value with UniNull if key exists in dtsod EXPORT void CALL kerep_DtsodV24_free(Hashtable* dtsod){ DtsodV24_free(dtsod); } diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index bf87775..fcc612d 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -1,7 +1,7 @@ #include "DtsodV24.h" #include "../StringFragment/StringBuilder.h" -//65536 max length! +// 65536 max length! #define STRB_BC 64 #define STRB_BL 1024 diff --git a/Hashtable/Hashtable.c b/Hashtable/Hashtable.c index 9f66450..7bddbb6 100644 --- a/Hashtable/Hashtable.c +++ b/Hashtable/Hashtable.c @@ -64,7 +64,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){ Hashtable_add_pair(ht,KVPair(key,u)); } -//returns null or pointer to value in hashtable +// returns null or pointer to value in hashtable Unitype* Hashtable_getptr(Hashtable* ht, char* key){ Autoarr(KeyValuePair)* ar=getrow(ht,key,false); uint32 arlen=Autoarr_length(ar); diff --git a/Hashtable/Hashtable.h b/Hashtable/Hashtable.h index 14714de..90ab0ac 100644 --- a/Hashtable/Hashtable.h +++ b/Hashtable/Hashtable.h @@ -9,35 +9,35 @@ extern "C" { #include "KeyValuePair.h" typedef struct Hashtable{ - uint8 hein; //height=HT_HEIGHTS[hein] + uint8 hein; // height=HT_HEIGHTS[hein] Autoarr(KeyValuePair)* rows; // Autoarr[height] } Hashtable; Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); -//amount of rows +// amount of rows uint32 Hashtable_height(Hashtable* ht); -//adds charptr and value to new KeyValuePair -//use cptr_copy() to create new string if needed +// adds charptr and value to new KeyValuePair +// use cptr_copy() to create new string if needed #define KVPair(key,value) (KeyValuePair){key,value} -// +// // don't add pairs with the same keys, // or something weird will happen -// +// void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); void Hashtable_add(Hashtable* ht, char* key, Unitype u); -//returns null or pointer to value in hashtable +// returns null or pointer to value in hashtable Unitype* Hashtable_getptr(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key); KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); -//not implemented yet +// not implemented yet void Hashtable_set_pair(Hashtable* ht, KeyValuePair p); void Hashtable_set(Hashtable* ht, char* key, Unitype u); diff --git a/Hashtable/KeyValuePair.c b/Hashtable/KeyValuePair.c index c11ae9f..d07216e 100644 --- a/Hashtable/KeyValuePair.c +++ b/Hashtable/KeyValuePair.c @@ -3,13 +3,13 @@ define_Autoarr(KeyValuePair) -//proper way to clear a KVP +// proper way to clear a KVP void KeyValuePair_free(KeyValuePair p){ free(p.key); Unitype_free(p.value); } -//func for KVP array clearing +// func for KVP array clearing void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){ for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++) for(uint16 elemI=0; elemI < ar->max_block_length; elemI++) diff --git a/Hashtable/KeyValuePair.h b/Hashtable/KeyValuePair.h index 7f99dfb..afbc803 100644 --- a/Hashtable/KeyValuePair.h +++ b/Hashtable/KeyValuePair.h @@ -14,10 +14,10 @@ typedef struct KeyValuePair{ declare_Autoarr(KeyValuePair) -//proper way to clear a KVP +// proper way to clear a KVP void KeyValuePair_free(KeyValuePair p); -//func to clear KVP array +// func to clear KVP array void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar); void printkvp(KeyValuePair p); diff --git a/Hashtable/hash.h b/Hashtable/hash.h index b13db28..2f575c5 100644 --- a/Hashtable/hash.h +++ b/Hashtable/hash.h @@ -6,9 +6,9 @@ extern "C" { #include "../base/base.h" -//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html +// djb2 hash function from http:// www.cse.yorku.ca/~oz/hash.html uint32 ihash(char *str); -//sdbm hash function +// sdbm hash function uint64 lhash(char* str); #if __cplusplus diff --git a/SearchTree/SearchTree.h b/SearchTree/SearchTree.h index d7ae5a9..4d83bb1 100644 --- a/SearchTree/SearchTree.h +++ b/SearchTree/SearchTree.h @@ -7,7 +7,7 @@ extern "C" { #include "../base/base.h" typedef struct SearchTreeNode{ - struct SearchTreeNode**** branches; //*STNode[8][8][4] + struct SearchTreeNode**** branches; // *STNode[8][8][4] Unitype value; } STNode; diff --git a/StringFragment/StringBuilder.h b/StringFragment/StringBuilder.h index 8dff2db..9369973 100644 --- a/StringFragment/StringBuilder.h +++ b/StringFragment/StringBuilder.h @@ -16,7 +16,7 @@ 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 +// returns StringFragment with '\0' at the end StringFragment StringBuilder_build(StringBuilder* b); #if __cplusplus diff --git a/StringFragment/StringFragment.c b/StringFragment/StringFragment.c index 4cb6f2b..b3c0ca7 100644 --- a/StringFragment/StringFragment.c +++ b/StringFragment/StringFragment.c @@ -1,6 +1,6 @@ #include "StringFragment.h" -//copies characters from to new StringFragment (adding '\0' at the end) +// copies characters from to new StringFragment (adding '\0' at the end) StringFragment StringFragment_extract(StringFragment str){ if(str.length==0) return stringNull; StringFragment extr={ @@ -15,7 +15,7 @@ StringFragment StringFragment_extract(StringFragment str){ return extr; } -//compares two strings, NullPtr-friendly +// 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; @@ -28,7 +28,7 @@ bool StringFragment_compare(StringFragment str0, StringFragment str1){ return true; } -//creates new StringFragment which is reversed variant of +// creates new StringFragment which is reversed variant of StringFragment StringFragment_reverse(StringFragment s){ if(s.length==0) return s; StringFragment r={ diff --git a/StringFragment/StringFragment.h b/StringFragment/StringFragment.h index 9cfbcd4..9331413 100644 --- a/StringFragment/StringFragment.h +++ b/StringFragment/StringFragment.h @@ -6,22 +6,22 @@ extern "C" { #include "../base/base.h" -//part of string +// part of string typedef struct StringFragment{ - char* ptr; //may be without '\0' at the end + char* ptr; // may be without '\0' at the end uint32 offset; uint32 length; } StringFragment; static const StringFragment stringNull={NULL,0,0}; -//copies characters from to new StringFragment (adding '\0' at the end) +// copies characters from to new StringFragment (adding '\0' at the end) StringFragment StringFragment_extract(StringFragment str); -//compares two strings, NullPtr-friendly +// compares two strings, NullPtr-friendly bool StringFragment_compare(StringFragment str0, StringFragment str1); -//creates new StringFragment which is reversed variant of +// creates new StringFragment which is reversed variant of StringFragment StringFragment_reverse(StringFragment s); #if __cplusplus diff --git a/base/base.h b/base/base.h index b23b92d..4e2708e 100644 --- a/base/base.h +++ b/base/base.h @@ -10,7 +10,7 @@ extern "C" { #include "cptr.h" // executes codeblock and prints execution time -#ifdef CLOCK_REALTIME //non-standard high-precision clock +#ifdef CLOCK_REALTIME // non-standard high-precision clock #define optime(opname,repeats,codeblock) ({\ struct timespec start, stop;\ clock_gettime(CLOCK_REALTIME, &start);\ @@ -20,7 +20,7 @@ extern "C" { double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ }) -#else // +#else // standard clock which works worse then previous #define optime(opname,repeats,codeblock) ({\ clock_t start=clock();\ for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ diff --git a/base/cptr.c b/base/cptr.c index 0646b15..50dbcdf 100644 --- a/base/cptr.c +++ b/base/cptr.c @@ -1,13 +1,13 @@ #include "base.h" -//returns length of string (without \0) +// returns length of string (without \0) uint32 cptr_length(char* str){ uint32 len=0; while(*(str++)) len++; return len; } -//allocates new char[] and copies src there +// allocates new char[] and copies src there char* cptr_copy(char* src){ uint32 len=cptr_length(src)+1; char* dst=malloc(len); @@ -16,7 +16,7 @@ char* cptr_copy(char* src){ return dst; } -//compares two char buffers, NullPtr-friendly +// compares two char buffers, NullPtr-friendly bool cptr_compare(char* key0, char* key1){ if(!key0) return key1 ? false : true; if(!key1) return false; @@ -26,7 +26,7 @@ bool cptr_compare(char* key0, char* key1){ return true; } -//multiplies char n times +// multiplies char n times char* char_multiply(char c, uint32 n){ char* rez=malloc(n+1); rez[n]=0; diff --git a/base/cptr.h b/base/cptr.h index 46f6679..93829fc 100644 --- a/base/cptr.h +++ b/base/cptr.h @@ -6,16 +6,16 @@ extern "C" { #include "types.h" -//returns length of string (without \0) +// returns length of string (without \0) uint32 cptr_length(char* str); -//allocates new char[] and copies src there +// allocates new char[] and copies src there char* cptr_copy(char* src); -//compares two char buffers, NullPtr-friendly +// compares two char buffers, NullPtr-friendly bool cptr_compare(char* key0, char* key1); -//multiplies char n times +// multiplies char n times char* char_multiply(char c, uint32 n); #if __cplusplus diff --git a/base/errors.h b/base/errors.h index 88f63b0..309b060 100644 --- a/base/errors.h +++ b/base/errors.h @@ -8,7 +8,7 @@ extern "C" { #include "types.h" typedef enum err_t { - SUCCESS, //not an error + SUCCESS, // not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR } err_t; diff --git a/base/types.c b/base/types.c index f0197c7..36504a7 100644 --- a/base/types.c +++ b/base/types.c @@ -45,7 +45,7 @@ const char* my_type_name(my_type t){ } } -//frees VoidPtr value or does nothing if type isn't pointer +// frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u){ switch (u.type) { case Null: diff --git a/base/types.h b/base/types.h index bbbf2f6..2ea0b48 100644 --- a/base/types.h +++ b/base/types.h @@ -47,7 +47,7 @@ static const Unitype UniFalse={.Bool=false,.type=Bool}; #define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} #define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL} -//frees VoidPtr value or does nothing if type isn't pointer +// frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); void printuni(Unitype v); void sprintuni(char* s, Unitype v); From d611287c1a7ae5842ee18971365159c79a5ae5a5 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Tue, 12 Apr 2022 23:09:13 +0300 Subject: [PATCH 3/4] visual studio project fixed --- kerep.vcxproj | 10 ++++++---- kerep.vcxproj.filters | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/kerep.vcxproj b/kerep.vcxproj index 5ed9303..11e08e8 100644 --- a/kerep.vcxproj +++ b/kerep.vcxproj @@ -204,10 +204,9 @@ - + - @@ -215,6 +214,8 @@ + + @@ -222,9 +223,8 @@ - + - @@ -234,6 +234,8 @@ + + diff --git a/kerep.vcxproj.filters b/kerep.vcxproj.filters index 802eb88..cdaa217 100644 --- a/kerep.vcxproj.filters +++ b/kerep.vcxproj.filters @@ -63,6 +63,15 @@ Файлы заголовков + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + @@ -134,5 +143,14 @@ Исходные файлы + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + \ No newline at end of file From e1addc40045b53ee45ddd446a7ad7772e3eaf5c1 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 16 Apr 2022 23:51:05 +0300 Subject: [PATCH 4/4] go back to string --- {StringFragment => Autoarr}/StringBuilder.c | 27 +++-- Autoarr/StringBuilder.h | 22 ++++ DtsodParser/DtsodV24_deserialize.c | 106 +++++++++++++------- DtsodParser/DtsodV24_serialize.c | 9 +- base/base.h | 2 +- base/errors.c | 2 +- base/mystr.c | 77 ++++++++++++++ base/mystr.h | 44 ++++++++ tests/main.c | 2 +- tests/test_string.c | 24 ++--- 10 files changed, 242 insertions(+), 73 deletions(-) rename {StringFragment => Autoarr}/StringBuilder.c (70%) create mode 100644 Autoarr/StringBuilder.h create mode 100644 base/mystr.c create mode 100644 base/mystr.h diff --git a/StringFragment/StringBuilder.c b/Autoarr/StringBuilder.c similarity index 70% rename from StringFragment/StringBuilder.c rename to Autoarr/StringBuilder.c index 4066c5a..6a12265 100644 --- a/StringFragment/StringBuilder.c +++ b/Autoarr/StringBuilder.c @@ -14,10 +14,11 @@ void StringBuilder_append_cptr(StringBuilder* b, char* s){ Autoarr_add(b,c); } -void StringBuilder_append_string(StringBuilder* b, StringFragment s){ - s.ptr+=s.offset; - while(s.length-->0) +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){ @@ -35,7 +36,7 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){ buf[i++]='0'+a%10; a/=10; } - StringFragment rev=StringFragment_reverse((StringFragment){buf,0,i}); + string rev=string_reverse((string){buf,i}); StringBuilder_append_string(b,rev); free(rev.ptr); } @@ -51,7 +52,8 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ buf[i++]='0'+a%10; a/=10; } - StringFragment rev=StringFragment_reverse((StringFragment){buf,0,i}); + string rev=string_reverse((string){buf,i}); + printf("\e[95mrev:%s\n",string_cpToCptr(rev)); StringBuilder_append_string(b,rev); free(rev.ptr); } @@ -65,14 +67,11 @@ void StringBuilder_append_double(StringBuilder* b, double 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 at:\n" - " \"%s\"\n" - "\\___[%s:%d] %s()", - c,errBuf, srcfile,line,funcname), - sprintf(errmsg, "unexpected <%c> at:\n" - " \"%s\"\n" - " \\___[%s:%d] %s()", - c,errBuf, srcfile,line,funcname) - ); - safethrow(cptr_copy(errmsg)); -} -#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text, shared->sh_text_first, __FILE__,__LINE__,__func__) + errBuf[i+22]=*(text - 16 + i); + safethrow(cptr_copy(errBuf)); +#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text) typedef struct DeserializeSharedData{ - const char* sh_text_first; char* sh_text; bool sh_partOfDollarList; bool sh_readingList; @@ -56,7 +38,7 @@ Maybe __SkipComment(DeserializeSharedData* shared) { Maybe __ReadName(DeserializeSharedData* shared){ char c; - StringFragment nameStr={text,0}; + string nameStr={text,0}; text--; while ((c=*++text)) switch (c){ case ' ': case '\t': @@ -82,7 +64,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ if((*++text)!=';') safethrow_wrongchar(c); case ':': - return SUCCESS(UniPtr(CharPtr,StringFragment_extract(nameStr).ptr)); + return SUCCESS(UniPtr(CharPtr,string_cpToCptr(nameStr))); case '$': if(nameStr.length!=0) safethrow_wrongchar(c); @@ -117,9 +99,9 @@ Maybe __ReadString(DeserializeSharedData* shared){ StringBuilder_append_char(b,c); } else { - StringFragment str=StringBuilder_build(b); + char* str=StringBuilder_build(b); Autoarr_clear(b); - return SUCCESS(UniPtr(CharPtr,str.ptr)); + return SUCCESS(UniPtr(CharPtr,str)); } } else { @@ -144,6 +126,7 @@ Maybe __ReadList(DeserializeSharedData* shared){ }; #define ReadList() __ReadList(shared) +<<<<<<< HEAD Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ // printf("\e[94m<\e[96m%s\e[94m>\n",StringFragment_extract(str)); const StringFragment trueStr= {"true" ,0,4}; @@ -152,10 +135,29 @@ Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ // Bool case 'e': { if(StringFragment_compare(str,trueStr)) +======= +Maybe __ParseValue(DeserializeSharedData* shared, string str){ + //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCptr(str)); + const string nullStr={"null",4}; + const string trueStr={"true",4}; + const string falseStr={"false",5}; + switch(*str.ptr){ + case 'n': + if(string_compare(str,nullStr)) + return SUCCESS(UniNull); + else safethrow_wrongchar(*str.ptr); + break; + case 't': + if(string_compare(str,trueStr)) +>>>>>>> parent of 41f32f4 (string -> StringFragment, throw_wrongchar() fixed) return SUCCESS(UniTrue); - else if(StringFragment_compare(str,falseStr)) + else safethrow_wrongchar(*str.ptr); + break; + case 'f': + if(string_compare(str,falseStr)) return SUCCESS(UniFalse); else safethrow_wrongchar(*str.ptr); +<<<<<<< HEAD } // Float64 case 'f': { @@ -191,15 +193,50 @@ Maybe __ParseValue(DeserializeSharedData* shared, StringFragment str){ // unknown type default: safethrow_wrongchar(str.ptr[str.length-1]); +======= + break; + default: + switch(str.ptr[str.length-1]){ + case 'f': { + char* _c=string_cpToCptr(str); + Unitype rez=Uni(Float64,strtod(_c,NULL)); + free(_c); + return SUCCESS(rez); + } + case 'u': { + uint64 lu=0; + char* _c=string_cpToCptr(str); + sscanf(_c,"%lu",&lu); + free(_c); + return SUCCESS(Uni(UInt64,lu)); + } + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': { + int64 li=0; + char* _c=string_cpToCptr(str); + if(sscanf(_c,"%li",&li)!=1){ + char err[64]; + IFWIN( + sprintf_s(err,64,"can't parse to int: <%s>",_c), + sprintf(err,"can't parse to int: <%s>",_c) + ); + safethrow(err); + } + free(_c); + return SUCCESS(Uni(Int64,li)); + } + default: + safethrow_wrongchar(str.ptr[str.length-1]); + } +>>>>>>> parent of 41f32f4 (string -> StringFragment, throw_wrongchar() fixed) } - safethrow(ERR_ENDOFSTR); }; #define ParseValue(str) __ParseValue(shared, str) Maybe __ReadValue(DeserializeSharedData* shared){ char c; - StringFragment valueStr={text+1,0}; + string valueStr={text+1,0}; Unitype value; while ((c=*++text)) switch (c){ case ' ': case '\t': @@ -255,7 +292,6 @@ Maybe __ReadValue(DeserializeSharedData* shared){ Maybe __deserialize(char** _text, bool _calledRecursively) { DeserializeSharedData _shared={ - .sh_text_first=*_text, .sh_text=*_text, .sh_partOfDollarList=false, .sh_readingList=false, diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index fcc612d..0847646 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -1,5 +1,5 @@ #include "DtsodV24.h" -#include "../StringFragment/StringBuilder.h" +#include "../Autoarr/StringBuilder.h" // 65536 max length! #define STRB_BC 64 @@ -51,7 +51,8 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){ StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); break; case Null: - throw("Null isn't supported in DtsodV24"); + if(!u.VoidPtr) StringBuilder_append_cptr(b, "null"); + else throw("Null-type pointer is not 0"); break; case AutoarrUnitypePtr: addc('['); @@ -96,7 +97,7 @@ void __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){ char* DtsodV24_serialize(Hashtable* dtsod){ StringBuilder sb=StringBuilder_create(STRB_BC,STRB_BL); __serialize(&sb,0,dtsod); - StringFragment str=StringBuilder_build(&sb); + char* str=StringBuilder_build(&sb); Autoarr_clear((&sb)); - return str.ptr; + return str; } diff --git a/base/base.h b/base/base.h index 4e2708e..ff07fdd 100644 --- a/base/base.h +++ b/base/base.h @@ -7,7 +7,7 @@ extern "C" { #include "std.h" #include "types.h" #include "errors.h" -#include "cptr.h" +#include "mystr.h" // executes codeblock and prints execution time #ifdef CLOCK_REALTIME // non-standard high-precision clock diff --git a/base/errors.c b/base/errors.c index f56687a..d10d5d1 100644 --- a/base/errors.c +++ b/base/errors.c @@ -1,6 +1,6 @@ #include "std.h" #include "errors.h" -#include "cptr.h" +#include "mystr.h" char* errname(err_t err){ switch(err){ diff --git a/base/mystr.c b/base/mystr.c new file mode 100644 index 0000000..5503b46 --- /dev/null +++ b/base/mystr.c @@ -0,0 +1,77 @@ +#include "base.h" + +//returns length of string (including \0) +uint32 cptr_length(char* str){ + uint32 len=0; + while(*(str++)) len++; + return ++len; +} + +//allocates new char[] and copies src there +char* cptr_copy(char* src){ + uint32 len=cptr_length(src); + char* dst=malloc(len*sizeof(char)); + while(len-->0) + dst[len]=src[len]; + return dst; +} + +//compares two char buffers, NullPtr-friendly +bool cptr_compare(char* key0, char* key1){ + if(!key0) return key1 ? false : true; + if(!key1) return false; + while(*key0&&*key1) + if(*key0++ != *key1++) + return false; + return true; +} + +//multiplies char n times +char* char_multiply(char c, uint32 n){ + char* rez=malloc(n+1); + rez[n]=0; + while(n-->0) + rez[n]=c; + return rez; +} + +//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;i0) + if(*str0.ptr++ != *str1.ptr++) + return false; + return true; +} + +//creates new string which is reversed variant of +string string_reverse(string s){ + if(s.length==0) return s; + string r={malloc(s.length), s.length}; + for(uint32 i=0; i +string string_reverse(string s); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index 6b3b54b..4b3149d 100644 --- a/tests/main.c +++ b/tests/main.c @@ -13,7 +13,7 @@ void test_all(){ int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); - optime("test_all",1,test_all()); + optime("test_all",1,{test_all();}); printf("\e[0m\n"); return 0; } diff --git a/tests/test_string.c b/tests/test_string.c index 5783047..e9e249f 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -1,26 +1,16 @@ #include "tests.h" -#include "../StringFragment/StringFragment.h" +#include "../base/mystr.h" void test_string(){ optime(__func__,1,({ printf("\e[96m-------------[test_string]-------------\n"); char c[]="0123456789abcdef"; - - StringFragment s={.ptr=cptr_copy(c),.offset=0,.length=cptr_length(c)}; - printf("\e[92m\"%s\" \e[94m-> StringFragment\n",c); - if(s.length!=16) throw("StringFragment created with incorrect length"); - - StringFragment extr=StringFragment_extract(s); - printf("\e[94mStringFragment_extract() -> \e[92m\"%s\"\n",extr.ptr); - if(extr.length!=16) throw("StringFragment extracted with incorrect length"); - free(extr.ptr); - - s.offset+=2; s.length-=4; - printf("\e[94mStringFragment offset+=2 length-=4\n"); - extr=StringFragment_extract(s); - printf("\e[94mStringFragment_extract() -> \e[92m\"%s\"\n",extr.ptr); - if(extr.length!=12) throw("StringFragment extracted with incorrect length"); + string s=string_cpFromCharPtr(c); + printf("\e[92m\"%s\" \e[94m-> string_cpFromCharPtr()\n",c); + if(s.length!=16) throw("string created with incorrect length"); + char* p=string_cpToCptr(s); + printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p); + free(p); free(s.ptr); - free(extr.ptr); })); } \ No newline at end of file