From 231f2f6a034d5e2568c234479d39738947d6f514 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 16 Nov 2020 12:07:42 +0100 Subject: Small update of the new cstr methods. --- stc/cstr.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stc/cstr.h b/stc/cstr.h index 1ceb892f..9fe382cc 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -194,15 +194,18 @@ cstr_find(cstr_t s, const char* needle) { } STC_INLINE bool -cstr_contains(const cstr_t s, const char* needle) { +cstr_contains(cstr_t s, const char* needle) { return strstr(s.str, needle) != NULL; } STC_INLINE bool -cstr_begins_with(const cstr_t s, const char* needle) { - return strncmp(s.str, needle, strlen(needle)) == 0; +cstr_begins_with(cstr_t s, const char* needle) { + for (;;) { + if (!(*needle && *s.str)) return *needle == 0; + if (*needle++ != *s.str++) return false; + } } STC_INLINE bool -cstr_ends_with(const cstr_t s, const char* needle) { +cstr_ends_with(cstr_t s, const char* needle) { size_t n = strlen(needle), sz = cstr_size(s); return n <= sz ? strcmp(s.str + sz - n, needle) == 0 : false; } -- cgit v1.2.3