commit 3f4de3da0a6326a32960ef06d5fa808f64104e65
parent b321334d7c901c4f057f729797ab4b32541c0518
Author: Jakub Kądziołka <kuba@kadziolka.net>
Date: Sat, 17 Nov 2018 00:45:44 +0100
Remove empty string handling edge-cases
An empty string will rarely be useful, since the only thing that
can be done to it is appending a character with the current state
of the string API. Storing empty strings with a NULL storage pointer
creates unnecessary edge cases in any code handling strings.
The tables test no longer segfaults.
Diffstat:
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/src/string.c b/src/string.c
@@ -3,15 +3,6 @@
#include "string.h"
#include "unicode.h"
-static void sanity_check(str_t *str) {
- if (str->str == NULL) {
- str->str = malloc(16);
- str->size = 16;
- str->len = 0;
- str->str[0] = '\0';
- }
-}
-
static int ensure_capacity(str_t *str, size_t len) {
if (len + 1 >= str->size) {
char *new = realloc(str->str, str->size * 2);
@@ -25,7 +16,12 @@ static int ensure_capacity(str_t *str, size_t len) {
}
str_t *str_create() {
- return calloc(sizeof(str_t), 1);
+ str_t *str = calloc(sizeof(str_t), 1);
+ str->str = malloc(16);
+ str->size = 16;
+ str->len = 0;
+ str->str[0] = '\0';
+ return str;
}
void str_free(str_t *str) {
@@ -39,7 +35,6 @@ int str_append_ch(str_t *str, uint32_t ch) {
if (size <= 0) {
return -1;
}
- sanity_check(str);
if (!ensure_capacity(str, str->len + size)) {
return -1;
}