aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-10 15:25:53 +0100
committerHugo Hörnquist <hugo@hornquist.se>2019-02-10 15:25:53 +0100
commit1ac2cebab221b2ccc8f167f9ceaa9098fa77efe0 (patch)
treecd9225a2aca79517ee8707f9a6a8d3d5ffb4d49d
parentAdd push_strbuf function. (diff)
downloadcalp-1ac2cebab221b2ccc8f167f9ceaa9098fa77efe0.tar.gz
calp-1ac2cebab221b2ccc8f167f9ceaa9098fa77efe0.tar.xz
Improve strbuf.
-rw-r--r--strbuf.c98
-rw-r--r--strbuf.h10
2 files changed, 45 insertions, 63 deletions
diff --git a/strbuf.c b/strbuf.c
index dcfcc826..0d7300d3 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -8,13 +8,13 @@
#endif
INIT_F(strbuf) {
- this->mem = NULL;
- this->alloc = 0;
- this->len = 0;
- this->ptr = 0;
+ INIT(strbuf, this, 1);
return 0;
}
+/*
+ * Giving len < 1 is an error.
+ */
INIT_F(strbuf, size_t len) {
this->mem = calloc(sizeof(*this->mem), len);
this->alloc = len;
@@ -24,15 +24,6 @@ INIT_F(strbuf, size_t len) {
}
int strbuf_realloc(strbuf* str, size_t len) {
-#ifdef SAFE_STR
- if (str->mem == NULL) {
- /* NOTE
- * this isn't an error, since
- * realloc(NULL, 10) ≡ malloc(10)
- */
- ERR("String memory not initialized");
- }
-#endif
str->mem = realloc(str->mem, len);
str->alloc = len;
return 0;
@@ -40,6 +31,7 @@ int strbuf_realloc(strbuf* str, size_t len) {
FREE_F(strbuf) {
#ifdef SAFE_STR
+ /* has already been freed */
if (this->mem == NULL) return 1;
#endif
free (this->mem);
@@ -49,37 +41,46 @@ FREE_F(strbuf) {
return 0;
}
+/*
+ * Reallocates memmory for you. Returns 1 if memory was reallocated.
+ */
int strbuf_append(strbuf* s, char c) {
+ int retval = 0;
#ifdef SAFE_STR
- if (s->len > s->alloc) {
- // printf("s->len = %i, s->alloc = %i\n", s->len, s->alloc);
- // ERR("Not enough memmory allocated");
- return 1;
+ if (s->len + 1 > s->alloc) {
+ s->alloc <<= 1;
+ s->mem = realloc(s->mem, s->alloc);
+ retval = 1;
}
#endif
s->mem[s->len] = c;
s->ptr = ++s->len;
- return 0;
+ return retval;
}
int strbuf_cap(strbuf* s) {
+ /*
+ * TODO check memmory usage
+ */
return strbuf_append(s, 0);
}
int strbuf_copy(strbuf* dest, strbuf* src) {
+ int retval = 0;
#ifdef SAFE_STR
- if (dest->alloc + 1 < src->len) {
- ERR("Not enough memmory allocated");
- return 1;
+ if (dest->alloc < src->len) {
+ /*
+ * one extra octet allocated to have space for a finishing
+ * '\0'.
+ * */
+ strbuf_realloc(dest, src->len + 1);
+ retval = 1;
}
#endif
dest->len = src->len;
memcpy(dest->mem, src->mem, src->len);
-
- // TODO should this be here?
- strbuf_cap(dest);
- return 0;
+ return retval;
}
int strbuf_cmp(strbuf* a, strbuf* b) {
@@ -89,10 +90,16 @@ int strbuf_cmp(strbuf* a, strbuf* b) {
return -1;
}
#endif
- return strcmp(a->mem, b->mem);
+ return strncmp(a->mem, b->mem, a->len);
}
int strbuf_c(strbuf* a, char* b) {
+#ifdef SAFE_STR
+ if (a->alloc == 0) {
+ ERR("a not allocated");
+ return -1;
+ }
+#endif
return strcmp(a->mem, b) == 0;
}
@@ -110,45 +117,20 @@ char* strbuf_cur(strbuf* s) {
return &s->mem[s->ptr];
}
-int strbuf_reset(strbuf* s)
-{
- s->ptr = 0;
- return 0;
+/* TODO merge this and strbuf_copy */
+int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) {
+ return strbuf_copy(dest, src);
}
-int strbuf_realloc_copy(strbuf* dest, strbuf* src) {
- strbuf_realloc(dest, src->len);
- strbuf_copy(dest, src);
- return 0;
+char* strbuf_end(strbuf* s) {
+ return &s->mem[s->len];
}
-int strbuf_init_copy(strbuf* dest, strbuf* src) {
-#ifdef SAFE_STR
- if (dest->alloc != 0) {
- printf("%u ", dest->alloc);
- ERR("Dest already allocated");
- return 1;
- }
-
- if (src == NULL) {
- ERR("Attempting to copy NULL strbuf");
- return 2;
- }
-#endif
-
- INIT(strbuf, dest, src->len + 1);
- strbuf_copy(dest, src);
-
+int strbuf_reset(strbuf* s) {
+ s->ptr = 0;
return 0;
}
-int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) {
- return strbuf_init_copy(dest, src);
-}
-
-char* strbuf_end(strbuf* s) {
- return &s->mem[s->len];
-}
int strbuf_soft_reset(strbuf* s) {
s->ptr = s->len = 0;
diff --git a/strbuf.h b/strbuf.h
index e6f59896..62c0ee08 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -66,11 +66,6 @@ int strbuf_append(strbuf* s, char c);
int strbuf_cap(strbuf* s);
/*
- * Resets the seek for strbuf to 0.
- */
-int strbuf_reset(strbuf* s);
-
-/*
* Returns a pointer to character at index. Allows mutation of the
* value pointed to by the return address.
*/
@@ -82,6 +77,11 @@ char* charat(strbuf* s, unsigned int idx);
char* strbuf_cur(strbuf* s);
/*
+ * Resets the seek for strbuf to 0.
+ */
+int strbuf_reset(strbuf* s);
+
+/*
* Sets the length and seek ptr to 0, but doesn't touch the memmory.
*/
int strbuf_soft_reset(strbuf* s);