StringBuilder

This commit is contained in:
Timerix22 2022-02-23 23:27:43 +03:00
parent 99da265217
commit 89cc9583af
9 changed files with 114 additions and 22 deletions

View File

@ -15,3 +15,4 @@ declare_Autoarr2(float)
declare_Autoarr2(double) declare_Autoarr2(double)
declare_Autoarr2(Unitype) declare_Autoarr2(Unitype)

View File

@ -0,0 +1,24 @@
#include "StringBuilder.h"
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length){
return Autoarr2_create(int8,max_blocks_count,max_block_length);
}
void StringBuilder_append(StringBuilder* b, char c){
Autoarr2_add(b,c);
}
void StringBuilder_append_str(StringBuilder* b, char* s){
char c;
while((c=*s++))
Autoarr2_add(b,c);
}
char* StringBuilder_build(StringBuilder* b){
uint32 len=Autoarr2_length(b);
char* str=malloc(len+1);
str[len]=0;
for(uint32 i=0;i<len;i++)
str[i]=Autoarr2_get(b,i);
return str;
}

View File

@ -0,0 +1,10 @@
#pragma once
#include "Autoarr2.h"
typedef Autoarr2(int8) StringBuilder;
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
void StringBuilder_append(StringBuilder* b, char c);
void StringBuilder_append_str(StringBuilder* b, char* s);
char* StringBuilder_build(StringBuilder* b);

View File

@ -1,29 +1,82 @@
#include "DtsodV24.h" #include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h"
#define ARR_BC 2
#define ARR_BL 8
Hashtable* __parse(const char* text, bool called_recursively){ Hashtable* __parse(const char* text, bool called_recursively){
Hashtable* dict=Hashtable_create(); Hashtable* dict=Hashtable_create();
/* char c; char c;
bool partOfDollarList=false;
void SkipComment(){
while((c=*text++)!='\n')
if(!c) throw(ERR_ENDOFSTR);
};
char* ReadName(){ char* ReadName(){
StringBuilder b=StringBuilder_create(4,32);
while ((c=*text)){
switch (c){
case ' ':
case '\t':
case '\r':
case '\n':
break;
case '#':
SkipComment();
break;
case ':':
return StringBuilder_build(&b);
case '$':
partOfDollarList=true;
break;
case '=': case ';':
case '\'': case '"':
case '[': case ']':
case '{': case '}':
char errstr[]="unexpected <c>";
errstr[12]=c;
throw(errstr);
break;
default:
StringBuilder_append(&b,c);
break;
}
text++;
}
if(Autoarr2_length((&b))>0) throw(ERR_ENDOFSTR);
return NULL;
}; };
Unitype ReadValue(){ Unitype ReadValue(){
Unitype value=UniNull;
}; return value;
void SkipComment(){
}; };
while ((c=*text)){ while((c=*text++)){
char* n=ReadName(); char* name=ReadName();
Unitype v=ReadValue(); if(!name){
Hashtable_add(dict,n,v); free(name);
text++; return dict;
} */ }
Unitype value=ReadValue();
if(partOfDollarList){
Autoarr2(Unitype)* list;
Unitype _lu;
if(Hashtable_try_get(dict,name, &_lu))
list=(Autoarr2(Unitype)*)_lu.VoidPtr;
else {
list=malloc(sizeof(Autoarr2(Unitype)));
*list=Autoarr2_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,name,UniF(AutoarrUnitypePtr,VoidPtr,list));
}
Autoarr2_add(list,value);
}
else Hashtable_add(dict,name,value);
}
return dict; return dict;
} }

View File

@ -88,7 +88,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){
KeyValuePair p=Autoarr2_get(ar,i); KeyValuePair p=Autoarr2_get(ar,i);
if(mystrcmp(key,p.key)) return p.value; if(mystrcmp(key,p.key)) return p.value;
} }
return (Unitype){.type=Null,.VoidPtr=NULL}; return UniNull;
} }
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){ KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
return cpair(key,Hashtable_get(ht,key)); return cpair(key,Hashtable_get(ht,key));

View File

@ -89,20 +89,18 @@ void ST_push(STNode* node_first, const char* key, Unitype value){
node_last->value=value; node_last->value=value;
} }
const Unitype UnitypeNull={.type=Null,.VoidPtr=NULL};
Unitype ST_pull(STNode* node_first, const char* key){ Unitype ST_pull(STNode* node_first, const char* key){
if (!node_first) throw(ERR_NULLPTR); if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first; STNode* node_last=node_first;
while (*key){ while (*key){
indexes3 i3=splitindex((uint8)*key); indexes3 i3=splitindex((uint8)*key);
if(!node_last->branches) return UnitypeNull; if(!node_last->branches) return UniNull;
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32) return UnitypeNull; if(!ptrn32) return UniNull;
STNode** ptrn4=ptrn32[i3.n4]; STNode** ptrn4=ptrn32[i3.n4];
if(!ptrn4) return UnitypeNull; if(!ptrn4) return UniNull;
node_last=ptrn4[i3.rem]; node_last=ptrn4[i3.rem];
if(!node_last) return UnitypeNull; if(!node_last) return UniNull;
key++; key++;
} }
return node_last->value; return node_last->value;

View File

@ -9,6 +9,7 @@ const char* errname(err_t err){
case ERR_WRONGINDEX: return "ERR_WRONGINDEX"; case ERR_WRONGINDEX: return "ERR_WRONGINDEX";
case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED"; case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
case ERR_NULLPTR: return "ERR_NULLPTR"; case ERR_NULLPTR: return "ERR_NULLPTR";
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
default: return "UNKNOWN_ERROR"; default: return "UNKNOWN_ERROR";
} }
} }

View File

@ -2,7 +2,7 @@
typedef enum err_t { typedef enum err_t {
SUCCESS, //not an error SUCCESS, //not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t; } err_t;
const char* errname(err_t err); const char* errname(err_t err);

View File

@ -14,7 +14,10 @@ typedef enum my_type{
Null, Float, Double, Char, Bool, Null, Float, Double, Char, Bool,
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr, UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
UniversalType, STNodePtr UniversalType, STNodePtr,
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
AutoarrUnitypePtr, AutoarrKVPairPtr
} __attribute__ ((__packed__)) my_type; } __attribute__ ((__packed__)) my_type;
//returns type name //returns type name
@ -42,3 +45,5 @@ typedef struct Unitype{
} Unitype; } Unitype;
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} #define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
#define UniF(TYPE,FIELD,VAL) (Unitype){.type=TYPE,.FIELD=VAL}
#define UniNull (Unitype){Null,.VoidPtr=NULL}