From 37e950b8a93172f5f07fedaca54c9b2894416b5a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 27 Apr 2022 14:20:54 +0200 Subject: Added cstr_expand_uninitialized(). --- include/stc/alt/cstr.h | 10 +++++++++- include/stc/cstr.h | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index f81736dc..b366cd35 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -137,6 +137,13 @@ STC_INLINE cstr cstr_with_size(const size_t len, const char fill) { return s; } +STC_INLINE char* cstr_expand_uninitialized(cstr *self, size_t n) { + size_t len = cstr_size(*self); char* d; + if (!(d = cstr_reserve(self, len + n))) return NULL; + _cstr_p(self)->size += n; + return d + len; +} + STC_INLINE cstr* cstr_take(cstr* self, cstr s) { if (self->str != s.str && _cstr_p(self)->cap) c_free(_cstr_p(self)); @@ -189,10 +196,11 @@ const cstr cstr_null = {_cstr_nullrep.chr}; STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { - cstr_priv* p = _cstr_p(self); + cstr_priv *p = _cstr_p(self); const size_t oldcap = p->cap; if (cap > oldcap) { p = (cstr_priv*) c_realloc(((oldcap != 0) & (p != &_cstr_nullrep)) ? p : NULL, _cstr_opt_mem(cap)); + if (!p) return NULL; self->str = p->chr; if (oldcap == 0) self->str[p->size = 0] = '\0'; p->cap = _cstr_opt_cap(cap); diff --git a/include/stc/cstr.h b/include/stc/cstr.h index b48a9151..fa791815 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -174,6 +174,13 @@ STC_INLINE size_t cstr_length(cstr s) STC_INLINE size_t cstr_capacity(cstr s) { return cstr_is_long(&s) ? cstr_l_cap(&s) : cstr_s_cap; } +STC_INLINE char* cstr_expand_uninitialized(cstr *self, size_t n) { + size_t len = cstr_size(*self); char* d; + if (!(d = cstr_reserve(self, len + n))) return NULL; + _cstr_set_size(self, len + n); + return d + len; +} + STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2) { return strcmp(cstr_str(s1), cstr_str(s2)); } -- cgit v1.2.3