go back to string

This commit is contained in:
2022-04-16 23:51:05 +03:00
parent d611287c1a
commit e1addc4004
10 changed files with 242 additions and 73 deletions

View File

@@ -7,7 +7,7 @@ extern "C" {
#include "std.h"
#include "types.h"
#include "errors.h"
#include "cptr.h"
#include "mystr.h"
// executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock

View File

@@ -1,6 +1,6 @@
#include "std.h"
#include "errors.h"
#include "cptr.h"
#include "mystr.h"
char* errname(err_t err){
switch(err){

77
base/mystr.c Normal file
View File

@@ -0,0 +1,77 @@
#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;
}
//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);
cptr[str.length]=0;
while(str.length-->0)
cptr[str.length]=str.ptr[str.length];
return cptr;
}
//copies cptr content (excluding '\0' at the end) to new string
string string_cpFromCharPtr(char* cptr){
if(!cptr) return stringNull;
string str;
str.length=cptr_length(cptr)-1;
str.ptr=malloc(str.length);
for(uint32 i=0;i<str.length;i++)
str.ptr[i]=cptr[i];
return str;
}
//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;
else if(!str1.ptr) return false;
while(str0.length-->0)
if(*str0.ptr++ != *str1.ptr++)
return false;
return true;
}
//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};
for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1];
return r;
}

44
base/mystr.h Normal file
View File

@@ -0,0 +1,44 @@
#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);
//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
} string;
static const string stringNull={NULL,0};
//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
string string_cpFromCharPtr(char* cptr);
//compares two strings, NullPtr-friendly
bool string_compare(string str0, string str1);
//creates new string which is reversed variant of <s>
string string_reverse(string s);
#if __cplusplus
}
#endif