From 549812812d2265709e4be00caf1d2041b4d588f5 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Sat, 3 May 2025 09:04:41 -0400 Subject: str: Add remove_at() and replace() + tests - Bring wstr_remove_at() in line with str. - Add a test for correct snprintf usage with str. Signed-off-by: Andrew Opalach --- include/al/str.h | 17 +++++++++++++++++ include/al/wstr.h | 9 +++------ 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'include/al') diff --git a/include/al/str.h b/include/al/str.h index 4ff936b..e9ac8f2 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -103,6 +103,23 @@ static inline void al_str_prepend(str *s, str *c) al_str_insert(s, 0, c); } +static inline void al_str_remove_at(str *s, u32 i) +{ + al_assert(i < s->length); + if (i != --s->length) { + al_memmove(s->data + i, s->data + i + 1, s->length - i); + } +} + +static inline void al_str_replace(str *s, char c, char n) +{ + for (u32 i = 0; i < s->length; i++) { + if (al_str_at(s, i) == c) { + al_str_at(s, i) = n; + } + } +} + static inline s32 al_str_cmp(str *s, str *c, u32 start, u32 length) { if (length > c->length) return 1; diff --git a/include/al/wstr.h b/include/al/wstr.h index efecbfe..f700037 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -147,13 +147,10 @@ static inline void al_wstr_insert(wstr *w, u32 i, wstr *c) static inline void al_wstr_remove_at(wstr *w, u32 i) { - if (i >= w->length) { - return; - } else if (i != w->length - 1) { - wchar_t *data = w->data + i; - al_memmove(data, data + 1, (w->length - i - 1) * sizeof(wchar_t)); + al_assert(i < w->length); + if (i != --w->length) { + al_memmove(w->data + i, w->data + i + 1, (w->length - i) * sizeof(wchar_t)); } - w->length--; w->data[w->length] = L'\0'; } -- cgit v1.2.3-101-g0448