Skip to content

Commit 6307b44

Browse files
committed
ImStrv: made length() returns an int as it simplify the most common case (of passing %.*s to printf)
1 parent 622a8b7 commit 6307b44

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

imgui.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,11 +2054,11 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c,
20542054

20552055
int ImStrcmp(ImStrv str1, ImStrv str2)
20562056
{
2057-
size_t str1_len = str1.length();
2058-
size_t str2_len = str2.length();
2057+
int str1_len = str1.length();
2058+
int str2_len = str2.length();
20592059
if (str1_len != str2_len)
2060-
return (int)str1_len - (int)str2_len;
2061-
return memcmp(str1.Begin, str2.Begin, str1_len);
2060+
return str1_len - str2_len;
2061+
return memcmp(str1.Begin, str2.Begin, (size_t)str1_len);
20622062
}
20632063

20642064
// Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more.
@@ -2089,7 +2089,7 @@ void ImStrncpy(char* dst, ImStrv src, size_t count)
20892089
{
20902090
// Even though src does not necessarily include \0 terminator it is ok to include it. ImStrncpy above does not
20912091
// actually include that in a copy operation and inserts zero terminator manually.
2092-
ImStrncpy(dst, src.Begin, ImMin(count, src.length() + 1));
2092+
ImStrncpy(dst, src.Begin, ImMin(count, (size_t)src.length() + 1));
20932093
}
20942094

20952095
char* ImStrdup(const char* str)
@@ -2101,7 +2101,7 @@ char* ImStrdup(const char* str)
21012101

21022102
char* ImStrdup(ImStrv str)
21032103
{
2104-
size_t len = str.length();
2104+
size_t len = (size_t)str.length();
21052105
void* buf = IM_ALLOC(len + 1);
21062106
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
21072107
if (len > 0)
@@ -2112,7 +2112,7 @@ char* ImStrdup(ImStrv str)
21122112
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStrv src)
21132113
{
21142114
size_t dst_buf_size = p_dst_size ? *p_dst_size : ImStrlen(dst) + 1;
2115-
size_t src_size = src.length() + 1;
2115+
size_t src_size = (size_t)src.length() + 1;
21162116
if (dst_buf_size < src_size)
21172117
{
21182118
IM_FREE(dst);
@@ -2192,7 +2192,7 @@ const char* ImStrstr(ImStrv haystack, ImStrv needle)
21922192
{
21932193
const char un0 = (char)*needle.Begin;
21942194
const char* p = haystack.Begin;
2195-
const size_t needle_len_m1 = needle.length() - 1;
2195+
const size_t needle_len_m1 = (size_t)needle.length() - 1;
21962196
while (true)
21972197
{
21982198
p = (const char*)memchr(p, un0, haystack.End - p);
@@ -2416,7 +2416,7 @@ ImGuiID ImHashStr(ImStrv str, ImGuiID seed)
24162416
#endif
24172417
if (str.End != NULL)
24182418
{
2419-
size_t data_size = str.length();
2419+
size_t data_size = (size_t)str.length();
24202420
while (data_size-- != 0)
24212421
{
24222422
unsigned char c = *data++;
@@ -2468,8 +2468,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
24682468
#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (defined(__MINGW32__) || (!defined(__CYGWIN__) && !defined(__GNUC__)))
24692469
// We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames.
24702470
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
2471-
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length() + 1, NULL, 0);
2472-
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length() + 1, NULL, 0);
2471+
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length() + 1, NULL, 0);
2472+
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length() + 1, NULL, 0);
24732473

24742474
// Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator.
24752475
// We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314).
@@ -2479,8 +2479,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
24792479
local_temp_heap.resize(filename_wsize + mode_wsize);
24802480
wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack;
24812481
wchar_t* mode_wbuf = filename_wbuf + filename_wsize;
2482-
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length(), filename_wbuf, filename_wsize);
2483-
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length(), mode_wbuf, mode_wsize);
2482+
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length(), filename_wbuf, filename_wsize);
2483+
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length(), mode_wbuf, mode_wsize);
24842484
filename_wbuf[filename_wsize - 1] = mode_wbuf[mode_wsize - 1] = 0;
24852485
return ::_wfopen(filename_wbuf, mode_wbuf);
24862486
#else
@@ -3097,7 +3097,7 @@ char ImGuiTextBuffer::EmptyString[1] = { 0 };
30973097

