Skip to content

Commit 1ec99f4

Browse files
committed
Internals: added ImStrlen/ImMemchr #define to facilitate experimenting with variations. (#8421)
1 parent 377a387 commit 1ec99f4

5 files changed

+56
-53
lines changed

imgui.cpp

+25-25
Original file line numberDiff line numberDiff line change
@@ -1986,15 +1986,15 @@ void ImStrncpy(char* dst, const char* src, size_t count)
19861986

19871987
char* ImStrdup(const char* str)
19881988
{
1989-
size_t len = strlen(str);
1989+
size_t len = ImStrlen(str);
19901990
void* buf = IM_ALLOC(len + 1);
19911991
return (char*)memcpy(buf, (const void*)str, len + 1);
19921992
}
19931993

19941994
char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
19951995
{
1996-
size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1;
1997-
size_t src_size = strlen(src) + 1;
1996+
size_t dst_buf_size = p_dst_size ? *p_dst_size : ImStrlen(dst) + 1;
1997+
size_t src_size = ImStrlen(src) + 1;
19981998
if (dst_buf_size < src_size)
19991999
{
20002000
IM_FREE(dst);
@@ -2007,7 +2007,7 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
20072007

20082008
const char* ImStrchrRange(const char* str, const char* str_end, char c)
20092009
{
2010-
const char* p = (const char*)memchr(str, (int)c, str_end - str);
2010+
const char* p = (const char*)ImMemchr(str, (int)c, str_end - str);
20112011
return p;
20122012
}
20132013

@@ -2022,13 +2022,13 @@ int ImStrlenW(const ImWchar* str)
20222022
// Find end-of-line. Return pointer will point to either first \n, either str_end.
20232023
const char* ImStreolRange(const char* str, const char* str_end)
20242024
{
2025-
const char* p = (const char*)memchr(str, '\n', str_end - str);
2025+
const char* p = (const char*)ImMemchr(str, '\n', str_end - str);
20262026
return p ? p : str_end;
20272027
}
20282028

20292029
const char* ImStrbol(const char* buf_mid_line, const char* buf_begin) // find beginning-of-line
20302030
{
2031-
IM_ASSERT_PARANOID(buf_mid_line >= buf_begin && buf_mid_line <= buf_begin + strlen(buf_begin));
2031+
IM_ASSERT_PARANOID(buf_mid_line >= buf_begin && buf_mid_line <= buf_begin + ImStrlen(buf_begin));
20322032
while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n')
20332033
buf_mid_line--;
20342034
return buf_mid_line;
@@ -2037,7 +2037,7 @@ const char* ImStrbol(const char* buf_mid_line, const char* buf_begin) // find be
20372037
const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end)
20382038
{
20392039
if (!needle_end)
2040-
needle_end = needle + strlen(needle);
2040+
needle_end = needle + ImStrlen(needle);
20412041

20422042
const char un0 = (char)ImToUpper(*needle);
20432043
while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end))
@@ -2158,7 +2158,7 @@ void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end,
21582158
if (buf == NULL)
21592159
buf = "(null)";
21602160
*out_buf = buf;
2161-
if (out_buf_end) { *out_buf_end = buf + strlen(buf); }
2161+
if (out_buf_end) { *out_buf_end = buf + ImStrlen(buf); }
21622162
}
21632163
else if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '*' && fmt[3] == 's' && fmt[4] == 0)
21642164
{
@@ -2567,11 +2567,11 @@ const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const cha
25672567
int ImTextCountLines(const char* in_text, const char* in_text_end)
25682568
{
25692569
if (in_text_end == NULL)
2570-
in_text_end = in_text + strlen(in_text); // FIXME-OPT: Not optimal approach, discourage use for now.
2570+
in_text_end = in_text + ImStrlen(in_text); // FIXME-OPT: Not optimal approach, discourage use for now.
25712571
int count = 0;
25722572
while (in_text < in_text_end)
25732573
{
2574-
const char* line_end = (const char*)memchr(in_text, '\n', in_text_end - in_text);
2574+
const char* line_end = (const char*)ImMemchr(in_text, '\n', in_text_end - in_text);
25752575
in_text = line_end ? line_end + 1 : in_text_end;
25762576
count++;
25772577
}
@@ -2852,7 +2852,7 @@ void ImGuiTextFilter::ImGuiTextRange::split(char separator, ImVector<ImGuiTextRa
28522852
void ImGuiTextFilter::Build()
28532853
{
28542854
Filters.resize(0);
2855-
ImGuiTextRange input_range(InputBuf, InputBuf + strlen(InputBuf));
2855+
ImGuiTextRange input_range(InputBuf, InputBuf + ImStrlen(InputBuf));
28562856
input_range.split(',', &Filters);
28572857

28582858
CountGrep = 0;
@@ -2920,7 +2920,7 @@ char ImGuiTextBuffer::EmptyString[1] = { 0 };
29202920

29212921
void ImGuiTextBuffer::append(const char* str, const char* str_end)
29222922
{
2923-
int len = str_end ? (int)(str_end - str) : (int)strlen(str);
2923+
int len = str_end ? (int)(str_end - str) : (int)ImStrlen(str);
29242924

29252925
// Add zero-terminator the first time
29262926
const int write_off = (Buf.Size != 0) ? Buf.Size : 1;
@@ -2979,7 +2979,7 @@ void ImGuiTextIndex::append(const char* base, int old_size, int new_size)
29792979
if (EndOffset == 0 || base[EndOffset - 1] == '\n')
29802980
LineOffsets.push_back(EndOffset);
29812981
const char* base_end = base + new_size;
2982-
for (const char* p = base + old_size; (p = (const char*)memchr(p, '\n', base_end - p)) != 0; )
2982+
for (const char* p = base + old_size; (p = (const char*)ImMemchr(p, '\n', base_end - p)) != 0; )
29832983
if (++p < base_end) // Don't push a trailing offset on last \n
29842984
LineOffsets.push_back((int)(intptr_t)(p - base));
29852985
EndOffset = ImMax(EndOffset, new_size);
@@ -3603,7 +3603,7 @@ void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool
36033603
else
36043604
{
36053605
if (!text_end)
3606-
text_end = text + strlen(text); // FIXME-OPT
3606+
text_end = text + ImStrlen(text); // FIXME-OPT
36073607
text_display_end = text_end;
36083608
}
36093609

@@ -3621,7 +3621,7 @@ void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end
36213621
ImGuiWindow* window = g.CurrentWindow;
36223622

36233623
if (!text_end)
3624-
text_end = text + strlen(text); // FIXME-OPT
3624+
text_end = text + ImStrlen(text); // FIXME-OPT
36253625

36263626
if (text != text_end)
36273627
{
@@ -4294,7 +4294,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, const char* name) : DrawListInst(NUL
42944294
memset(this, 0, sizeof(*this));
42954295
Ctx = ctx;
42964296
Name = ImStrdup(name);
4297-
NameBufLen = (int)strlen(name) + 1;
4297+
NameBufLen = (int)ImStrlen(name) + 1;
42984298
ID = ImHashStr(name);
42994299
IDStack.push_back(ID);
43004300
MoveId = GetID("#MOVE");
@@ -8829,7 +8829,7 @@ const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord)
88298829
(key != ImGuiKey_None || key_chord == ImGuiKey_None) ? GetKeyName(key) : "");
88308830
size_t len;
88318831
if (key == ImGuiKey_None && key_chord != 0)
8832-
if ((len = strlen(g.TempKeychordName)) != 0) // Remove trailing '+'
8832+
if ((len = ImStrlen(g.TempKeychordName)) != 0) // Remove trailing '+'
88338833
g.TempKeychordName[len - 1] = 0;
88348834
return g.TempKeychordName;
88358835
}
@@ -14134,7 +14134,7 @@ bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_s
1413414134
cond = ImGuiCond_Always;
1413514135

1413614136
IM_ASSERT(type != NULL);
14137-
IM_ASSERT(strlen(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long");
14137+
IM_ASSERT(ImStrlen(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long");
1413814138
IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0));
1413914139
IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once);
1414014140
IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource()
@@ -14378,7 +14378,7 @@ void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char*
1437814378
}
1437914379

1438014380
if (prefix)
14381-
LogRenderedText(ref_pos, prefix, prefix + strlen(prefix)); // Calculate end ourself to ensure "##" are included here.
14381+
LogRenderedText(ref_pos, prefix, prefix + ImStrlen(prefix)); // Calculate end ourself to ensure "##" are included here.
1438214382

1438314383
// Re-adjust padding if we have popped out of our starting depth
1438414384
if (g.LogDepthRef > window->DC.TreeDepth)
@@ -14411,7 +14411,7 @@ void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char*
1441114411
}
1441214412

1441314413
if (suffix)
14414-
LogRenderedText(ref_pos, suffix, suffix + strlen(suffix));
14414+
LogRenderedText(ref_pos, suffix, suffix + ImStrlen(suffix));
1441514415
}
1441614416

1441714417
// Start logging/capturing text output
@@ -14677,7 +14677,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)
1467714677
// For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).
1467814678
// 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..
1467914679
if (ini_size == 0)
14680-
ini_size = strlen(ini_data);
14680+
ini_size = ImStrlen(ini_data);
1468114681
g.SettingsIniData.Buf.resize((int)ini_size + 1);
1468214682
char* const buf = g.SettingsIniData.Buf.Data;
1468314683
char* const buf_end = buf + ini_size;
@@ -14778,7 +14778,7 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)
1477814778
if (const char* p = strstr(name, "###"))
1477914779
name = p;
1478014780
}
14781-
const size_t name_len = strlen(name);
14781+
const size_t name_len = ImStrlen(name);
1478214782

