string_copy adds trailing zero byte

This commit is contained in:
Timerix22 2023-02-15 21:44:38 +06:00
parent dbf2c2772c
commit 5bbab0a414
2 changed files with 9 additions and 4 deletions

View File

@ -14,14 +14,16 @@ char* string_extract(string str){
return cptr;
}
// copies src.ptr content to new string
// copies src.ptr content to new string and adds \0 at the end
string string_copy(string src){
if(!src.ptr) return src;
if(!src.ptr)
return src;
string nstr;
nstr.length=src.length;
nstr.ptr=malloc(nstr.length);
nstr.ptr=malloc(nstr.length+1);
for(u32 i=0;i<nstr.length;i++)
nstr.ptr[i]=src.ptr[i];
nstr.ptr[nstr.length]='\0';
return nstr;
}

View File

@ -20,10 +20,13 @@ Autoarr_declare(string)
static const string stringNull={NULL,0};
/// wraps pointer without copy
#define string_fromCptr(CPTR) (string){ .ptr=CPTR, .length=cptr_length(CPTR) }
// copies str content to new char pointer value (adding '\0' at the end)
char* string_extract(string str);
// copies src.ptr content to new string
// copies src.ptr content to new string and adds \0 at the end
string string_copy(string src);
// compares two strings, NullPtr-friendly