30983098
void ImGuiTextBuffer::append(ImStrv str)
30993099
{
3100-
int len = (int)str.length();
3100+
const int len = str.length();
31013101
if (len == 0)
31023102
return;
31033103

@@ -4511,7 +4511,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, ImStrv name) : DrawListInst(NULL)
45114511
memset(this, 0, sizeof(*this));
45124512
Ctx = ctx;
45134513
Name = ImStrdup(name);
4514-
NameBufLen = (int)name.length() + 1;
4514+
NameBufLen = name.length() + 1;
45154515
ID = ImHashStr(name);
45164516
IDStack.push_back(ID);
45174517
MoveId = GetID("#MOVE");
@@ -5069,7 +5069,7 @@ void ImGui::SetClipboardText(ImStrv text)
50695069
ImGuiContext& g = *GImGui;
50705070
if (g.PlatformIO.Platform_SetClipboardTextFn != NULL)
50715071
{
5072-
int len = (int)text.length();
5072+
int len = text.length();
50735073
char* text_p = (char*)IM_ALLOC(len + 1);
50745074
if (len > 0)
50755075
memcpy(text_p, text.Begin, len);
@@ -6407,10 +6407,10 @@ bool ImGui::BeginChildEx(ImStrv name, ImGuiID id, const ImVec2& size_arg, ImGuiC
64076407
// e.g. "ParentName###ParentIdentifier/ChildName###ChildIdentifier" would get hashed incorrectly by ImHashStr(), trailing _%08X somehow fixes it.
64086408
ImStrv temp_window_name;
64096409
/*if (name && parent_window->IDStack.back() == parent_window->ID)
6410-
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, (int)name.length(), name.Begin); // May omit ID if in root of ID stack
6410+
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, name.length(), name.Begin); // May omit ID if in root of ID stack
64116411
else*/
64126412
if (name)
6413-
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, (int)name.length(), name.Begin, id);
6413+
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, name.length(), name.Begin, id);
64146414
else
64156415
ImFormatStringToTempBuffer(&temp_window_name, "%s/%08X", parent_window->Name, id);
64166416

@@ -12321,7 +12321,7 @@ bool ImGui::BeginPopupMenuEx(ImGuiID id, ImStrv label, ImGuiWindowFlags extra_wi
1232112321

1232212322
char name[128];
1232312323
IM_ASSERT(extra_window_flags & ImGuiWindowFlags_ChildMenu);
12324-
ImFormatString(name, IM_ARRAYSIZE(name), "%.*s###Menu_%02d", (int)label.length(), label.Begin, g.BeginMenuDepth); // Recycle windows based on depth
12324+
ImFormatString(name, IM_ARRAYSIZE(name), "%.*s###Menu_%02d", label.length(), label.Begin, g.BeginMenuDepth); // Recycle windows based on depth
1232512325
bool is_open = Begin(name, NULL, extra_window_flags | ImGuiWindowFlags_Popup);
1232612326
if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display)
1232712327
EndPopup();
@@ -15320,7 +15320,7 @@ void ImGui::LoadIniSettingsFromMemory(ImStrv ini_data)
1532015320

1532115321
// For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).
1532215322
// 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..
15323-
const int ini_size = (int)ini_data.length();
15323+
const int ini_size = ini_data.length();
1532415324
g.SettingsIniData.Buf.resize((int)ini_size + 1);
1532515325
char* const buf = g.SettingsIniData.Buf.Data;
1532615326
char* const buf_end = buf + ini_size;
@@ -15417,7 +15417,7 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStrv name)
1541715417
// Preserve the full string when ConfigDebugVerboseIniSettings is set to make .ini inspection easier.
1541815418
if (g.IO.ConfigDebugIniSettings == false)
1541915419
name.Begin = ImHashSkipUncontributingPrefix(name);
15420-
const size_t name_len = name.length();
15420+
const size_t name_len = (size_t)name.length();
1542115421
if (name_len == 0)
1542215422
{
1542315423
IM_ASSERT(false && "Name must not be empty.");
@@ -16044,7 +16044,7 @@ void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list)
1604416044
// Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct.
1604516045
void ImGui::DebugTextEncoding(ImStrv str)
1604616046
{
16047-
Text("Text: \"%.*s\"", (int)str.length(), str.Begin);
16047+
Text("Text: \"%.*s\"", str.length(), str.Begin);
1604816048
if (!BeginTable("##DebugTextEncoding", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable))
1604916049
return;
1605016050
TableSetupColumn("Offset");

imgui.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ struct ImStrv
327327
ImStrv() { Begin = End = NULL; }
328328
ImStrv(const char* b) { Begin = b; End = b ? b + strlen(b) : NULL; }
329329
ImStrv(const char* b, const char* e){ Begin = b; End = e ? e : b ? b + strlen(b) : NULL; }
330-
inline size_t length() const { return (size_t)(End - Begin); }
330+
inline int length() const { return (int)(End - Begin); }
331331
inline bool empty() const { return Begin == End; } // == "" or == NULL
332332
inline operator bool() const { return Begin != NULL; } // return true when valid ("" is valid, NULL construction is not)
333333
#ifdef IM_STRV_CLASS_EXTRA
@@ -2714,7 +2714,7 @@ struct ImGuiPayload
27142714

27152715
ImGuiPayload() { Clear(); }
27162716
void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; }
2717-
bool IsDataType(ImStrv type) const { size_t len = type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
2717+
bool IsDataType(ImStrv type) const { size_t len = (size_t)type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
27182718
bool IsPreview() const { return Preview; }
27192719
bool IsDelivery() const { return Delivery; }
27202720
};

imgui_draw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,7 +3155,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(ImStrv filename, float size_pixels, cons
31553155
const char* p;
31563156
for (p = filename.End; p > filename.Begin && p[-1] != '/' && p[-1] != '\\'; p--) {}
31573157
filename.Begin = p;
3158-
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s", (int)filename.length(), filename.Begin);
3158+
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s", filename.length(), filename.Begin);
31593159
}
31603160
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
31613161
}
@@ -3189,7 +3189,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
31893189