1478314783
// Allocate chunk
1478414784
const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1;
@@ -15070,7 +15070,7 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext*, const char* t
1507015070
if (!main_clipboard)
1507115071
PasteboardCreate(kPasteboardClipboard, &main_clipboard);
1507215072
PasteboardClear(main_clipboard);
15073-
CFDataRef cf_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text));
15073+
CFDataRef cf_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, ImStrlen(text));
1507415074
if (cf_data)
1507515075
{
1507615076
PasteboardPutItemFlavor(main_clipboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), cf_data, 0);
@@ -15124,7 +15124,7 @@ static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext* ctx, const cha
1512415124
{
1512515125
ImGuiContext& g = *ctx;
1512615126
g.ClipboardHandlerData.clear();
15127-
const char* text_end = text + strlen(text);
15127+
const char* text_end = text + ImStrlen(text);
1512815128
g.ClipboardHandlerData.resize((int)(text_end - text) + 1);
1512915129
memcpy(&g.ClipboardHandlerData[0], text, (size_t)(text_end - text));
1513015130
g.ClipboardHandlerData[(int)(text_end - text)] = 0;
@@ -16894,7 +16894,7 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat
1689416894
ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%d", (int)(intptr_t)data_id);
1689516895
break;
1689616896
case ImGuiDataType_String:
16897-
ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%.*s", data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)strlen((const char*)data_id), (const char*)data_id);
16897+
ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%.*s", data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)ImStrlen((const char*)data_id), (const char*)data_id);
1689816898
break;
1689916899
case ImGuiDataType_Pointer:
1690016900
ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "(void*)0x%p", data_id);

