Hi… I am well aware that this diff view is very suboptimal. It will be fixed when the refactored server comes along!
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.
#include <stdlib.h> #include <stdint.h> #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);
if (!new) {
return 0;
}
str->str = new;
str->size *= 2;
}
return 1;
}
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) {
if (!str) return;
free(str->str);
free(str);
}
int str_append_ch(str_t *str, uint32_t ch) {
int size = utf8_chsize(ch);
if (size <= 0) {
return -1;
}
sanity_check(str);
if (!ensure_capacity(str, str->len + size)) {
return -1;
}
utf8_encode(&str->str[str->len], ch);
str->len += size;
str->str[str->len] = '\0';
return size;
}