Add dstr_find function
This commit is contained in:
parent
5f926f060e
commit
7a326421b8
@ -14,6 +14,7 @@ void dstr_append(dstr_t **dst, char c);
|
|||||||
void dstr_resize(dstr_t **str);
|
void dstr_resize(dstr_t **str);
|
||||||
void dstr_clear(dstr_t *str);
|
void dstr_clear(dstr_t *str);
|
||||||
void dstr_print(const dstr_t *str);
|
void dstr_print(const dstr_t *str);
|
||||||
|
i64 dstr_find(const dstr_t *str, const char *substr);
|
||||||
u64 dstr_length(const dstr_t *str);
|
u64 dstr_length(const dstr_t *str);
|
||||||
u64 dstr_capacity(const dstr_t *str);
|
u64 dstr_capacity(const dstr_t *str);
|
||||||
const char *dstr_to_cstr(const dstr_t *str);
|
const char *dstr_to_cstr(const dstr_t *str);
|
||||||
|
@ -155,6 +155,37 @@ void dstr_print(const dstr_t *str) {
|
|||||||
printf("%s\n", str->buf);
|
printf("%s\n", str->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i64 dstr_find(const dstr_t *str, const char *substr) {
|
||||||
|
if (!str || !substr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 substr_length = strlen(substr);
|
||||||
|
|
||||||
|
if (substr_length == 0 || substr_length > str->size) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[substr_length + 1];
|
||||||
|
memset(buf, 0, substr_length + 1);
|
||||||
|
|
||||||
|
for (i64 i = 0; i < str->size; ++i) {
|
||||||
|
if (i + substr_length >= str->size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u64 j = 0; j < substr_length; ++j) {
|
||||||
|
buf[j] = str->buf[i + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(buf, substr) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
u64 dstr_length(const dstr_t *str) {
|
u64 dstr_length(const dstr_t *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user