imgui_draw.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ namespace IMGUI_STB_NAMESPACE
143143
#define STBTT_fabs(x) ImFabs(x)
144144
#define STBTT_ifloor(x) ((int)ImFloor(x))
145145
#define STBTT_iceil(x) ((int)ImCeil(x))
146+
#define STBTT_strlen(x) ImStrlen(x)
146147
#define STBTT_STATIC
147148
#define STB_TRUETYPE_IMPLEMENTATION
148149
#else
@@ -2672,7 +2673,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels,
26722673
{
26732674
// Store a short copy of filename into into the font name for convenience
26742675
const char* p;
2675-
for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
2676+
for (p = filename + ImStrlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {}
26762677
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels);
26772678
}
26782679
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
@@ -2707,7 +2708,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
27072708

27082709
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
27092710
{
2710-
int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4;
2711+
int compressed_ttf_size = (((int)ImStrlen(compressed_ttf_data_base85) + 4) / 5) * 4;
27112712
void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
27122713
Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
27132714
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);
@@ -4029,7 +4030,7 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c
40294030
ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining)
40304031
{
40314032
if (!text_end)
4032-
text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.
4033+
text_end = text_begin + ImStrlen(text_begin); // FIXME-OPT: Need to avoid this.
40334034

40344035
const float line_height = size;
40354036
const float scale = size / FontSize;
@@ -4129,7 +4130,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
41294130
return;
41304131

41314132
if (!text_end)
4132-
text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
4133+
text_end = text_begin + ImStrlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
41334134

41344135
const float scale = size / FontSize;
41354136
const float line_height = FontSize * scale;
@@ -4141,7 +4142,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
41414142
if (y + line_height < clip_rect.y)
41424143
while (y + line_height < clip_rect.y && s < text_end)
41434144
{
4144-
const char* line_end = (const char*)memchr(s, '\n', text_end - s);
4145+
const char* line_end = (const char*)ImMemchr(s, '\n', text_end - s);
41454146
if (word_wrap_enabled)
41464147
{
41474148
// FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA().
@@ -4165,7 +4166,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im
41654166
float y_end = y;
41664167
while (y_end < clip_rect.w && s_end < text_end)
41674168
{
4168-
s_end = (const char*)memchr(s_end, '\n', text_end - s_end);
4169+
s_end = (const char*)ImMemchr(s_end, '\n', text_end - s_end);
41694170
s_end = s_end ? s_end + 1 : text_end;
41704171
y_end += line_height;
41714172
}

imgui_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v &
372372
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
373373

374374
// Helpers: String
375+
#define ImStrlen strlen
376+
#define ImMemchr memchr
375377
IMGUI_API int ImStricmp(const char* str1, const char* str2); // Case insensitive compare.
376378
IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); // Case insensitive compare to a certain count.
377379
IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); // Copy to a certain count and always zero terminate (strncpy doesn't).

imgui_tables.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo
16481648
if (label != NULL && label[0] != 0)
16491649
{
16501650
column->NameOffset = (ImS16)table->ColumnsNames.size();
1651-
table->ColumnsNames.append(label, label + strlen(label) + 1);
1651+
table->ColumnsNames.append(label, label + ImStrlen(label) + 1);
16521652
}
16531653
}
16541654

0 commit comments

Comments
 (0)