From 9d20b5f012edd89fa48691ffb84fde71294f203c Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Thu, 18 Aug 2022 09:47:35 +0200 Subject: Added cstr_u8_erase(). Renamed cstr_erase_n() => cstr_erase(). --- include/stc/alt/cstr.h | 6 ++---- include/stc/cstr.h | 12 ++++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index a2233432..e38cff0f 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -55,7 +55,7 @@ STC_API int cstr_printf(cstr* self, const char* fmt, ...); STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n); STC_API cstr cstr_replace_sv(csview str, csview find, csview repl, unsigned count); STC_DEF void cstr_replace_at_sv(cstr* self, const size_t pos, size_t len, csview repl); -STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n); +STC_API void cstr_erase(cstr* self, size_t pos, size_t n); STC_API size_t cstr_find(const cstr* self, const char* needle); STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* needle); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream); @@ -101,8 +101,6 @@ STC_INLINE void cstr_replace_at(cstr* self, const size_t pos, const size { cstr_replace_at_sv(self, pos, len, c_sv(str, strlen(str))); } STC_INLINE void cstr_replace_s(cstr* self, const size_t pos, const size_t len, cstr s) { cstr_replace_at_sv(self, pos, len, c_sv(s.str, _cstr_p(&s)->size)); } -STC_INLINE void cstr_erase(cstr* self, const size_t pos) - { cstr_erase_n(self, pos, 1); } STC_INLINE char* cstr_front(cstr* self) { return self->str; } STC_INLINE char* cstr_back(cstr* self) { return self->str + _cstr_p(self)->size - 1; } @@ -336,7 +334,7 @@ cstr_replace_sv(csview str, csview find, csview repl, unsigned count) { } STC_DEF void -cstr_erase_n(cstr* self, const size_t pos, size_t n) { +cstr_erase(cstr* self, const size_t pos, size_t n) { const size_t len = _cstr_p(self)->size; if (n > len - pos) n = len - pos; if (len) { diff --git a/include/stc/cstr.h b/include/stc/cstr.h index b731289b..fe253c71 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -84,7 +84,8 @@ STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* search); STC_API char* cstr_assign_n(cstr* self, const char* str, size_t len); STC_API char* cstr_append_n(cstr* self, const char* str, size_t len); STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); -STC_API void cstr_erase_n(cstr* self, size_t pos, size_t len); +STC_API void cstr_erase(cstr* self, size_t pos, size_t len); +STC_API void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); STC_API cstr cstr_from_fmt(const char* fmt, ...); STC_API int cstr_printf(cstr* self, const char* fmt, ...); STC_API void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count); @@ -540,13 +541,20 @@ cstr_replace(cstr* self, const char* search, const char* repl, unsigned count) { c_sv(repl, strlen(repl)), count)); } -STC_DEF void cstr_erase_n(cstr* self, const size_t pos, size_t len) { +STC_DEF void cstr_erase(cstr* self, const size_t pos, size_t len) { cstr_buf r = cstr_buffer(self); if (len > r.size - pos) len = r.size - pos; memmove(&r.data[pos], &r.data[pos + len], r.size - (pos + len)); _cstr_set_size(self, r.size - len); } +STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len) { + cstr_buf r = cstr_buffer(self); + size_t len = utf8_pos(r.data + bytepos, u8len); + memmove(&r.data[bytepos], &r.data[bytepos + len], r.size - (bytepos + len)); + _cstr_set_size(self, r.size - len); +} + #if defined(__clang__) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wdeprecated-declarations" -- cgit v1.2.3