From 1f3372b7f1fbf73a2b701fafbeb0af086d0f508f Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 24 Oct 2017 20:48:29 +0200 Subject: [PATCH] ImFormatString, ImFormatStringV(): clarifying specs so that passing a NULL buffer should return the desired length. (#1038) --- imgui.cpp | 13 ++++++++----- imgui_demo.cpp | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index fd1c44de8..6d2ff61c9 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -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, ...) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 35ee618b9..5fe78dbe5 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -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);