31903190
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(ImStrv compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
31913191
{
3192-
int compressed_ttf_size = (((int)compressed_ttf_data_base85.length() + 4) / 5) * 4;
3192+
int compressed_ttf_size = ((compressed_ttf_data_base85.length() + 4) / 5) * 4;
31933193
void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
31943194
Decode85(compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
31953195
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);

imgui_widgets.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,10 +1538,10 @@ bool ImGui::TextLinkOpenURL(ImStrv label, ImStrv url)
15381538
if (pressed && g.PlatformIO.Platform_OpenInShellFn != NULL)
15391539
{
15401540
ImStrv url_zt;
1541-
ImFormatStringToTempBuffer(&url_zt, "%.*s", (int)url.length(), url.Begin);
1541+
ImFormatStringToTempBuffer(&url_zt, "%.*s", url.length(), url.Begin);
15421542
g.PlatformIO.Platform_OpenInShellFn(&g, url_zt.Begin);
15431543
}
1544-
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
1544+
SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), url.length(), url.Begin); // It is more reassuring for user to _always_ display URL when we same as label
15451545
if (BeginPopupContextItem())
15461546
{
15471547
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_CopyLink)))
@@ -4325,7 +4325,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, ImStrv new_text)
43254325
IM_ASSERT(obj->ID != 0 && g.ActiveId == obj->ID);
43264326
const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;
43274327
const bool is_readonly = (Flags & ImGuiInputTextFlags_ReadOnly) != 0;
4328-
int new_text_len = (int)new_text.length();
4328+
int new_text_len = new_text.length();
43294329

43304330
// We support partial insertion (with a mod in stb_textedit.h)
43314331
const int avail = BufSize - 1 - BufTextLen;
@@ -5168,7 +5168,7 @@ bool ImGui::InputTextEx(ImStrv label, ImStrv hint, char* buf, int buf_size, cons
51685168
if (ImStrv clipboard = GetClipboardText())
51695169
{
51705170
// Filter pasted buffer
5171-
const int clipboard_len = (int)clipboard.length();
5171+
const int clipboard_len = clipboard.length();
51725172
ImVector<char> clipboard_filtered;
51735173
clipboard_filtered.reserve(clipboard_len + 1);
51745174
for (const char* s = clipboard.Begin; *s != 0; )
@@ -8888,30 +8888,30 @@ void ImGui::PlotHistogram(ImStrv label, float (*values_getter)(void* data, int i
88888888

88898889
void ImGui::Value(ImStrv prefix, bool b)
88908890
{
8891-
Text("%.*s: %s", (int)prefix.length(), prefix.Begin, (b ? "true" : "false"));
8891+
Text("%.*s: %s", prefix.length(), prefix.Begin, (b ? "true" : "false"));
88928892
}
88938893

88948894
void ImGui::Value(ImStrv prefix, int v)
88958895
{
8896-
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
8896+
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
88978897
}
88988898

88998899
void ImGui::Value(ImStrv prefix, unsigned int v)
89008900
{
8901-
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
8901+
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
89028902
}
89038903

89048904
void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
89058905
{
89068906
if (float_format)
89078907
{
89088908
char fmt[64];
8909-
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", (int)float_format.length(), float_format.Begin);
8910-
Text(fmt, (int)prefix.length(), prefix.Begin, v);
8909+
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", float_format.length(), float_format.Begin);
8910+
Text(fmt, prefix.length(), prefix.Begin, v);
89118911
}
89128912
else
89138913
{
8914-
Text("%.*s: %.3f", (int)prefix.length(), prefix.Begin, v);
8914+
Text("%.*s: %.3f", prefix.length(), prefix.Begin, v);
89158915
}
89168916
}
89178917

0 commit comments

Comments
 (0)