updated throw_wrongchar(), removoved null parsing, commits cleared

This commit is contained in:
2022-04-17 00:50:24 +03:00
parent b4f2ca92c7
commit 394fbffc12
21 changed files with 137 additions and 128 deletions

View File

@@ -10,7 +10,7 @@ extern "C" {
#include "mystr.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 //
#define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\

View File

@@ -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;

View File

@@ -1,13 +1,13 @@
#include "base.h"
//returns length of string (including \0)
// 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
// allocates new char[] and copies src there
char* cptr_copy(char* src){
uint32 len=cptr_length(src);
char* dst=malloc(len*sizeof(char));
@@ -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;
@@ -35,7 +35,7 @@ char* char_multiply(char c, uint32 n){
return rez;
}
//copies str content to new char pointer value (adding '\0' at the end)
// 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);
@@ -45,7 +45,7 @@ char* string_cpToCptr(string str){
return cptr;
}
//copies cptr content (excluding '\0' at the end) to new string
// copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr){
if(!cptr) return stringNull;
string str;
@@ -56,7 +56,7 @@ string string_cpFromCharPtr(char* cptr){
return str;
}
//compares two strings, NullPtr-friendly
// compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1){
if(str0.length!=str1.length) return false;
if(!str0.ptr) return str1.ptr ? false : true;
@@ -67,7 +67,7 @@ bool string_compare(string str0, string str1){
return true;
}
//creates new string which is reversed variant of <s>
// creates new string which is reversed variant of <s>
string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length};

View File

@@ -6,37 +6,37 @@ extern "C" {
#include "types.h"
//returns length of string (including \0)
// returns length of string (including \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);
//my fixed length string struct
//doesn't store '\0' at the end
// my fixed length string struct
// doesn't store '\0' at the end
typedef struct string{
char* ptr; //char pointer
uint32 length; //amount of chars in ptr value
char* ptr; // char pointer
uint32 length; // amount of chars in ptr value
} string;
static const string stringNull={NULL,0};
//copies str content to new char pointer value (adding '\0' at the end)
// copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCptr(string str);
//copies cptr content (excluding '\0' at the end) to new string
// copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr);
//compares two strings, NullPtr-friendly
// compares two strings, NullPtr-friendly
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);
#if __cplusplus

View File

@@ -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:

View File

@@ -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);