Skip to content

Commit 8fd0a1d

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

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

imgui.cpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -1965,11 +1965,11 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c,
19651965

19661966
int ImStrcmp(ImStrv str1, ImStrv str2)
19671967
{
1968-
size_t str1_len = str1.length();
1969-
size_t str2_len = str2.length();
1968+
int str1_len = str1.length();
1969+
int str2_len = str2.length();
19701970
if (str1_len != str2_len)
1971-
return (int)str1_len - (int)str2_len;
1972-
return memcmp(str1.Begin, str2.Begin, str1_len);
1971+
return str1_len - str2_len;
1972+
return memcmp(str1.Begin, str2.Begin, (size_t)str1_len);
19731973
}
19741974

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

20062006
char* ImStrdup(const char* str)
@@ -2012,7 +2012,7 @@ char* ImStrdup(const char* str)
20122012

20132013
char* ImStrdup(ImStrv str)
20142014
{
2015-
size_t len = str.length();
2015+
size_t len = (size_t)str.length();
20162016
void* buf = IM_ALLOC(len + 1);
20172017
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
20182018
if (len > 0)
@@ -2023,7 +2023,7 @@ char* ImStrdup(ImStrv str)
20232023
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStrv src)
20242024
{
20252025
size_t dst_buf_size = p_dst_size ? *p_dst_size : ImStrlen(dst) + 1;
2026-
size_t src_size = src.length() + 1;
2026+
size_t src_size = (size_t)src.length() + 1;
20272027
if (dst_buf_size < src_size)
20282028
{
20292029
IM_FREE(dst);
@@ -2097,7 +2097,7 @@ const char* ImStrstr(ImStrv haystack, ImStrv needle)
20972097
{
20982098
const char un0 = (char)*needle.Begin;
20992099
const char* p = haystack.Begin;
2100-
const size_t needle_len_m1 = needle.length() - 1;
2100+
const size_t needle_len_m1 = (size_t)needle.length() - 1;
21012101
while (true)
21022102
{
21032103
p = (const char*)memchr(p, un0, haystack.End - p);
@@ -2321,7 +2321,7 @@ ImGuiID ImHashStr(ImStrv str, ImGuiID seed)
23212321
#endif
23222322
if (str.End != NULL)
23232323
{
2324-
size_t data_size = str.length();
2324+
size_t data_size = (size_t)str.length();
23252325
while (data_size-- != 0)
23262326
{
23272327
unsigned char c = *data++;
@@ -2362,8 +2362,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
23622362
#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (defined(__MINGW32__) || (!defined(__CYGWIN__) && !defined(__GNUC__)))
23632363
// We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames.
23642364
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
2365-
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length() + 1, NULL, 0);
2366-
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length() + 1, NULL, 0);
2365+
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length() + 1, NULL, 0);
2366+
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length() + 1, NULL, 0);
23672367

23682368
// Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator.
23692369
// We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314).
@@ -2373,8 +2373,8 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
23732373
local_temp_heap.resize(filename_wsize + mode_wsize);
23742374
wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack;
23752375
wchar_t* mode_wbuf = filename_wbuf + filename_wsize;
2376-
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length(), filename_wbuf, filename_wsize);
2377-
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length(), mode_wbuf, mode_wsize);
2376+
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, filename.length(), filename_wbuf, filename_wsize);
2377+
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, mode.length(), mode_wbuf, mode_wsize);
23782378
filename_wbuf[filename_wsize - 1] = mode_wbuf[mode_wsize - 1] = 0;
23792379
return ::_wfopen(filename_wbuf, mode_wbuf);
23802380
#else
@@ -2978,7 +2978,7 @@ char ImGuiTextBuffer::EmptyString[1] = { 0 };
29782978

