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){
char buf[32];
sprintf(buf,32,"%f",a);
sprintf(buf,"%lf",a);
StringBuilder_append_cptr(b,buf);
}

View File

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

View File

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

View File

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

View File

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