Skip to content

Commit

Permalink
add str_containsi
Browse files Browse the repository at this point in the history
  • Loading branch information
annacrombie committed Dec 17, 2024
1 parent 94548f6 commit 1d024b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/lang/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum sbuf_flags {
#define SBUF(name) SBUF_CUSTOM(name, 1024, 0)
#define SBUF_manual(name) SBUF_CUSTOM(name, 1024, sbuf_flag_overflow_alloc)

#define SBUF_WKSTR(sb) (struct str) { .s = (sb)->buf, .len = (sb)->len }

struct sbuf {
char *buf;
uint32_t len, cap;
Expand Down Expand Up @@ -88,6 +90,7 @@ bool str_startswithi(const struct str *ss, const struct str *pre);
bool str_endswith(const struct str *ss, const struct str *suf);
bool str_endswithi(const struct str *ss, const struct str *suf);
bool str_contains(const struct str *str, const struct str *substr);
bool str_containsi(const struct str *str, const struct str *substr);
obj str_join(struct workspace *wk, obj s1, obj s2);

bool str_to_i(const struct str *ss, int64_t *res, bool strip);
Expand Down
20 changes: 20 additions & 0 deletions src/lang/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ str_contains(const struct str *str, const struct str *substr)
return !!memmem(str->s, str->len, substr->s, substr->len);
}

bool
str_containsi(const struct str *str, const struct str *substr)
{
if (substr->len > str->len) {
return false;
} else if (substr->len == str->len) {
return str_eqli(str, substr);
}

uint32_t i;
for (i = 0; i < str->len - substr->len; ++i) {
struct str a = { .s = str->s + i, .len = substr->len };
if (str_eqli(&a, substr)) {
return true;
}
}

return false;
}

obj
str_join(struct workspace *wk, obj s1, obj s2)
{
Expand Down

0 comments on commit 1d024b8

Please sign in to comment.