Browse Source

ImStrv: made length() returns an int as it simplify the most common case (of passing %.*s to printf)

features/string_view
ocornut 4 months ago
parent
commit
f759cdff2b
  1. 42
      imgui.cpp
  2. 4
      imgui.h
  3. 4
      imgui_draw.cpp
  4. 20
      imgui_widgets.cpp

42
imgui.cpp

@ -1945,11 +1945,11 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c,
int ImStrcmp(ImStrv str1, ImStrv str2)
{
size_t str1_len = str1.length();
size_t str2_len = str2.length();
int str1_len = str1.length();
int str2_len = str2.length();
if (str1_len != str2_len)
return (int)str1_len - (int)str2_len;
return memcmp(str1.Begin, str2.Begin, str1_len);
return str1_len - str2_len;
return memcmp(str1.Begin, str2.Begin, (size_t)str1_len);
}
// Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more.
@ -1980,7 +1980,7 @@ void ImStrncpy(char* dst, ImStrv src, size_t count)
{
// Even though src does not necessarily include \0 terminator it is ok to include it. ImStrncpy above does not
// actually include that in a copy operation and inserts zero terminator manually.
ImStrncpy(dst, src.Begin, ImMin(count, src.length() + 1));
ImStrncpy(dst, src.Begin, ImMin(count, (size_t)src.length() + 1));
}
char* ImStrdup(const char* str)
@ -1992,7 +1992,7 @@ char* ImStrdup(const char* str)
char* ImStrdup(ImStrv str)
{
size_t len = str.length();
size_t len = (size_t)str.length();
void* buf = IM_ALLOC(len + 1);
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
if (len > 0)
@ -2003,7 +2003,7 @@ char* ImStrdup(ImStrv str)
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStrv src)
{
size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1;
size_t src_size = src.length() + 1;
size_t src_size = (size_t)src.length() + 1;
if (dst_buf_size < src_size)
{
IM_FREE(dst);
@ -2076,7 +2076,7 @@ const char* ImStrstr(ImStrv haystack, ImStrv needle)
{
const char un0 = (char)*needle.Begin;
const char* p = haystack.Begin;
const size_t needle_len_m1 = needle.length() - 1;
const size_t needle_len_m1 = (size_t)needle.length() - 1;
while (true)
{
p = (const char*)memchr(p, un0, haystack.End - p);
@ -2263,7 +2263,7 @@ ImGuiID ImHashStr(ImStrv str, ImGuiID seed)
const ImU32* crc32_lut = GCrc32LookupTable;
if (str.End != NULL)
{
size_t data_size = str.length();
size_t data_size = (size_t)str.length();
while (data_size-- != 0)
{
unsigned char c = *data++;
@ -2296,8 +2296,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(__CYGWIN__) && !defined(__GNUC__)
// We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames.
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length() + 1, NULL, 0);
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length() + 1, NULL, 0);
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length() + 1, NULL, 0);
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length() + 1, NULL, 0);
// Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator.
// We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314).
@ -2307,8 +2307,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
local_temp_heap.resize(filename_wsize + mode_wsize);
wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack;
wchar_t* mode_wbuf = filename_wbuf + filename_wsize;
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length(), filename_wbuf, filename_wsize);
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length(), mode_wbuf, mode_wsize);
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length(), filename_wbuf, filename_wsize);
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length(), mode_wbuf, mode_wsize);
filename_wbuf[filename_wsize - 1] = mode_wbuf[mode_wsize - 1] = 0;
return ::_wfopen(filename_wbuf, mode_wbuf);
#else
@ -2912,7 +2912,7 @@ char ImGuiTextBuffer::EmptyString[1] = { 0 };
void ImGuiTextBuffer::append(ImStrv str)
{
int len = (int)str.length();
const int len = str.length();
if (len == 0)
return;
@ -4263,7 +4263,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, ImStrv name) : DrawListInst(NULL)
memset(this, 0, sizeof(*this));
Ctx = ctx;
Name = ImStrdup(name);
NameBufLen = (int)name.length() + 1;
NameBufLen = name.length() + 1;
ID = ImHashStr(name);
IDStack.push_back(ID);
MoveId = GetID("#MOVE");
@ -4775,7 +4775,7 @@ void ImGui::SetClipboardText(ImStrv text)
ImGuiContext& g = *GImGui;
if (g.PlatformIO.Platform_SetClipboardTextFn != NULL)
{
int len = (int)text.length();
int len = text.length();
char* text_p = (char*)IM_ALLOC(len + 1);
if (len > 0)
memcpy(text_p, text.Begin, len);
@ -6054,10 +6054,10 @@ bool ImGui::BeginChildEx(ImStrv name, ImGuiID id, const ImVec2& size_arg, ImGuiC
// e.g. "ParentName###ParentIdentifier/ChildName###ChildIdentifier" would get hashed incorrectly by ImHashStr(), trailing _%08X somehow fixes it.
ImStrv temp_window_name;
/*if (name && parent_window->IDStack.back() == parent_window->ID)
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, (int)name.length(), name.Begin); // May omit ID if in root of ID stack
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, name.length(), name.Begin); // May omit ID if in root of ID stack
else*/
if (name)
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, (int)name.length(), name.Begin, id);
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, name.length(), name.Begin, id);
else
ImFormatStringToTempBuffer(&temp_window_name, "%s/%08X", parent_window->Name, id);
@ -14607,7 +14607,7 @@ void ImGui::LoadIniSettingsFromMemory(ImStrv ini_data)
// For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).
// For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy..
const int ini_size = (int)ini_data.length();
const int ini_size = ini_data.length();
g.SettingsIniData.Buf.resize((int)ini_size + 1);
char* const buf = g.SettingsIniData.Buf.Data;
char* const buf_end = buf + ini_size;
@ -14709,7 +14709,7 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStrv name)
name.Begin = p;
}
const size_t name_len = name.length();
const size_t name_len = (size_t)name.length();
if (!name_len)
{
IM_ASSERT(false && "Name must not be empty.");
@ -15273,7 +15273,7 @@ void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list)
// Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct.
void ImGui::DebugTextEncoding(ImStrv str)
{
Text("Text: \"%.*s\"", (int)str.length(), str.Begin);
Text("Text: \"%.*s\"", str.length(), str.Begin);
if (!BeginTable("##DebugTextEncoding", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable))
return;
TableSetupColumn("Offset");

4
imgui.h

@ -322,7 +322,7 @@ struct ImStrv
ImStrv() { Begin = End = NULL; }
ImStrv(const char* b) { Begin = b; End = b ? b + strlen(b) : NULL; }
ImStrv(const char* b, const char* e){ Begin = b; End = e ? e : b ? b + strlen(b) : NULL; }
inline size_t length() const { return (size_t)(End - Begin); }
inline int length() const { return (int)(End - Begin); }
inline bool empty() const { return Begin == End; } // == "" or == NULL
inline operator bool() const { return Begin != NULL; } // return true when valid ("" is valid, NULL construction is not)
#ifdef IM_STRV_CLASS_EXTRA
@ -2564,7 +2564,7 @@ struct ImGuiPayload
ImGuiPayload() { Clear(); }
void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; }
bool IsDataType(ImStrv type) const { size_t len = type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
bool IsDataType(ImStrv type) const { size_t len = (size_t)type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
bool IsPreview() const { return Preview; }
bool IsDelivery() const { return Delivery; }
};

4
imgui_draw.cpp

@ -2611,7 +2611,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(ImStrv filename, float size_pixels, cons
const char* p;
for (p = filename.End; p > filename.Begin && p[-1] != '/' && p[-1] != '\\'; p--) {}
filename.Begin = p;
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", (int)filename.length(), filename.Begin, size_pixels);
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", filename.length(), filename.Begin, size_pixels);
}
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
}
@ -2645,7 +2645,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(ImStrv compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
{
int compressed_ttf_size = (((int)compressed_ttf_data_base85.length() + 4) / 5) * 4;
int compressed_ttf_size = ((compressed_ttf_data_base85.length() + 4) / 5) * 4;
void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
Decode85(compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);

20
imgui_widgets.cpp

@ -1461,10 +1461,10 @@ void ImGui::TextLinkOpenURL(ImStrv label, ImStrv url)
if (pressed && g.PlatformIO.Platform_OpenInShellFn != NULL)
{
ImStrv url_zt;
ImFormatStringToTempBuffer(&url_zt, "%.*s", (int)url.length(), url.Begin);
ImFormatStringToTempBuffer(&url_zt, "%.*s", url.length(), url.Begin);
g.PlatformIO.Platform_OpenInShellFn(&g, url_zt.Begin);
}
SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), (int)url.length(), url.Begin); // It is more reassuring for user to _always_ display URL when we same as label
SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), url.length(), url.Begin); // It is more reassuring for user to _always_ display URL when we same as label
if (BeginPopupContextItem())
{
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_CopyLink)))
@ -4212,7 +4212,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, ImStrv new_text)
return;
const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;
const int new_text_len = (int)new_text.length();
const int new_text_len = new_text.length();
if (new_text_len + BufTextLen >= BufSize)
{
if (!is_resizable)
@ -4902,7 +4902,7 @@ bool ImGui::InputTextEx(ImStrv label, ImStrv hint, char* buf, int buf_size, cons
if (ImStrv clipboard = GetClipboardText())
{
// Filter pasted buffer
const int clipboard_len = (int)clipboard.length();
const int clipboard_len = clipboard.length();
char* clipboard_filtered = (char*)IM_ALLOC(clipboard_len + 1);
int clipboard_filtered_len = 0;
for (const char* s = clipboard.Begin; *s; )
@ -8507,17 +8507,17 @@ void ImGui::PlotHistogram(ImStrv label, float (*values_getter)(void* data, int i
void ImGui::Value(ImStrv prefix, bool b)
{
Text("%.*s: %s", (int)prefix.length(), prefix.Begin, (b ? "true" : "false"));
Text("%.*s: %s", prefix.length(), prefix.Begin, (b ? "true" : "false"));
}
void ImGui::Value(ImStrv prefix, int v)
{
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
}
void ImGui::Value(ImStrv prefix, unsigned int v)
{
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
}
void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
@ -8525,12 +8525,12 @@ void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
if (float_format)
{
char fmt[64];
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", (int)float_format.length(), float_format.Begin);
Text(fmt, (int)prefix.length(), prefix.Begin, v);
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", float_format.length(), float_format.Begin);
Text(fmt, prefix.length(), prefix.Begin, v);
}
else
{
Text("%.*s: %.3f", (int)prefix.length(), prefix.Begin, v);
Text("%.*s: %.3f", prefix.length(), prefix.Begin, v);
}
}

Loading…
Cancel
Save