str_seek
This commit is contained in:
parent
2831474f79
commit
9e9b43afb4
68
src/cstr.c
68
src/cstr.c
@ -50,71 +50,3 @@ char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
|||||||
}
|
}
|
||||||
return buf;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
20
src/std.h
20
src/std.h
@ -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 isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
|
||||||
static inline bool isAlphabeticalUpper(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'; }
|
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);
|
|
||||||
|
|||||||
@ -3,27 +3,97 @@
|
|||||||
str str_copy(str src){
|
str str_copy(str src){
|
||||||
if(src.data == NULL || src.len == 0)
|
if(src.data == NULL || src.len == 0)
|
||||||
return src;
|
return src;
|
||||||
|
|
||||||
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
|
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
|
||||||
memcpy(nstr.data, src.data, src.len);
|
memcpy(nstr.data, src.data, src.len);
|
||||||
nstr.data[nstr.len] = '\0';
|
nstr.data[nstr.len] = '\0';
|
||||||
return nstr;
|
return nstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool str_compare(str str0, str str1){
|
bool str_equals(str s0, str s1){
|
||||||
if(str0.len!= str1.len) return false;
|
if(s0.len != s1.len)
|
||||||
if(!str0.data) return str1.data ? false : true;
|
return false;
|
||||||
else if(!str1.data) return false;
|
|
||||||
while(str0.len-- > 0)
|
for(u32 i = 0; i < s0.len; i++)
|
||||||
if(*str0.data++ != *str1.data++ )
|
if(s0.data[i] != s1.data[i])
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
str str_reverse(str s){
|
str str_reverse(str s){
|
||||||
if(s.data == NULL || s.len == 0)
|
if(s.data == NULL || s.len == 0)
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
|
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
|
||||||
for(u32 i = 0; i < s.len; i++ )
|
for(u32 i = 0; i < s.len; i++ )
|
||||||
r.data[i] = s.data[s.len - i - 1];
|
r.data[i] = s.data[s.len - i - 1];
|
||||||
return r;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -8,23 +8,27 @@ typedef struct str {
|
|||||||
bool isZeroTerminated;
|
bool isZeroTerminated;
|
||||||
} str;
|
} 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 })
|
#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);
|
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
|
/// copies src content to new string and adds \0 at the end
|
||||||
str str_copy(str src);
|
str str_copy(str src);
|
||||||
|
|
||||||
/// compares two strings, NullPtr-friendly
|
/// 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>
|
/// allocates new string which is reversed variant of <s>
|
||||||
str str_reverse(str s);
|
str str_reverse(str s);
|
||||||
|
|
||||||
#if __cplusplus
|
i32 str_seek(str src, str fragment, u32 startIndex);
|
||||||
}
|
i32 str_seekReverse(str src, str fragment, u32 startIndex);
|
||||||
#endif
|
|
||||||
|
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);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user