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. --- include/stc/alt/cstr.h | 7 +++---- include/stc/cstr.h | 18 +++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) (limited to 'include') 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