ImStrv: standardized code doing format copy, optimized ImStrStr

This commit is contained in:
ocornut 2021-05-25 14:08:20 +02:00
parent 11083874bc
commit 973806fe6b
2 changed files with 53 additions and 37 deletions

View File

@ -1639,22 +1639,19 @@ const char* ImStristr(const char* haystack, const char* haystack_end, const char
return NULL; return NULL;
} }
// FIXME-IMSTR: probably unneeded.
const char* ImStrstr(ImStrv haystack, ImStrv needle) const char* ImStrstr(ImStrv haystack, ImStrv needle)
{ {
const char un0 = (char)*needle.Begin; const char un0 = (char)*needle.Begin;
while ((!haystack.End && *haystack.Begin) || (haystack.End && haystack.Begin < haystack.End)) const char* p = haystack.Begin;
const size_t needle_len_m1 = needle.length() - 1;
while (true)
{ {
if (*haystack.Begin == un0) p = (const char*)memchr(p, un0, haystack.End - p);
{ if (p == NULL || (size_t)(haystack.End - p) - 1u < needle_len_m1)
const char* b = needle.Begin + 1; return NULL;
for (const char* a = haystack.Begin + 1; b < needle.End; a++, b++) if (needle_len_m1 == 0 || memcmp(p + 1, needle.Begin + 1, needle_len_m1) == 0)
if (*a != *b) return p;
break; p += 1;
if (b == needle.End)
return haystack.Begin;
}
haystack.Begin++;
} }
return NULL; return NULL;
} }

View File

@ -2351,16 +2351,22 @@ bool ImGui::DragScalar(ImStrv label, ImGuiDataType data_type, void* p_data, floa
return false; return false;
// FIXME-IMSTR // FIXME-IMSTR
char format_0[64]; // format may not end with \0 const char* format;
const char* format = format_0; char format_buf[64];
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (!format_p) if (!format_p)
{
// Default format string when passing NULL
format = DataTypeGetInfo(data_type)->PrintFmt; format = DataTypeGetInfo(data_type)->PrintFmt;
else if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) }
else
{
// Copy format string (format may not be zero-terminated)
format = format_buf;
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_buf));
ImStrncpy(format_buf, format_p, IM_ARRAYSIZE(format_buf));
if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format); format = PatchFormatStringFloatToInt(format);
}
// Tabbing or CTRL-clicking on Drag turns it into an InputText // Tabbing or CTRL-clicking on Drag turns it into an InputText
const bool hovered = ItemHoverable(frame_bb, id); const bool hovered = ItemHoverable(frame_bb, id);
@ -2977,16 +2983,19 @@ bool ImGui::SliderScalar(ImStrv label, ImGuiDataType data_type, void* p_data, co
return false; return false;
// FIXME-IMSTR // FIXME-IMSTR
char format_0[64]; // format may not end with \0 const char* format;
const char* format = format_0; char format_buf[64];
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (!format_p) if (!format_p)
{
// Default format string when passing NULL
format = DataTypeGetInfo(data_type)->PrintFmt; format = DataTypeGetInfo(data_type)->PrintFmt;
}
else else
{ {
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0)); // Copy format string (format may not be zero-terminated)
format = format_buf;
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_buf));
ImStrncpy(format_buf, format_p, IM_ARRAYSIZE(format_buf));
if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format); format = PatchFormatStringFloatToInt(format);
} }
@ -3150,16 +3159,19 @@ bool ImGui::VSliderScalar(ImStrv label, const ImVec2& size, ImGuiDataType data_t
return false; return false;
// FIXME-IMSTR // FIXME-IMSTR
char format_0[64]; // format may not end with \0 const char* format;
const char* format = format_0; char format_buf[64];
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (!format_p) if (!format_p)
{
// Default format string when passing NULL
format = DataTypeGetInfo(data_type)->PrintFmt; format = DataTypeGetInfo(data_type)->PrintFmt;
}
else else
{ {
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0)); // Copy format string (format may not be zero-terminated)
format = format_buf;
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_buf));
ImStrncpy(format_buf, format_p, IM_ARRAYSIZE(format_buf));
if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format); format = PatchFormatStringFloatToInt(format);
} }
@ -3401,13 +3413,20 @@ bool ImGui::InputScalar(ImStrv label, ImGuiDataType data_type, void* p_data, con
ImGuiStyle& style = g.Style; ImGuiStyle& style = g.Style;
// FIXME-IMSTR // FIXME-IMSTR
char format_0[64]; // format may not end with \0 const char* format;
const char* format = format_0; char format_buf[64];
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
if (!format_p) if (!format_p)
{
// Default format string when passing NULL
format = DataTypeGetInfo(data_type)->PrintFmt; format = DataTypeGetInfo(data_type)->PrintFmt;
}
else
{
// Copy format string (format may not be zero-terminated)
format = format_buf;
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_buf));
ImStrncpy(format_buf, format_p, IM_ARRAYSIZE(format_buf));
}
char buf[64]; char buf[64];
DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format);