Browse Source

Internals: move "%s" skip-formatting logic to ImFormatStringToTempBuffer() function, meaning Text() and all the *V() functions can also benefit from it. (#3466)

Amend 645a6e0 and 23a785a.
pull/6129/head
ocornut 2 years ago
parent
commit
27f2dd56d6
  1. 1
      docs/CHANGELOG.txt
  2. 30
      imgui.cpp
  3. 18
      imgui_widgets.cpp

1
docs/CHANGELOG.txt

@ -54,6 +54,7 @@ All changes:
- PlotHistogram, PlotLines: Passing negative sizes honor alignment like other widgets.
- ImDrawList: Added missing early-out in AddPolyline() and AddConvexPolyFilled() when
color alpha is zero.
- Misc: Most text functions treat "%s" as a shortcut to no-formatting. (#3466)
- Misc: Tolerate zero delta-time under Emscripten as backends are imprecise in their
values for io.DeltaTime, and browser features such as "privacy.resistFingerprinting=true"
can exacerbate that. (#6114, #3644)

30
imgui.cpp

@ -1805,18 +1805,36 @@ void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end,
ImGuiContext& g = *GImGui;
va_list args;
va_start(args, fmt);
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
{
const char* buf = va_arg(args, const char*); // Skip formatting when using "%s"
*out_buf = buf;
if (out_buf_end) { *out_buf_end = buf + strlen(buf); }
}
else
{
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
}
va_end(args);
}
void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args)
{
ImGuiContext& g = *GImGui;
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
{
const char* buf = va_arg(args, const char*); // Skip formatting when using "%s"
*out_buf = buf;
if (out_buf_end) { *out_buf_end = buf + strlen(buf); }
}
else
{
int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args);
*out_buf = g.TempBuffer.Data;
if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; }
}
}
// CRC32 needs a 1KB lookup table (not cache friendly)

18
imgui_widgets.cpp

@ -274,7 +274,6 @@ void ImGui::TextV(const char* fmt, va_list args)
if (window->SkipItems)
return;
// FIXME-OPT: Handle the %s shortcut?
const char* text, *text_end;
ImFormatStringToTempBufferV(&text, &text_end, fmt, args);
TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText);
@ -291,10 +290,7 @@ void ImGui::TextColored(const ImVec4& col, const char* fmt, ...)
void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args)
{
PushStyleColor(ImGuiCol_Text, col);
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting
else
TextV(fmt, args);
TextV(fmt, args);
PopStyleColor();
}
@ -310,10 +306,7 @@ void ImGui::TextDisabledV(const char* fmt, va_list args)
{
ImGuiContext& g = *GImGui;
PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting
else
TextV(fmt, args);
TextV(fmt, args);
PopStyleColor();
}
@ -328,13 +321,10 @@ void ImGui::TextWrapped(const char* fmt, ...)
void ImGui::TextWrappedV(const char* fmt, va_list args)
{
ImGuiContext& g = *GImGui;
bool need_backup = (g.CurrentWindow->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set
const bool need_backup = (g.CurrentWindow->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set
if (need_backup)
PushTextWrapPos(0.0f);
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting
else
TextV(fmt, args);
TextV(fmt, args);
if (need_backup)
PopTextWrapPos();
}

Loading…
Cancel
Save