diff options
| author | Tyge Løvset <[email protected]> | 2022-05-23 14:44:05 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-05-23 14:44:05 +0200 |
| commit | 7349441c0c95b21da87f1a13176ac4014ed98ea8 (patch) | |
| tree | 5de86b3c64f7de4f8051e83655d3519b0bcef23d /include/stc | |
| parent | 673de33eeb754142a32ac5cad551230d8fc86849 (diff) | |
| download | STC-modified-7349441c0c95b21da87f1a13176ac4014ed98ea8.tar.gz STC-modified-7349441c0c95b21da87f1a13176ac4014ed98ea8.zip | |
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.
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/alt/cstr.h | 7 | ||||
| -rw-r--r-- | include/stc/cstr.h | 18 |
2 files changed, 12 insertions, 13 deletions
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;
}
|
