From 42567a95166aa6f873858718c0fcdbf08bd5d937 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 13 Aug 2015 21:25:32 -0600 Subject: [PATCH] ImGuiTextFilter::PassFilter() supports string range. Added [] helper to ImGuiTextBuffer. --- imgui.cpp | 15 ++++++++------- imgui.h | 4 +++- imgui_internal.h | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index bd60d7bc..920168b6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -777,13 +777,13 @@ const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) return buf_mid_line; } -const char* ImStristr(const char* haystack, const char* needle, const char* needle_end) +const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end) { if (!needle_end) needle_end = needle + strlen(needle); const char un0 = (char)toupper(*needle); - while (*haystack) + while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end)) { if (toupper(*haystack) == un0) { @@ -1326,13 +1326,13 @@ void ImGuiTextFilter::Build() } } -bool ImGuiTextFilter::PassFilter(const char* val) const +bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const { if (Filters.empty()) return true; - if (val == NULL) - val = ""; + if (text == NULL) + text = ""; for (int i = 0; i != Filters.Size; i++) { @@ -1342,13 +1342,13 @@ bool ImGuiTextFilter::PassFilter(const char* val) const if (f.front() == '-') { // Subtract - if (ImStristr(val, f.begin()+1, f.end()) != NULL) + if (ImStristr(text, text_end, f.begin()+1, f.end()) != NULL) return false; } else { // Grep - if (ImStristr(val, f.begin(), f.end()) != NULL) + if (ImStristr(text, text_end, f.begin(), f.end()) != NULL) return true; } } @@ -2635,6 +2635,7 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex // Helper to calculate coarse clipping of large list of evenly sized items. // NB: Prefer using the ImGuiListClipper higher-level helper if you can! +// NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX // If you are displaying thousands of items and you have a random access to the list, you can perform clipping yourself to save on CPU. // { // float item_height = ImGui::GetTextLineHeightWithSpacing(); diff --git a/imgui.h b/imgui.h index e0cf6c66..821e9e34 100644 --- a/imgui.h +++ b/imgui.h @@ -851,7 +851,7 @@ struct ImGuiTextFilter ImGuiTextFilter(const char* default_filter = ""); void Clear() { InputBuf[0] = 0; Build(); } void Draw(const char* label = "Filter (inc,-exc)", float width = -1.0f); // Helper calling InputText+Build - bool PassFilter(const char* val) const; + bool PassFilter(const char* text, const char* text_end = NULL) const; bool IsActive() const { return !Filters.empty(); } IMGUI_API void Build(); }; @@ -862,6 +862,7 @@ struct ImGuiTextBuffer ImVector Buf; ImGuiTextBuffer() { Buf.push_back(0); } + inline char operator[](int i) { return Buf.Data[i]; } const char* begin() const { return &Buf.front(); } const char* end() const { return &Buf.back(); } // Buf is zero-terminated, so end() will point on the zero-terminator int size() const { return Buf.Size-1; } @@ -962,6 +963,7 @@ struct ImColor // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) // display only visible items // ImGui::Text("line number %d", i); // clipper.End(); +// NB: 'count' is only used to clamp the result, if you don't know your count you can use INT_MAX struct ImGuiListClipper { float ItemsHeight; diff --git a/imgui_internal.h b/imgui_internal.h index 8512a635..9e09fd30 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -93,7 +93,7 @@ int ImStrnicmp(const char* str1, const char* str2, int count); char* ImStrdup(const char* str); int ImStrlenW(const ImWchar* str); const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line -const char* ImStristr(const char* haystack, const char* needle, const char* needle_end); +const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); int ImFormatString(char* buf, int buf_size, const char* fmt, ...) IM_PRINTFARGS(3); int ImFormatStringV(char* buf, int buf_size, const char* fmt, va_list args);