summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-08-18 09:47:35 +0200
committerTyge Lovset <[email protected]>2022-08-18 09:47:35 +0200
commit9d20b5f012edd89fa48691ffb84fde71294f203c (patch)
tree5089e6f5550a6640341ecc06cf4df203cce4d428
parente774adc245c74341ec5c4318de0dfd24a42b21e5 (diff)
downloadSTC-modified-9d20b5f012edd89fa48691ffb84fde71294f203c.tar.gz
STC-modified-9d20b5f012edd89fa48691ffb84fde71294f203c.zip
Added cstr_u8_erase(). Renamed cstr_erase_n() => cstr_erase().
-rw-r--r--docs/cstr_api.md6
-rw-r--r--examples/demos.c2
-rw-r--r--include/stc/alt/cstr.h6
-rw-r--r--include/stc/cstr.h12
4 files changed, 16 insertions, 10 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index 8c28a601..273f4de4 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -63,8 +63,7 @@ void cstr_insert(cstr* self, size_t pos, const char* ins);
void cstr_insert_sv(cstr* self, size_t pos, csview ins);
void cstr_insert_s(cstr* self, size_t pos, cstr ins);
-void cstr_erase(cstr* self, size_t pos);
-void cstr_erase_n(cstr* self, size_t pos, size_t n); // erase n bytes from pos
+void cstr_erase(cstr* self, size_t pos, size_t len); // erase len bytes from pos
void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count); // count==0: replace all.
cstr cstr_replace_sv(csview in, csview search, csview repl, unsigned count);
@@ -100,6 +99,7 @@ size_t cstr_u8_to_pos(const cstr* self, size_t u8idx); // byte po
const char* cstr_u8_at(const cstr* self, size_t u8idx); // char* position at utf8 codepoint index
csview cstr_u8_chr(const cstr* self, size_t u8idx); // get utf8 character as a csview
void cstr_u8_replace(cstr* self, size_t bytepos, size_t u8len, csview repl); // replace u8len utf8 chars
+void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); // erase u8len codepoints from pos
// iterate utf8 codepoints
cstr_iter cstr_begin(const cstr* self);
@@ -165,7 +165,7 @@ int main() {
cstr_insert(&s1, 3, "-two");
printf("%s\n", cstr_str(&s1));
- cstr_erase_n(&s1, 7, 5); // -nine
+ cstr_erase(&s1, 7, 5); // -nine
printf("%s\n", cstr_str(&s1));
cstr_replace(&s1, "seven", "four", 1);
diff --git a/examples/demos.c b/examples/demos.c
index 331ef04f..d7097961 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -10,7 +10,7 @@ void stringdemo1()
cstr_insert(&cs, 3, "-two");
printf("%s.\n", cstr_str(&cs));
- cstr_erase_n(&cs, 7, 5); // -nine
+ cstr_erase(&cs, 7, 5); // -nine
printf("%s.\n", cstr_str(&cs));
cstr_replace(&cs, "seven", "four", 1);
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"