summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-04-27 14:20:54 +0200
committerTyge Løvset <[email protected]>2022-04-27 15:09:45 +0200
commit37e950b8a93172f5f07fedaca54c9b2894416b5a (patch)
tree47a5c9526c0d94598f1576fcd70d38ec82b9e9f4
parent8f55ea9afb719728afdc09d055fdf86c446f19ba (diff)
downloadSTC-modified-37e950b8a93172f5f07fedaca54c9b2894416b5a.tar.gz
STC-modified-37e950b8a93172f5f07fedaca54c9b2894416b5a.zip
Added cstr_expand_uninitialized().
-rw-r--r--docs/cstr_api.md2
-rw-r--r--examples/lower_bound.c4
-rw-r--r--include/stc/alt/cstr.h10
-rw-r--r--include/stc/cstr.h7
4 files changed, 18 insertions, 5 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index a7c45152..2e8cde33 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -38,6 +38,8 @@ bool cstr_empty(cstr s);
size_t cstr_reserve(cstr* self, size_t capacity);
void cstr_resize(cstr* self, size_t len, char fill);
+void cstr_shrink_to_fit(cstr* self);
+char* cstr_expand_uninitialized(cstr* self, size_t n); // return ptr to uninit data
void cstr_clear(cstr* self);
cstr* cstr_assign(cstr* self, const char* str);
diff --git a/examples/lower_bound.c b/examples/lower_bound.c
index 1d9f5843..bb8fdf66 100644
--- a/examples/lower_bound.c
+++ b/examples/lower_bound.c
@@ -6,10 +6,6 @@
#define i_val int
#include <stc/csset.h>
-
-#define i_val char
-#include <stc/cvec.h>
-
int main()
{
// TEST SORTED VECTOR
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)); }