diff options
| author | 2025-05-03 09:04:41 -0400 | |
|---|---|---|
| committer | 2025-05-03 09:20:38 -0400 | |
| commit | 549812812d2265709e4be00caf1d2041b4d588f5 (patch) | |
| tree | 72ed245b78301869e759b5dd43c950267fdbb839 /include | |
| parent | a5516233d66eb1552cf7fdfe03779b2bf896c82d (diff) | |
| download | libalabaster-549812812d2265709e4be00caf1d2041b4d588f5.tar.gz libalabaster-549812812d2265709e4be00caf1d2041b4d588f5.tar.bz2 libalabaster-549812812d2265709e4be00caf1d2041b4d588f5.zip | |
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 <andrew@akon.city>
Diffstat (limited to 'include')
| -rw-r--r-- | include/al/str.h | 17 | ||||
| -rw-r--r-- | include/al/wstr.h | 9 |
2 files changed, 20 insertions, 6 deletions
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'; } |