some code moved to String directory
This commit is contained in:
@@ -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
|
||||
|
||||
36
base/cptr.c
Normal file
36
base/cptr.c
Normal file
@@ -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;
|
||||
}
|
||||
23
base/cptr.h
Normal file
23
base/cptr.h
Normal file
@@ -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
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "std.h"
|
||||
#include "errors.h"
|
||||
#include "mystr.h"
|
||||
#include "cptr.h"
|
||||
|
||||
char* errname(err_t err){
|
||||
switch(err){
|
||||
|
||||
77
base/mystr.c
77
base/mystr.c
@@ -1,77 +0,0 @@
|
||||
#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
44
base/mystr.h
@@ -1,44 +0,0 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user