29792979
void ImGuiTextBuffer::append(ImStrv str)
29802980
{
2981-
int len = (int)str.length();
2981+
const int len = str.length();
29822982
if (len == 0)
29832983
return;
29842984

@@ -4340,7 +4340,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, ImStrv name) : DrawListInst(NULL)
43404340
memset(this, 0, sizeof(*this));
43414341
Ctx = ctx;
43424342
Name = ImStrdup(name);
4343-
NameBufLen = (int)name.length() + 1;
4343+
NameBufLen = name.length() + 1;
43444344
ID = ImHashStr(name);
43454345
IDStack.push_back(ID);
43464346
MoveId = GetID("#MOVE");
@@ -4875,7 +4875,7 @@ void ImGui::SetClipboardText(ImStrv text)
48754875
ImGuiContext& g = *GImGui;
48764876
if (g.PlatformIO.Platform_SetClipboardTextFn != NULL)
48774877
{
4878-
int len = (int)text.length();
4878+
int len = text.length();
48794879
char* text_p = (char*)IM_ALLOC(len + 1);
48804880
if (len > 0)
48814881
memcpy(text_p, text.Begin, len);
@@ -6173,10 +6173,10 @@ bool ImGui::BeginChildEx(ImStrv name, ImGuiID id, const ImVec2& size_arg, ImGuiC
61736173
// e.g. "ParentName###ParentIdentifier/ChildName###ChildIdentifier" would get hashed incorrectly by ImHashStr(), trailing _%08X somehow fixes it.
61746174
ImStrv temp_window_name;
61756175
/*if (name && parent_window->IDStack.back() == parent_window->ID)
6176-
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, (int)name.length(), name.Begin); // May omit ID if in root of ID stack
6176+
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s", parent_window->Name, name.length(), name.Begin); // May omit ID if in root of ID stack
61776177
else*/
61786178
if (name)
6179-
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, (int)name.length(), name.Begin, id);
6179+
ImFormatStringToTempBuffer(&temp_window_name, "%s/%.*s_%08X", parent_window->Name, name.length(), name.Begin, id);
61806180
else
61816181
ImFormatStringToTempBuffer(&temp_window_name, "%s/%08X", parent_window->Name, id);
61826182

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

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

1474214742
// For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).
1474314743
// 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..
14744-
const int ini_size = (int)ini_data.length();
14744+
const int ini_size = ini_data.length();
1474514745
g.SettingsIniData.Buf.resize((int)ini_size + 1);
1474614746
char* const buf = g.SettingsIniData.Buf.Data;
1474714747
char* const buf_end = buf + ini_size;
@@ -14843,7 +14843,7 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStrv name)
1484314843
name.Begin = p;
1484414844
}
1484514845

14846-
const size_t name_len = name.length();
14846+
const size_t name_len = (size_t)name.length();
1484714847
if (!name_len)
1484814848
{
1484914849
IM_ASSERT(false && "Name must not be empty.");
@@ -15430,7 +15430,7 @@ void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list)
1543015430
// Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct.
1543115431
void ImGui::DebugTextEncoding(ImStrv str)
1543215432
{
15433-
Text("Text: \"%.*s\"", (int)str.length(), str.Begin);
15433+
Text("Text: \"%.*s\"", str.length(), str.Begin);
1543415434
if (!BeginTable("##DebugTextEncoding", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable))
1543515435
return;
1543615436
TableSetupColumn("Offset");

imgui.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ struct ImStrv
311311
ImStrv() { Begin = End = NULL; }
312312
ImStrv(const char* b) { Begin = b; End = b ? b + strlen(b) : NULL; }
313313
ImStrv(const char* b, const char* e){ Begin = b; End = e ? e : b ? b + strlen(b) : NULL; }
314-
inline size_t length() const { return (size_t)(End - Begin); }
314+
inline int length() const { return (int)(End - Begin); }
315315
inline bool empty() const { return Begin == End; } // == "" or == NULL
316316
inline operator bool() const { return Begin != NULL; } // return true when valid ("" is valid, NULL construction is not)
317317
#ifdef IM_STRV_CLASS_EXTRA
@@ -2580,7 +2580,7 @@ struct ImGuiPayload
25802580

25812581
ImGuiPayload() { Clear(); }
25822582
void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; }
2583-
bool IsDataType(ImStrv type) const { size_t len = type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
2583+
bool IsDataType(ImStrv type) const { size_t len = (size_t)type.length(); return DataFrameCount != -1 && memcmp(DataType, type.Begin, len) == 0 && DataType[len] == 0; }
25842584
bool IsPreview() const { return Preview; }
25852585
bool IsDelivery() const { return Delivery; }
25862586
};

