ImFormatString, ImFormatStringV(): clarifying specs so that passing a NULL buffer should return the desired length. (#1038)

This commit is contained in:
omar 2017-10-24 20:48:29 +02:00
parent 8fd5620277
commit 1f3372b7f1
2 changed files with 9 additions and 5 deletions

View File

@ -1010,16 +1010,18 @@ static const char* ImAtoi(const char* src, int* output)
return src;
}
// MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size).
// A) MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size).
// Ideally we would test for only one of those limits at runtime depending on the behavior the vsnprintf(), but trying to deduct it at compile time sounds like a pandora can of worm.
// B) When buf==NULL vsnprintf() will return the output size.
#ifndef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS
int ImFormatString(char* buf, int buf_size, const char* fmt, ...)
{
IM_ASSERT(buf_size > 0);
va_list args;
va_start(args, fmt);
int w = vsnprintf(buf, buf_size, fmt, args);
va_end(args);
if (buf == NULL)
return w;
if (w == -1 || w >= buf_size)
w = buf_size - 1;
buf[w] = 0;
@ -1028,8 +1030,9 @@ int ImFormatString(char* buf, int buf_size, const char* fmt, ...)
int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args)
{
IM_ASSERT(buf_size > 0);
int w = vsnprintf(buf, buf_size, fmt, args);
if (buf == NULL)
return w;
if (w == -1 || w >= buf_size)
w = buf_size - 1;
buf[w] = 0;
@ -1671,7 +1674,7 @@ void ImGuiTextBuffer::appendv(const char* fmt, va_list args)
va_list args_copy;
va_copy(args_copy, args);
int len = vsnprintf(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass.
int len = ImFormatStringV(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass.
if (len <= 0)
return;
@ -1684,7 +1687,7 @@ void ImGuiTextBuffer::appendv(const char* fmt, va_list args)
}
Buf.resize(needed_sz);
ImFormatStringV(&Buf[write_off] - 1, len+1, fmt, args_copy);
ImFormatStringV(&Buf[write_off - 1], len + 1, fmt, args_copy);
}
void ImGuiTextBuffer::append(const char* fmt, ...)

View File

@ -2359,6 +2359,7 @@ struct ExampleAppConsole
void AddLog(const char* fmt, ...) IM_FMTARGS(2)
{
// FIXME-OPT
char buf[1024];
va_list args;
va_start(args, fmt);