mirror of
https://github.com/neovim/neovim.git
synced 2025-09-11 22:08:18 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user