Skip to content

Commit

Permalink
ImStrv: made length() returns an int as it simplify the most common c…
Browse files Browse the repository at this point in the history
…ase (of passing %.*s to printf)
  • Loading branch information
ocornut committed Dec 11, 2024
1 parent 61094fe commit 696d049
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
42 changes: 21 additions & 21 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1935,11 +1935,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.
Expand Down Expand Up @@ -1970,7 +1970,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)
Expand All @@ -1982,7 +1982,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)
Expand All @@ -1993,7 +1993,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);
Expand Down Expand Up @@ -2066,7 +2066,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);
Expand Down Expand Up @@ -2290,7 +2290,7 @@ ImGuiID ImHashStr(ImStrv str, ImGuiID seed)
#endif
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++;
Expand Down Expand Up @@ -2331,8 +2331,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).
Expand All @@ -2342,8 +2342,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
Expand Down Expand Up @@ -2947,7 +2947,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;

Expand Down Expand Up @@ -4299,7 +4299,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");
Expand Down Expand Up @@ -4811,7 +4811,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);
Expand Down Expand Up @@ -6097,10 +6097,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);

Expand Down Expand Up @@ -14537,7 +14537,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;
Expand Down Expand Up @@ -14639,7 +14639,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.");
Expand Down Expand Up @@ -15203,7 +15203,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");
Expand Down
4 changes: 2 additions & 2 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,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
Expand Down Expand Up @@ -2545,7 +2545,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; }
};
Expand Down
4 changes: 2 additions & 2 deletions imgui_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2640,7 +2640,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);
}
Expand Down Expand Up @@ -2674,7 +2674,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);
Expand Down
20 changes: 10 additions & 10 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -4214,7 +4214,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, ImStrv new_text)

// Grow internal buffer if needed
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)
Expand Down Expand Up @@ -4909,7 +4909,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; )
Expand Down Expand Up @@ -8516,30 +8516,30 @@ 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)
{
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);
}
}

Expand Down

0 comments on commit 696d049

Please sign in to comment.