Only realloc once when appending string to stringbuilder

This commit is contained in:
K. Lange 2021-02-14 11:26:22 +09:00
parent 22453f42e6
commit a961def84a

View File

@ -111,9 +111,11 @@ static inline void pushStringBuilder(struct StringBuilder * sb, char c) {
} }
static inline void pushStringBuilderStr(struct StringBuilder * sb, char *str, size_t len) { static inline void pushStringBuilderStr(struct StringBuilder * sb, char *str, size_t len) {
while (sb->capacity < sb->length + len) { if (sb->capacity < sb->length + len) {
size_t old = sb->capacity; while (sb->capacity < sb->length + len) {
sb->capacity = GROW_CAPACITY(old); size_t old = sb->capacity;
sb->capacity = GROW_CAPACITY(old);
}
sb->bytes = realloc(sb->bytes, sb->capacity); sb->bytes = realloc(sb->bytes, sb->capacity);
} }
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {