Skip to content

Commit f6293b4

Browse files
committed
ImStr: rework toward ensuring End is always set to constant can be compile time calculated
1 parent ed721a2 commit f6293b4

File tree

4 files changed

+10
-31
lines changed

4 files changed

+10
-31
lines changed

imgui.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,6 @@ const char* ImStristr(const char* haystack, const char* haystack_end, const char
13621362

13631363
const char* ImStrstr(ImStr haystack, ImStr needle)
13641364
{
1365-
IM_IMSTR_ENSURE_HAS_END(needle);
13661365
const char un0 = (char)*needle.Begin;
13671366
while ((!haystack.End && *haystack.Begin) || (haystack.End && haystack.Begin < haystack.End))
13681367
{
@@ -2085,8 +2084,7 @@ bool ImGuiTextFilter::PassFilter(ImStr text) const
20852084
if (Filters.empty())
20862085
return true;
20872086

2088-
IM_IMSTR_ENSURE_HAS_END(text);
2089-
if (text.Empty())
2087+
if (text.Empty()) // FIXME-IMSTR
20902088
text.Begin = text.End = "";
20912089

20922090
for (int i = 0; i != Filters.Size; i++)
@@ -2661,14 +2659,9 @@ void ImGui::RenderText(ImVec2 pos, ImStr text, bool hide_text_after_hash)
26612659
// Hide anything after a '##' string
26622660
const char* text_display_end;
26632661
if (hide_text_after_hash)
2664-
{
26652662
text_display_end = FindRenderedTextEnd(text);
2666-
}
26672663
else
2668-
{
2669-
IM_IMSTR_ENSURE_HAS_END(text);
26702664
text_display_end = text.End;
2671-
}
26722665

26732666
if (text.Begin != text_display_end)
26742667
{
@@ -2682,7 +2675,6 @@ void ImGui::RenderTextWrapped(ImVec2 pos, ImStr text, float wrap_width)
26822675
{
26832676
ImGuiContext& g = *GImGui;
26842677
ImGuiWindow* window = g.CurrentWindow;
2685-
IM_IMSTR_ENSURE_HAS_END(text);
26862678

26872679
if (text.Begin != text.End)
26882680
{
@@ -2918,7 +2910,6 @@ ImGuiID ImGuiWindow::GetID(ImStr str)
29182910
ImGui::KeepAliveID(id);
29192911
#ifdef IMGUI_ENABLE_TEST_ENGINE
29202912
ImGuiContext& g = *GImGui;
2921-
IM_IMSTR_ENSURE_HAS_END(str);
29222913
IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str.Begin, str.End);
29232914
#endif
29242915
return id;
@@ -2954,7 +2945,6 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(ImStr str)
29542945
ImGuiID id = ImHashStr(str, seed);
29552946
#ifdef IMGUI_ENABLE_TEST_ENGINE
29562947
ImGuiContext& g = *GImGui;
2957-
IM_IMSTR_ENSURE_HAS_END(str);
29582948
IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str.Begin, str.End);
29592949
#endif
29602950
return id;
@@ -4414,7 +4404,6 @@ ImVec2 ImGui::CalcTextSize(ImStr text, bool hide_text_after_double_hash, float w
44144404
{
44154405
ImGuiContext& g = *GImGui;
44164406

4417-
IM_IMSTR_ENSURE_HAS_END(text);
44184407
if (hide_text_after_double_hash)
44194408
text.End = FindRenderedTextEnd(text); // Hide anything after a '##' string
44204409

imgui.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,19 @@ struct ImVec4
243243
#endif
244244
};
245245

246-
#define IM_IMSTR_LENGTH(s) (s.Begin ? (s.End ? (size_t)(s.End - s.Begin) : strlen(s.Begin)) : 0)
247-
#define IM_IMSTR_ENSURE_HAS_END(s) if (s.End == NULL) s.End = s.Begin + strlen(s.Begin)
246+
#define IM_IMSTR_LENGTH(s) (size_t)(s.End - s.Begin)
248247

249248
// String view class.
250249
#define IMGUI_HAS_IMSTR 1
251250
struct ImStr
252251
{
253252
const char* Begin;
254253
const char* End;
255-
ImStr() { Begin = End = NULL; }
256-
ImStr(const char* b) { Begin = b; End = NULL; }
257-
ImStr(const char* b, const char* e) { Begin = b; End = e; }
258-
ImStr(const char* b, size_t size) { Begin = b; End = b + size; }
259-
bool Empty() const { return Begin == NULL || Begin == End || Begin[0] == 0; }
254+
ImStr() { Begin = End = NULL; }
255+
ImStr(const char* b) { Begin = b; End = b ? b + strlen(b) : NULL; }
256+
ImStr(const char* b, const char* e) { Begin = b; End = e ? e : b + strlen(b); }
257+
ImStr(const char* b, size_t size) { Begin = b; End = b + size; }
258+
bool Empty() const { return Begin == End || Begin[0] == 0; } // FIXME: Ambiguous
260259
// void EnsureHasEnd() { if (End == NULL) End = Begin + Length(); }
261260
// size_t Length() const
262261
// {
@@ -275,10 +274,10 @@ struct ImStr
275274
return memcmp(Begin, other.Begin, len) == 0;
276275
return false;
277276
}
278-
operator bool() const { return Begin != NULL; }
279-
char operator[](int index) const { return Begin[index]; }
277+
inline operator bool() const { return Begin != NULL; }
278+
inline char operator[](int index) const { return Begin[index]; }
280279
#ifdef IM_IMSTR_CLASS_EXTRA
281-
IM_IMSTR_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImStr.
280+
IM_IMSTR_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your string types and ImStr.
282281
#endif
283282
};
284283

imgui_draw.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,6 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos,
13261326
if ((col & IM_COL32_A_MASK) == 0)
13271327
return;
13281328

1329-
IM_IMSTR_ENSURE_HAS_END(text);
13301329
if (text.Empty())
13311330
return;
13321331

@@ -3019,7 +3018,6 @@ const char* ImFont::CalcWordWrapPositionA(float scale, ImStr text, float wrap_wi
30193018

30203019
// Cut words that cannot possibly fit within one line.
30213020
// e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish"
3022-
IM_IMSTR_ENSURE_HAS_END(text);
30233021

30243022
float line_width = 0.0f;
30253023
float word_width = 0.0f;
@@ -3105,8 +3103,6 @@ const char* ImFont::CalcWordWrapPositionA(float scale, ImStr text, float wrap_wi
31053103

31063104
ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, ImStr text, const char** remaining) const
31073105
{
3108-
IM_IMSTR_ENSURE_HAS_END(text);
3109-
31103106
const float line_height = size;
31113107
const float scale = size / FontSize;
31123108

@@ -3210,8 +3206,6 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
32103206

32113207
void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, ImStr text, float wrap_width, bool cpu_fine_clip) const
32123208
{
3213-
IM_IMSTR_ENSURE_HAS_END(text); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.
3214-
32153209
// Align to be pixel perfect
32163210
pos.x = IM_FLOOR(pos.x);
32173211
pos.y = IM_FLOOR(pos.y);

imgui_widgets.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ void ImGui::TextEx(ImStr text, ImGuiTextFlags flags)
148148
return;
149149

150150
ImGuiContext& g = *GImGui;
151-
IM_IMSTR_ENSURE_HAS_END(text);
152151

153152
const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
154153
const float wrap_pos_x = window->DC.TextWrapPos;
@@ -4182,7 +4181,6 @@ bool ImGui::InputTextEx(ImStr label, ImStr hint, char* buf, int buf_size, const
41824181
if (ImStr clipboard = GetClipboardText())
41834182
{
41844183
// Filter pasted buffer
4185-
IM_IMSTR_ENSURE_HAS_END(clipboard);
41864184
const int clipboard_len = (int)IM_IMSTR_LENGTH(clipboard);
41874185
ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len + 1) * sizeof(ImWchar));
41884186
int clipboard_filtered_len = 0;
@@ -4392,7 +4390,6 @@ bool ImGui::InputTextEx(ImStr label, ImStr hint, char* buf, int buf_size, const
43924390
const char* buf_display_end = NULL; // We have specialized paths below for setting the length
43934391
if (is_displaying_hint)
43944392
{
4395-
IM_IMSTR_ENSURE_HAS_END(hint);
43964393
buf_display = hint.Begin;
43974394
buf_display_end = hint.End;
43984395
}

0 commit comments

Comments
 (0)