From 7349441c0c95b21da87f1a13176ac4014ed98ea8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 23 May 2022 14:44:05 +0200 Subject: Renamed cstr_find_n(self, search, pos, nmax) => cstr_find_from(self, pos, search), and cstr_replace_first(self, search, repl) => cstr_replace_first(self, pos, search, repl). // returns pos after replaced str. --- docs/cstr_api.md | 4 ++-- examples/cstr_match.c | 2 +- examples/demos.c | 2 +- include/stc/alt/cstr.h | 7 +++---- include/stc/cstr.h | 18 +++++++++--------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 7c8b4815..5570df8e 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -61,7 +61,7 @@ void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n); void cstr_replace(cstr* self, size_t pos, size_t len, const char* repl); void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s); void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* repl, size_t n); -void cstr_replace_first(cstr* self, const char* search, const char* repl); +size_t cstr_replace_first(cstr* self, size_t pos, const char* search, const char* repl); void cstr_replace_all(cstr* self, const char* search, const char* repl); void cstr_erase(cstr* self, size_t pos); @@ -70,7 +70,7 @@ void cstr_erase_n(cstr* self, size_t pos, size_t n); bool cstr_equals(cstr s, const char* str); bool cstr_equals_s(cstr s, cstr s2); size_t cstr_find(cstr s, const char* search); -size_t cstr_find_n(cstr s, const char* search, size_t pos, size_t nmax); +size_t cstr_find_from(cstr s, size_t pos, const char* search); bool cstr_contains(cstr s, const char* search); bool cstr_starts_with(cstr s, const char* str); bool cstr_ends_with(cstr s, const char* str); diff --git a/examples/cstr_match.c b/examples/cstr_match.c index 08236a0d..cd3f04be 100644 --- a/examples/cstr_match.c +++ b/examples/cstr_match.c @@ -4,7 +4,7 @@ int main() { c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { - size_t pos = cstr_find_n(ss, "brown", 0, 5); + size_t pos = cstr_find_from(ss, 0, "brown"); printf("%" PRIuMAX " [%s]\n", pos, pos == cstr_npos ? "" : cstr_str(&ss) + pos); printf("equals: %d\n", cstr_equals(ss, "The quick brown fox jumps over the lazy dog.JPG")); printf("contains: %d\n", cstr_contains(ss, "umps ove")); diff --git a/examples/demos.c b/examples/demos.c index 1304cf85..99b9e570 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -13,7 +13,7 @@ void stringdemo1() cstr_erase_n(&cs, 7, 5); // -nine printf("%s.\n", cstr_str(&cs)); - cstr_replace_first(&cs, "seven", "four"); + cstr_replace_first(&cs, 0, "seven", "four"); printf("%s.\n", cstr_str(&cs)); cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs))); diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index ef116ade..a43c7cc4 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -60,7 +60,7 @@ STC_API void cstr_replace_n(cstr* self, size_t pos, size_t len, const STC_API void cstr_replace_all(cstr* self, const char* find, const char* replace); STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n); STC_API size_t cstr_find(cstr s, const char* needle); -STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax); +STC_API size_t cstr_find_from(cstr s, size_t pos, const char* needle); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream); STC_API void cstr_replace_all(cstr* self, const char* find, const char* repl); @@ -378,10 +378,9 @@ cstr_find(cstr s, const char* needle) { } STC_DEF size_t -cstr_find_n(cstr s, const char* needle, const size_t pos, const size_t nmax) { +cstr_find_from(cstr s, const size_t pos, const char* needle) { if (pos > _cstr_p(&s)->size) return cstr_npos; - const size_t nlen = strlen(needle); - char* res = c_strnstrn(s.str + pos, needle, _cstr_p(&s)->size - pos, nmax < nlen ? nmax : nlen); + char* res = strstr(s.str + pos, needle); return res ? res - s.str : cstr_npos; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 9b66d161..69ab8a2d 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -79,7 +79,7 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); STC_API char* cstr_reserve(cstr* self, size_t cap); STC_API void cstr_shrink_to_fit(cstr* self); STC_API void cstr_resize(cstr* self, size_t size, char value); -STC_API size_t cstr_find_n(cstr s, const char* search, size_t pos, size_t nmax); +STC_API size_t cstr_find_from(cstr s, size_t pos, const char* search); STC_API char* cstr_assign_n(cstr* self, const char* str, size_t n); STC_API char* cstr_append_n(cstr* self, const char* str, size_t n); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); @@ -252,12 +252,13 @@ STC_INLINE void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* r STC_INLINE void cstr_replace(cstr* self, size_t pos, size_t len, const char* repl) { cstr_replace_n(self, pos, len, repl, strlen(repl)); } -STC_INLINE bool cstr_replace_first(cstr* self, const char* search, const char* repl) { - size_t pos = cstr_find(*self, search); +STC_INLINE size_t cstr_replace_first(cstr* self, size_t pos, const char* search, const char* repl) { + pos = cstr_find_from(*self, pos, search); if (pos == cstr_npos) - return false; - cstr_replace_n(self, pos, strlen(search), repl, strlen(repl)); - return true; + return pos; + const size_t rlen = strlen(repl); + cstr_replace_n(self, pos, strlen(search), repl, rlen); + return pos + rlen; } STC_INLINE void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s) { @@ -354,11 +355,10 @@ STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) { _cstr_set_size(self, size); } -STC_DEF size_t cstr_find_n(cstr s, const char* search, const size_t pos, const size_t nmax) { +STC_DEF size_t cstr_find_from(cstr s, const size_t pos, const char* search) { csview sv = cstr_sv(&s); - const size_t nlen = (size_t) strlen(search); if (pos > sv.size) return cstr_npos; - char* res = c_strnstrn(sv.str + pos, search, sv.size, nmax < nlen ? nmax : nlen); + char* res = strstr(sv.str + pos, search); return res ? res - sv.str : cstr_npos; } -- cgit v1.2.3