imgui_draw.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(ImStrv filename, float size_pixels, cons
26802680
const char* p;
26812681
for (p = filename.End; p > filename.Begin && p[-1] != '/' && p[-1] != '\\'; p--) {}
26822682
filename.Begin = p;
2683-
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", (int)filename.length(), filename.Begin, size_pixels);
2683+
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", filename.length(), filename.Begin, size_pixels);
26842684
}
26852685
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
26862686
}
@@ -2714,7 +2714,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
27142714

27152715
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(ImStrv compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
27162716
{
2717-
int compressed_ttf_size = (((int)compressed_ttf_data_base85.length() + 4) / 5) * 4;
2717+
int compressed_ttf_size = ((compressed_ttf_data_base85.length() + 4) / 5) * 4;
27182718
void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
27192719
Decode85(compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
27202720
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);

imgui_widgets.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1492,10 +1492,10 @@ void ImGui::TextLinkOpenURL(ImStrv label, ImStrv url)
14921492
if (pressed && g.PlatformIO.Platform_OpenInShellFn != NULL)
14931493
{
14941494
ImStrv url_zt;
1495-
ImFormatStringToTempBuffer(&url_zt, "%.*s", (int)url.length(), url.Begin);
1495+
ImFormatStringToTempBuffer(&url_zt, "%.*s", url.length(), url.Begin);
14961496
g.PlatformIO.Platform_OpenInShellFn(&g, url_zt.Begin);
14971497
}
1498-
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
1498+
SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), url.length(), url.Begin); // It is more reassuring for user to _always_ display URL when we same as label
14991499
if (BeginPopupContextItem())
15001500
{
15011501
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_CopyLink)))
@@ -4252,7 +4252,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, ImStrv new_text)
42524252

42534253
// Grow internal buffer if needed
42544254
const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;
4255-
const int new_text_len = (int)new_text.length();
4255+
const int new_text_len = new_text.length();
42564256
if (new_text_len + BufTextLen >= BufSize)
42574257
{
42584258
if (!is_resizable)
@@ -4967,7 +4967,7 @@ bool ImGui::InputTextEx(ImStrv label, ImStrv hint, char* buf, int buf_size, cons
49674967
if (ImStrv clipboard = GetClipboardText())
49684968
{
49694969
// Filter pasted buffer
4970-
const int clipboard_len = (int)clipboard.length();
4970+
const int clipboard_len = clipboard.length();
49714971
ImVector<char> clipboard_filtered;
49724972
clipboard_filtered.reserve(clipboard_len + 1);
49734973
for (const char* s = clipboard.Begin; *s != 0; )
@@ -8595,30 +8595,30 @@ void ImGui::PlotHistogram(ImStrv label, float (*values_getter)(void* data, int i
85958595

85968596
void ImGui::Value(ImStrv prefix, bool b)
85978597
{
8598-
Text("%.*s: %s", (int)prefix.length(), prefix.Begin, (b ? "true" : "false"));
8598+
Text("%.*s: %s", prefix.length(), prefix.Begin, (b ? "true" : "false"));
85998599
}
86008600

86018601
void ImGui::Value(ImStrv prefix, int v)
86028602
{
8603-
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
8603+
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
86048604
}
86058605

86068606
void ImGui::Value(ImStrv prefix, unsigned int v)
86078607
{
8608-
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
8608+
Text("%.*s: %d", prefix.length(), prefix.Begin, v);
86098609
}
86108610

86118611
void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
86128612
{
86138613
if (float_format)
86148614
{
86158615
char fmt[64];
8616-
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", (int)float_format.length(), float_format.Begin);
8617-
Text(fmt, (int)prefix.length(), prefix.Begin, v);
8616+
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", float_format.length(), float_format.Begin);
8617+
Text(fmt, prefix.length(), prefix.Begin, v);
86188618
}
86198619
else
86208620
{
8621-
Text("%.*s: %.3f", (int)prefix.length(), prefix.Begin, v);
8621+
Text("%.*s: %.3f", prefix.length(), prefix.Begin, v);
86228622
}
86238623
}
86248624

0 commit comments

Comments
 (0)