void sb_append_char(string_builder* sb, char c) { if (sb->count + 1 >= sb->_len) sb_resize(sb); sb->_str_p[sb->count] = c; sb->count++; }
void sb_append(string_builder* sb, const char* str) { if (str == NULL) return; int str_len = strlen(str); if (sb->count + str_len >= sb->_len) { sb->_len += str_len; sb_resize(sb); } memcpy(sb->_str_p + sb->count, str, str_len); sb->count += str_len; }
/** * Appends at most length of the given src string to the string buffer */ void sb_append_strn(stringbuilder* sb, const char* src, int length) { int chars_remaining; int chars_required; int new_size; // <buffer size> - <zero based index of next char to write> - <space for null terminator> chars_remaining = sb->size - sb->pos - 1; if (chars_remaining < length) { chars_required = length - chars_remaining; new_size = sb->size; do { new_size = new_size * 2; } while (new_size < (sb->size + chars_required)); sb_resize(sb, new_size); } memcpy(sb->cstr + sb->pos, src, length); sb->pos += length; }
int sb_double_size(stringbuilder* sb) { return sb_resize(sb, sb->size * 2); }