comments and <\"> fixed

This commit is contained in:
Timerix22 2022-03-09 00:37:55 +03:00
parent 3cb878c75f
commit 8819853e15
6 changed files with 51 additions and 21 deletions

View File

@ -55,7 +55,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
void StringBuilder_append_double(StringBuilder* b, double a){ void StringBuilder_append_double(StringBuilder* b, double a){
char buf[32]; char buf[32];
sprintf(buf,32,"%f",a); sprintf(buf,"%lf",a);
StringBuilder_append_cptr(b,buf); StringBuilder_append_cptr(b,buf);
} }

View File

@ -1,7 +1,10 @@
#include "DtsodV24.h" #include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h"
#define ARR_BC 8 #define ARR_BC 8
#define ARR_BL 16 #define ARR_BL 16
#define STRB_BC 64
#define STRB_BL 1024
Hashtable* __deserialize(char** _text, bool calledRecursively){ Hashtable* __deserialize(char** _text, bool calledRecursively){
Hashtable* dict=Hashtable_create(); Hashtable* dict=Hashtable_create();
@ -47,7 +50,7 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
SkipComment(); SkipComment();
if(nameStr.length!=0) if(nameStr.length!=0)
throw_wrongchar(c); throw_wrongchar(c);
nameStr.ptr=text; nameStr.ptr=text+1; //skips '\n'
break; break;
case '}': case '}':
if(!calledRecursively) throw_wrongchar(c); if(!calledRecursively) throw_wrongchar(c);
@ -72,15 +75,26 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
Unitype ReadValue(){ Unitype ReadValue(){
//returns part of <text> without quotes //returns part of <text> without quotes
string ReadString(){ char* ReadString(){
bool prevIsBackslash=false; bool prevIsBackslash=false;
string str={text+1,0}; StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL);
while ((c=*++text)!='"' || prevIsBackslash){ StringBuilder* b=&_b;
if (!c) throw(ERR_ENDOFSTR); while ((c=*++text)){
prevIsBackslash= c=='\\' && !prevIsBackslash; if(c=='"') {
str.length++; if(prevIsBackslash) {
//replacing <\"> with <">
Autoarr_remove(b);
StringBuilder_append_char(b,c);
}
else return StringBuilder_build(b);
}
else {
prevIsBackslash= c=='\\' && !prevIsBackslash;
StringBuilder_append_char(b,c);
}
} }
return str; throw(ERR_ENDOFSTR);
return NULL;
}; };
Autoarr(Unitype)* ReadList(){ Autoarr(Unitype)* ReadList(){
@ -174,11 +188,11 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
SkipComment(); SkipComment();
if(valueStr.length!=0) if(valueStr.length!=0)
throw_wrongchar(_c); throw_wrongchar(_c);
valueStr.ptr=text; valueStr.ptr=text+1; //skips '\n'
break; break;
case '"': case '"':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) throw_wrongchar(c);
value=UniPtr(CharPtr,string_cpToCharPtr(ReadString())); value=UniPtr(CharPtr,ReadString());
break; break;
case '\'': case '\'':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) throw_wrongchar(c);

View File

@ -10,7 +10,7 @@
void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){ void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
void AppendTabs(){ void AppendTabs(){
for(uint8 t; t<tabs; t++) for(uint8 t=0; t<tabs; t++)
addc(b,'\t'); addc(b,'\t');
}; };
@ -25,11 +25,15 @@ void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
break; break;
case Double: case Double:
StringBuilder_append_double(b,u.Double); StringBuilder_append_double(b,u.Double);
addc(b,'d'); addc(b,'f');
break; break;
case CharPtr: case CharPtr:
addc(b,'"'); addc(b,'"');
StringBuilder_append_cptr(b,u.VoidPtr); char c;
while((c=*(char*)(u.VoidPtr++))){
if(c=='\"') addc(b,'\\');
addc(b,c);
}
addc(b,'"'); addc(b,'"');
break; break;
case Char: case Char:
@ -37,6 +41,13 @@ void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
addc(b,u.Char); addc(b,u.Char);
addc(b,'\''); addc(b,'\'');
break; break;
case Bool:
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");
break;
case AutoarrUnitypePtr: case AutoarrUnitypePtr:
addc(b,'['); addc(b,'[');
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({ Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
@ -55,7 +66,7 @@ void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
AppendTabs(); AppendTabs();
addc(b,'}'); addc(b,'}');
break; break;
default: throw(ERR_WRONGTYPE); default: dbg((u.type)); throw(ERR_WRONGTYPE);
} }
}; };

View File

@ -70,7 +70,7 @@ bool string_compare(string str0, string str1){
//creates new string which is reversed variant of <s> //creates new string which is reversed variant of <s>
string string_reverse(string s){ string string_reverse(string s){
string r={malloc(s.length), s.length}; string r={malloc(s.length), s.length};
for(uint32 i; i<s.length; i++) for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1]; r.ptr[i]=s.ptr[s.length-i-1];
return r; return r;
} }

View File

@ -2,10 +2,10 @@
#include "tests.h" #include "tests.h"
void test_all(){ void test_all(){
test_searchtree(); /* test_searchtree();
test_autoarr(); test_autoarr();
test_hashtable(); test_hashtable();
test_string(); test_string(); */
test_dtsod(); test_dtsod();
printf("\e[96m---------------------------------------\e[0m\n"); printf("\e[96m---------------------------------------\e[0m\n");
} }

View File

@ -8,12 +8,12 @@ const char text[]=
" int: -2515;" " int: -2515;"
" uint:#comment!\n 0u;" " uint:#comment!\n 0u;"
" double: 965.557f;#another comment!\n" " double: 965.557f;#another comment!\n"
" text: \"_$\\\"\\\\'''\n\ta ыыы000;2;=:%d;```\";\n" " text: \"_$\\\"\\\\'''a ыыы000;2;=:%d;```\";\n"
"}; "; "}; ";
void test_dtsod(){ void test_dtsod(){
printf("\e[96m-------------[test_dtsod]-------------\n"); printf("\e[96m-------------[test_dtsod]-------------\n");
optime(__func__,1,({ optime(__func__,200,({
Hashtable* dtsod; Hashtable* dtsod;
optime("deserialize",1,(dtsod=DtsodV24_deserialize(text))); optime("deserialize",1,(dtsod=DtsodV24_deserialize(text)));
Hashtable_foreach(dtsod, p,({ Hashtable_foreach(dtsod, p,({
@ -29,7 +29,12 @@ void test_dtsod(){
printf("}"); printf("}");
} }
printf("\n"); printf("\n");
char* s=DtsodV24_serialize(dtsod);
printf("\e[93m\n%s",s);
dtsod=DtsodV24_deserialize(s);
Hashtable_free(dtsod);
})); }));
Hashtable_free(dtsod);
})); }));
} }