Skip to content

Commit c1c0db1

Browse files
committed
utilize provided function str_reserve
1 parent f931acd commit c1c0db1

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/str.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
/* inclusion and configuration of vector */
66
#include "str.h"
77

8+
#define VEC_SETTINGS_DEFAULT_SIZE STR_DEFAULT_SIZE
89
#define VEC_SETTINGS_KEEP_ZERO_END 1
910
#define VEC_SETTINGS_STRUCT_ITEMS s
1011

1112
VEC_IMPLEMENT(Str, str, char, BY_VAL, 0);
1213

1314
/* other functions */
1415

15-
#define STR_DEFAULT_BLOCKSIZE 32
16-
1716
#if 1
1817
int str_fmt(Str *str, char *format, ...)
1918
{
@@ -28,18 +27,9 @@ int str_fmt(Str *str, char *format, ...)
2827
}
2928
va_end(argp);
3029
// calculate required memory
31-
size_t len_new = str->last + len_app + 1;
32-
size_t required = str->cap ? str->cap : STR_DEFAULT_BLOCKSIZE;
33-
while(required < len_new) required = required << 1;
34-
// make sure to have enough memory
35-
if(required > str->cap)
36-
{
37-
char *temp = realloc(str->s, required);
38-
// safety check
39-
// apply address and set new allocd
40-
if(!temp) return -1;
41-
str->s = temp;
42-
str->cap = required;
30+
size_t len_new = str->last + len_app;
31+
if(str_reserve(str, len_new)) {
32+
return -1;
4333
}
4434
// actual append
4535
va_start(argp, format);

0 commit comments

Comments
 (0)