From 2434a126da7389443a224d617502a6e6a7c22cf3 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Tue, 19 Apr 2022 20:50:44 +0300 Subject: [PATCH] some code moved to String directory --- Autoarr/StringBuilder.h | 24 ------------------- DtsodParser/DtsodV24_deserialize.c | 2 +- DtsodParser/DtsodV24_serialize.c | 2 +- {Autoarr => String}/StringBuilder.c | 0 String/StringBuilder.h | 25 +++++++++++++++++++ base/mystr.c => String/string.c | 37 +---------------------------- base/mystr.h => String/string.h | 14 +---------- base/base.h | 2 +- base/cptr.c | 36 ++++++++++++++++++++++++++++ base/cptr.h | 23 ++++++++++++++++++ base/errors.c | 2 +- kerep.vcxproj | 2 +- kerep.vcxproj.filters | 2 +- tests/test_string.c | 2 +- 14 files changed, 93 insertions(+), 80 deletions(-) delete mode 100644 Autoarr/StringBuilder.h rename {Autoarr => String}/StringBuilder.c (100%) create mode 100644 String/StringBuilder.h rename base/mystr.c => String/string.c (60%) rename base/mystr.h => String/string.h (68%) create mode 100644 base/cptr.c create mode 100644 base/cptr.h diff --git a/Autoarr/StringBuilder.h b/Autoarr/StringBuilder.h deleted file mode 100644 index 96378f0..0000000 --- a/Autoarr/StringBuilder.h +++ /dev/null @@ -1,24 +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_free(StringBuilder* b); - void StringBuilder_pop(StringBuilder* b); - 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 5cf65e1..6e1de1c 100644 --- a/DtsodParser/DtsodV24_deserialize.c +++ b/DtsodParser/DtsodV24_deserialize.c @@ -1,5 +1,5 @@ #include "DtsodV24.h" -#include "../Autoarr/StringBuilder.h" +#include "../String/StringBuilder.h" #define ARR_BC 64 #define ARR_BL 1024 diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index 13c7fc9..ab09ffd 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -1,5 +1,5 @@ #include "DtsodV24.h" -#include "../Autoarr/StringBuilder.h" +#include "../String/StringBuilder.h" // 65536 max length! #define STRB_BC 64 diff --git a/Autoarr/StringBuilder.c b/String/StringBuilder.c similarity index 100% rename from Autoarr/StringBuilder.c rename to String/StringBuilder.c diff --git a/String/StringBuilder.h b/String/StringBuilder.h new file mode 100644 index 0000000..6cdac39 --- /dev/null +++ b/String/StringBuilder.h @@ -0,0 +1,25 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../Autoarr/Autoarr.h" +#include "../String/string.h" + +typedef Autoarr(int8) StringBuilder; + +StringBuilder* StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length); +void StringBuilder_free(StringBuilder* b); +void StringBuilder_pop(StringBuilder* b); +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/base/mystr.c b/String/string.c similarity index 60% rename from base/mystr.c rename to String/string.c index 520ed9c..511fa2b 100644 --- a/base/mystr.c +++ b/String/string.c @@ -1,39 +1,4 @@ -#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; -} +#include "../String/string.h" // copies str content to new char pointer value (adding '\0' at the end) char* string_cpToCptr(string str){ diff --git a/base/mystr.h b/String/string.h similarity index 68% rename from base/mystr.h rename to String/string.h index f5e0f56..43c60b7 100644 --- a/base/mystr.h +++ b/String/string.h @@ -4,19 +4,7 @@ extern "C" { #endif -#include "types.h" - -// returns length of string (including \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); +#include "../base/base.h" // my fixed length string struct // doesn't store '\0' at the end diff --git a/base/base.h b/base/base.h index 7986532..f9d56b8 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..75a0ed7 --- /dev/null +++ b/base/cptr.c @@ -0,0 +1,36 @@ +#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; +} diff --git a/base/cptr.h b/base/cptr.h new file mode 100644 index 0000000..50cd173 --- /dev/null +++ b/base/cptr.h @@ -0,0 +1,23 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "types.h" + +// returns length of string (including \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/kerep.vcxproj b/kerep.vcxproj index 5ed9303..8bf4ec8 100644 --- a/kerep.vcxproj +++ b/kerep.vcxproj @@ -207,7 +207,7 @@ - + diff --git a/kerep.vcxproj.filters b/kerep.vcxproj.filters index 802eb88..362d871 100644 --- a/kerep.vcxproj.filters +++ b/kerep.vcxproj.filters @@ -33,7 +33,7 @@ Файлы заголовков - + Файлы заголовков diff --git a/tests/test_string.c b/tests/test_string.c index e9e249f..1a8a523 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -1,5 +1,5 @@ #include "tests.h" -#include "../base/mystr.h" +#include "../String/string.h" void test_string(){ optime(__func__,1,({