2
0
Эх сурвалжийг харах

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

ocornut 1 жил өмнө
parent
commit
6fdfebb85d
4 өөрчлөгдсөн 36 нэмэгдсэн , 36 устгасан
  1. 22 22
      imgui.cpp
  2. 2 2
      imgui.h
  3. 2 2
      imgui_draw.cpp
  4. 10 10
      imgui_widgets.cpp

+ 22 - 22
imgui.cpp

@@ -2049,11 +2049,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.
@@ -2084,7 +2084,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)
@@ -2096,7 +2096,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)
@@ -2107,7 +2107,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 : ImStrlen(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);
@@ -2187,7 +2187,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);
@@ -2411,7 +2411,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++;
@@ -2463,8 +2463,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
 #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (defined(__MINGW32__) || (!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).
@@ -2474,8 +2474,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
@@ -3079,7 +3079,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;
 
@@ -4487,7 +4487,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");
@@ -5044,7 +5044,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);
@@ -6380,10 +6380,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);
 
@@ -12289,7 +12289,7 @@ bool ImGui::BeginPopupMenuEx(ImGuiID id, ImStrv label, ImGuiWindowFlags extra_wi
 
     char name[128];
     IM_ASSERT(extra_window_flags & ImGuiWindowFlags_ChildMenu);
-    ImFormatString(name, IM_ARRAYSIZE(name), "%.*s###Menu_%02d", (int)label.length(), label.Begin, g.BeginMenuDepth); // Recycle windows based on depth
+    ImFormatString(name, IM_ARRAYSIZE(name), "%.*s###Menu_%02d", label.length(), label.Begin, g.BeginMenuDepth); // Recycle windows based on depth
     bool is_open = Begin(name, NULL, extra_window_flags | ImGuiWindowFlags_Popup);
     if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display)
         EndPopup();
@@ -15288,7 +15288,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;
@@ -15385,7 +15385,7 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStrv name)
     // Preserve the full string when ConfigDebugVerboseIniSettings is set to make .ini inspection easier.
     if (g.IO.ConfigDebugIniSettings == false)
         name.Begin = ImHashSkipUncontributingPrefix(name);
-    const size_t name_len = name.length();
+    const size_t name_len = (size_t)name.length();
     if (name_len == 0)
     {
         IM_ASSERT(false && "Name must not be empty.");
@@ -16012,7 +16012,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");

+ 2 - 2
imgui.h

@@ -324,7 +324,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
@@ -2711,7 +2711,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; }
 };

+ 2 - 2
imgui_draw.cpp

@@ -3155,7 +3155,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", (int)filename.length(), filename.Begin);
+        ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s", filename.length(), filename.Begin);
     }
     return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
 }
@@ -3189,7 +3189,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);

+ 10 - 10
imgui_widgets.cpp

@@ -1535,10 +1535,10 @@ bool 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)))
@@ -4319,7 +4319,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 + 1 > obj->TextA.Size && (Flags & ImGuiInputTextFlags_ReadOnly) == 0)
     {
         if (!is_resizable)
@@ -5155,7 +5155,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();
                 ImVector<char> clipboard_filtered;
                 clipboard_filtered.reserve(clipboard_len + 1);
                 for (const char* s = clipboard.Begin; *s != 0; )
@@ -8870,17 +8870,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)
@@ -8888,12 +8888,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);
     }
 }