ImGuiTextFilter: minor tweaks, made ImGuiTextRange fields match upcoming ImStrv. Removed exposed ImGuiTextRange helpers.

Rename CountGrep to CountInclude (#3906)
Toward #2435
This commit is contained in:
ocornut 2026-09-18 16:51:18 +02:00
parent f09485d7be
commit c8fc1829d0
2 changed files with 31 additions and 31 deletions

View File

@ -3093,7 +3093,7 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE
ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) //-V1077 ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) //-V1077
{ {
InputBuf[0] = 0; InputBuf[0] = 0;
CountGrep = 0; CountInclude = 0;
if (default_filter) if (default_filter)
{ {
ImStrncpy(InputBuf, default_filter, IM_COUNTOF(InputBuf)); ImStrncpy(InputBuf, default_filter, IM_COUNTOF(InputBuf));
@ -3111,7 +3111,7 @@ bool ImGuiTextFilter::Draw(const char* label, float width)
return value_changed; return value_changed;
} }
void ImGuiTextFilter::ImGuiTextRange::split(char separator, ImVector<ImGuiTextRange>* out) const static void ImStrSplit(const char* b, const char* e, char separator, ImVector<ImGuiTextFilter::ImGuiTextRange>* out)
{ {
out->resize(0); out->resize(0);
const char* wb = b; const char* wb = b;
@ -3120,32 +3120,32 @@ void ImGuiTextFilter::ImGuiTextRange::split(char separator, ImVector<ImGuiTextRa
{ {
if (*we == separator) if (*we == separator)
{ {
out->push_back(ImGuiTextRange(wb, we)); out->push_back(ImGuiTextFilter::ImGuiTextRange(wb, we));
wb = we + 1; wb = we + 1;
} }
we++; we++;
} }
if (wb != we) if (wb != we)
out->push_back(ImGuiTextRange(wb, we)); out->push_back(ImGuiTextFilter::ImGuiTextRange(wb, we));
} }
void ImGuiTextFilter::Build() void ImGuiTextFilter::Build()
{ {
Filters.resize(0); Filters.resize(0);
ImGuiTextRange input_range(InputBuf, InputBuf + ImStrlen(InputBuf)); ImGuiTextRange input_range(InputBuf, InputBuf + ImStrlen(InputBuf));
input_range.split(',', &Filters); ImStrSplit(InputBuf, InputBuf + ImStrlen(InputBuf), ',', &Filters);
CountGrep = 0; CountInclude = 0;
for (ImGuiTextRange& f : Filters) for (ImGuiTextRange& f : Filters)
{ {
while (f.b < f.e && ImCharIsBlankA(f.b[0])) while (f.Begin < f.End && ImCharIsBlankA(f.Begin[0]))
f.b++; f.Begin++;
while (f.e > f.b && ImCharIsBlankA(f.e[-1])) while (f.End > f.Begin && ImCharIsBlankA(f.End[-1]))
f.e--; f.End--;
if (f.empty()) if (f.Begin == f.End)
continue; continue;
if (f.b[0] != '-') if (f.Begin[0] != '-')
CountGrep += 1; CountInclude += 1;
} }
} }
@ -3159,24 +3159,24 @@ bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const
for (const ImGuiTextRange& f : Filters) for (const ImGuiTextRange& f : Filters)
{ {
if (f.b == f.e) if (f.Begin == f.End)
continue; continue;
if (f.b[0] == '-') if (f.Begin[0] == '-')
{ {
// Subtract // Subtract
if (ImStristr(text, text_end, f.b + 1, f.e) != NULL) if (ImStristr(text, text_end, f.Begin + 1, f.End) != NULL)
return false; return false;
} }
else else
{ {
// Grep // Grep
if (ImStristr(text, text_end, f.b, f.e) != NULL) if (ImStristr(text, text_end, f.Begin, f.End) != NULL)
return true; return true;
} }
} }
// Implicit * grep // Implicit * grep
if (CountGrep == 0) if (CountInclude == 0)
return true; return true;
return false; return false;

26
imgui.h
View File

@ -2789,26 +2789,26 @@ struct ImGuiOnceUponAFrame
struct ImGuiTextFilter struct ImGuiTextFilter
{ {
IMGUI_API ImGuiTextFilter(const char* default_filter = ""); IMGUI_API ImGuiTextFilter(const char* default_filter = "");
IMGUI_API bool Draw(const char* label = "Filter (inc,-exc)", float width = 0.0f); // Helper calling InputText+Build
IMGUI_API bool PassFilter(const char* text, const char* text_end = NULL) const; IMGUI_API bool PassFilter(const char* text, const char* text_end = NULL) const;
IMGUI_API void Build(); IMGUI_API void Build(); // Update internal data when filter changes
void Clear() { InputBuf[0] = 0; Build(); } inline void Clear() { InputBuf[0] = 0; Build(); } // Clear filter
bool IsActive() const { return !Filters.empty(); } inline bool IsActive() const { return Filters.Size != 0; } // Useful if you need e.g. an alternative code-path when there are no filters
// [Internal] // Helper to call InputText() + Build() when buffer is changed.
IMGUI_API bool Draw(const char* label = "Filter (inc,-exc)", float width = 0.0f);
// [Internal] Don't use! Will be replaced with ImStrv.
struct ImGuiTextRange struct ImGuiTextRange
{ {
const char* b; const char* Begin;
const char* e; const char* End;
ImGuiTextRange(const char* b, const char* e) { Begin = b; End = e; }
ImGuiTextRange() { b = e = NULL; }
ImGuiTextRange(const char* _b, const char* _e) { b = _b; e = _e; }
bool empty() const { return b == e; }
IMGUI_API void split(char separator, ImVector<ImGuiTextRange>* out) const;
}; };
// [Internal] Members
char InputBuf[256]; char InputBuf[256];
ImVector<ImGuiTextRange>Filters; ImVector<ImGuiTextRange>Filters;
int CountGrep; int CountInclude;
}; };
// Helper: Growable text buffer for logging/accumulating text // Helper: Growable text buffer for logging/accumulating text