refactor(api/nvim_cmd): use kvec_t for constructing cmdline string

Co-authored-by: Björn Linse <bjorn.linse@gmail.com>
This commit is contained in:
Famiu Haque
2022-05-12 10:57:43 +02:00
parent 8fba428bc6
commit 566f8f80d6
5 changed files with 111 additions and 109 deletions

View File

@@ -1480,3 +1480,32 @@ int vim_vsnprintf_typval(char *str, size_t str_m, const char *fmt, va_list ap, t
// written to the buffer if it were large enough.
return (int)str_l;
}
int kv_do_printf(StringBuilder *str, const char *fmt, ...)
FUNC_ATTR_PRINTF(2, 3)
{
size_t remaining = str->capacity - str->size;
va_list ap;
va_start(ap, fmt);
int printed = vsnprintf(str->items ? str->items + str->size : NULL, remaining, fmt, ap);
va_end(ap);
if (printed < 0) {
return -1;
}
// printed string didn't fit, resize and try again
if ((size_t)printed >= remaining) {
kv_ensure_space(*str, (size_t)printed + 1); // include space for NUL terminator at the end
va_start(ap, fmt);
printed = vsnprintf(str->items + str->size, str->capacity - str->size, fmt, ap);
va_end(ap);
if (printed < 0) {
return -1;
}
}
str->size += (size_t)printed;
return printed;
}