This commit is contained in:
Timerix 2025-02-02 17:59:30 +05:00
parent 2831474f79
commit 9e9b43afb4
4 changed files with 88 additions and 102 deletions

View File

@ -50,71 +50,3 @@ char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
}
return buf;
}
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
char sc = *src, fc = *fragment;
if(sc == 0 || fc == 0)
return -1;
u32 fr_start = startIndex;
for(u32 si = startIndex; si-startIndex < seekLength && sc != 0; si++){
sc = src[si];
fc = fragment[si-fr_start];
if(fc == 0)
return fr_start;
if(sc != fc)
fr_start++;
}
return -1;
}
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
char sc = *src, fc = *fragment;
if(sc == 0 || fc == 0)
return -1;
i32 len = strlen(src);
if(startIndex == (u32)-1)
startIndex = len-1;
u32 fr_len = strlen(fragment);
for(u32 si = startIndex; si < (u32)-1 && si != (len - seekLength - 1); si--){
if(si + 1 < fr_len)
return -1;
sc = src[si];
fc = fragment[0];
u32 fr_start = si;
for(u32 fi = 0; fc == sc ; fi++){
if(fi == fr_len)
return fr_start;
fc = fragment[fi];
sc = src[si--];
}
}
return -1;
}
i32 cstr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength){
char sc=*src;
if(sc==0 || fragment==0)
return -1;
for(u32 si=startIndex; si-startIndex<seekLength && sc!=0; si++){
sc=src[si];
if(sc==fragment)
return si;
}
return -1;
}
i32 cstr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength){
char sc=*src;
if(sc==0 || fragment==0)
return -1;
i32 len=strlen(src);
if(startIndex==(u32)-1)
startIndex=len-1;
for(u32 si=startIndex; si<(u32)-1 && si!=len-1-seekLength; si--){
sc=src[si];
if(sc==fragment)
return si;
}
return -1;
}

View File

@ -58,23 +58,3 @@ char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
/// @param startIndex 0 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
/// @param startIndex -1 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
/// @param startIndex 0 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength);
/// @param startIndex -1 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength);

View File

@ -3,27 +3,97 @@
str str_copy(str src){
if(src.data == NULL || src.len == 0)
return src;
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
memcpy(nstr.data, src.data, src.len);
nstr.data[nstr.len] = '\0';
return nstr;
}
bool str_compare(str str0, str str1){
if(str0.len!= str1.len) return false;
if(!str0.data) return str1.data ? false : true;
else if(!str1.data) return false;
while(str0.len-- > 0)
if(*str0.data++ != *str1.data++ )
bool str_equals(str s0, str s1){
if(s0.len != s1.len)
return false;
for(u32 i = 0; i < s0.len; i++)
if(s0.data[i] != s1.data[i])
return false;
return true;
}
str str_reverse(str s){
if(s.data == NULL || s.len == 0)
return s;
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
for(u32 i = 0; i < s.len; i++ )
r.data[i] = s.data[s.len - i - 1];
return r;
}
i32 str_seek(str src, str fragment, u32 startIndex){
if(src.len == 0 || fragment.len == 0)
return -1;
for(u32 i = startIndex; i < src.len - fragment.len + 1; i++){
for(u32 j = 0;; j++){
if(j == fragment.len)
return i;
if(src.data[i + j] != fragment.data[j])
break;
}
}
return -1;
}
i32 str_seekReverse(str src, str fragment, u32 startIndex){
if(src.len == 0 || fragment.len == 0)
return -1;
if(startIndex > src.len - 1)
startIndex = src.len - 1;
for(u32 i = startIndex; i >= fragment.len - 1; i--){
for(u32 j = 0;; j++){
if(j == fragment.len)
return i - j + 1;
if(src.data[i - j] != fragment.data[fragment.len - 1 - j])
break;
}
}
return -1;
}
i32 str_seekChar(str src, char c, u32 startIndex){
for(u32 i = startIndex; i < src.len; i++){
if(src.data[i] == c)
return i;
}
return -1;
}
i32 str_seekCharReverse(str src, char c, u32 startIndex){
if(startIndex > src.len - 1)
startIndex = src.len - 1;
for(u32 i = startIndex; i != (u32)-1; i--){
if(src.data[i] == c)
return i;
}
return -1;
}
bool str_startsWith(str src, str fragment){
if(src.len < fragment.len)
return false;
src.len = fragment.len;
return str_equals(src, fragment);
}
bool str_endsWith(str src, str fragment){
if(src.len < fragment.len)
return false;
src.data = (char*)(src.data + src.len - fragment.len);
src.len = fragment.len;
return str_equals(src, fragment);
}

View File

@ -8,23 +8,27 @@ typedef struct str {
bool isZeroTerminated;
} str;
// creates str from a string literal
#define STR(LITERAL) str_construct(LITERAL, ARRAY_SIZE(LITERAL) - 1, true)
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
static const str str_null = str_construct(NULL, 0, 0);
/// copies str content to new char pointer value (adding '\0' at the end)
char* str_extractcstr(str str);
/// copies src content to new string and adds \0 at the end
str str_copy(str src);
/// compares two strings, NullPtr-friendly
bool str_compare(str str0, str str1);
bool str_equals(str str0, str str1);
/// allocates new string which is reversed variant of <s>
str str_reverse(str s);
#if __cplusplus
}
#endif
i32 str_seek(str src, str fragment, u32 startIndex);
i32 str_seekReverse(str src, str fragment, u32 startIndex);
i32 str_seekChar(str src, char c, u32 startIndex);
i32 str_seekCharReverse(str src, char c, u32 startIndex);
bool str_startsWith(str src, str fragment);
bool str_endsWith(str src, str fragment);