From 6cd420511dcc5ca81d79f891427e482be22f7e0e Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Mon, 23 Nov 2009 20:18:57 +0100 Subject: [PATCH] perform case insensitive searching in text fields --- src/Makefile.am | 9 ++++----- src/conv.cpp | 5 ----- src/conv.h | 2 -- src/info.cpp | 2 +- src/ncmpcpp.cpp | 2 +- src/scrollpad.cpp | 9 ++++----- src/scrollpad.h | 4 +++- src/strbuffer.h | 35 ++++++++++++++++++++++++----------- 8 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index cc510b1b..bfb028c8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ ncmpcpp_SOURCES = browser.cpp charset.cpp clock.cpp conv.cpp display.cpp \ error.cpp help.cpp helpers.cpp info.cpp lyrics.cpp media_library.cpp menu.cpp \ misc.cpp mpdpp.cpp ncmpcpp.cpp outputs.cpp playlist.cpp playlist_editor.cpp \ scrollpad.cpp search_engine.cpp server_info.cpp settings.cpp song.cpp status.cpp \ - tag_editor.cpp tiny_tag_editor.cpp visualizer.cpp window.cpp + tag_editor.cpp tiny_tag_editor.cpp tolower.cpp visualizer.cpp window.cpp # set the include path found by configure INCLUDES= $(all_includes) @@ -11,7 +11,6 @@ INCLUDES= $(all_includes) # the library search path. ncmpcpp_LDFLAGS = $(all_libraries) noinst_HEADERS = browser.h charset.h clock.h conv.h display.h error.h global.h \ - help.h helpers.h home.h info.h lyrics.h media_library.h menu.h misc.h \ - mpdpp.h outputs.h playlist_editor.h screen.h scrollpad.h search_engine.h \ - server_info.h settings.h song.h tag_editor.h tiny_tag_editor.h visualizer.h \ - window.h + help.h helpers.h home.h info.h lyrics.h media_library.h menu.h misc.h mpdpp.h \ + outputs.h playlist_editor.h screen.h scrollpad.h search_engine.h server_info.h \ + settings.h song.h tag_editor.h tiny_tag_editor.h tolower.h visualizer.h window.h diff --git a/src/conv.cpp b/src/conv.cpp index f48bbe14..6ff0431f 100644 --- a/src/conv.cpp +++ b/src/conv.cpp @@ -23,11 +23,6 @@ #include "conv.h" -void ToLower(std::string &s) -{ - transform(s.begin(), s.end(), s.begin(), tolower); -} - int StrToInt(const std::string &str) { return atoi(str.c_str()); diff --git a/src/conv.h b/src/conv.h index 4f56b4e6..afdded72 100644 --- a/src/conv.h +++ b/src/conv.h @@ -37,8 +37,6 @@ template void Replace(std::string &s, const char (&from)[N], const ch s.replace(i, N-1, to); } -void ToLower(std::string &); - int StrToInt(const std::string &); long StrToLong(const std::string &); diff --git a/src/info.cpp b/src/info.cpp index 83021165..fc1e66e2 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -207,7 +207,7 @@ void Info::GetArtist() } input.close(); w->SetFormatting(fmtBold, U("\n\nSimilar artists:\n"), fmtBoldEnd, 0); - w->SetFormatting(Config.color2, U("\n * "), clEnd); + w->SetFormatting(Config.color2, U("\n * "), clEnd, 1); // below is used so format won't be removed using RemoveFormatting() by accident. w->ForgetFormatting(); w->Flush(); diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 350fd940..54263dcd 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -1812,7 +1812,7 @@ int main(int argc, char *argv[]) ShowMessage("Searching..."); Screen *s = static_cast *>(myScreen); s->Main()->RemoveFormatting(); - ShowMessage("%s", findme.empty() || s->Main()->SetFormatting(fmtReverse, TO_WSTRING(findme), fmtReverseEnd) ? "Done!" : "No matching patterns found"); + ShowMessage("%s", findme.empty() || s->Main()->SetFormatting(fmtReverse, TO_WSTRING(findme), fmtReverseEnd, 0) ? "Done!" : "No matching patterns found"); s->Main()->Flush(); } } diff --git a/src/scrollpad.cpp b/src/scrollpad.cpp index 95267ea6..51980b73 100644 --- a/src/scrollpad.cpp +++ b/src/scrollpad.cpp @@ -33,7 +33,6 @@ Scrollpad::Scrollpad(size_t startx, Border border) : Window(startx, starty, width, height, title, color, border), itsBeginning(0), - itsFoundForEach(0), itsFoundValueBegin(-1), itsFoundValueEnd(-1), itsRealHeight(1) @@ -92,12 +91,13 @@ void Scrollpad::Flush() itsBuffer.SetTemp(0); } -bool Scrollpad::SetFormatting(short val_b, const std::basic_string &s, short val_e, bool for_each) +bool Scrollpad::SetFormatting(short val_b, const std::basic_string &s, short val_e, bool case_sensitive, bool for_each) { - bool result = itsBuffer.SetFormatting(val_b, s, val_e, for_each); + bool result = itsBuffer.SetFormatting(val_b, s, val_e, case_sensitive, for_each); if (result) { itsFoundForEach = for_each; + itsFoundCaseSensitive = case_sensitive; itsFoundValueBegin = val_b; itsFoundValueEnd = val_e; itsFoundPattern = s; @@ -109,7 +109,6 @@ bool Scrollpad::SetFormatting(short val_b, const std::basic_string &s void Scrollpad::ForgetFormatting() { - itsFoundForEach = 0; itsFoundValueBegin = -1; itsFoundValueEnd = -1; itsFoundPattern.clear(); @@ -118,7 +117,7 @@ void Scrollpad::ForgetFormatting() void Scrollpad::RemoveFormatting() { if (itsFoundValueBegin >= 0 && itsFoundValueEnd >= 0) - itsBuffer.RemoveFormatting(itsFoundValueBegin, itsFoundPattern, itsFoundValueEnd, itsFoundForEach); + itsBuffer.RemoveFormatting(itsFoundValueBegin, itsFoundPattern, itsFoundValueEnd, itsFoundCaseSensitive, itsFoundForEach); } void Scrollpad::Refresh() diff --git a/src/scrollpad.h b/src/scrollpad.h index 357f3f2e..aff9622f 100644 --- a/src/scrollpad.h +++ b/src/scrollpad.h @@ -60,13 +60,14 @@ namespace NCurses /// @param val_b flag set at the beginning of found occurence of string /// @param s string that function seaches for /// @param val_e flag set at the end of found occurence of string + /// @param case_sensitive indicates whether algorithm should care about case sensitivity /// @param for_each indicates whether function searches through whole text and sets /// given format for all occurences of given string or stops after first occurence /// @return true if at least one occurence of the string was found, false otherwise /// @see basic_buffer::SetFormatting() /// bool SetFormatting(short val_b, const std::basic_string &s, - short val_e, bool for_each = 1); + short val_e, bool case_sensitive, bool for_each = 1); /// Removes all format flags and colors from stored text /// @@ -130,6 +131,7 @@ namespace NCurses int itsBeginning; bool itsFoundForEach; + bool itsFoundCaseSensitive; short itsFoundValueBegin; short itsFoundValueEnd; std::basic_string itsFoundPattern; diff --git a/src/strbuffer.h b/src/strbuffer.h index 9058bee2..f3b12d85 100644 --- a/src/strbuffer.h +++ b/src/strbuffer.h @@ -21,6 +21,7 @@ #ifndef _STRBUFFER_H #define _STRBUFFER_H +#include "tolower.h" #include "window.h" #include @@ -85,23 +86,25 @@ namespace NCurses /// @param val_b flag set at the beginning of found occurence of string /// @param s string that function seaches for /// @param val_e flag set at the end of found occurence of string + /// @param case_sensitive indicates whether algorithm should care about case sensitivity /// @param for_each indicates whether function searches through whole buffer and sets /// the format for all occurences of given string or stops after the first one /// @return true if at least one occurence of the string was found, false otherwise /// - bool SetFormatting(short val_b, const std::basic_string &s, - short val_e, bool for_each = 1); + bool SetFormatting(short val_b, std::basic_string s, short val_e, + bool case_sensitive, bool for_each = 1); /// Searches for given string in buffer and removes given /// format/color from the beginning and end of its occurence /// @param val_b flag to be removed from the beginning of the string /// @param s string that function seaches for /// @param val_e flag to be removed from the end of the string + /// @param case_sensitive indicates whether algorithm should care about case sensitivity /// @param for_each indicates whether function searches through whole buffer and removes /// given format from all occurences of given string or stops after the first one /// - void RemoveFormatting(short val_b, const std::basic_string &s, - short val_e, bool for_each = 1); + void RemoveFormatting(short val_b, std::basic_string pattern, short val_e, + bool case_sensitive, bool for_each = 1); /// Sets the pointer to string, that will be passed in operator<<() to window /// object instead of the internal buffer. This is useful if you took the content @@ -187,8 +190,9 @@ template std::basic_string NCurses::basic_buffer::Str() const } template bool NCurses::basic_buffer::SetFormatting( short val_b, - const std::basic_string &s, + std::basic_string s, short val_e, + bool case_sensitive, bool for_each ) { @@ -196,8 +200,12 @@ template bool NCurses::basic_buffer::SetFormatting( short val_b, return false; bool result = false; std::basic_string base = itsString.str(); + if (!case_sensitive) + { + ToLower(s); + ToLower(base); + } FormatPos fp; - for (size_t i = base.find(s); i != std::basic_string::npos; i = base.find(s, i)) { result = true; @@ -216,22 +224,27 @@ template bool NCurses::basic_buffer::SetFormatting( short val_b, } template void NCurses::basic_buffer::RemoveFormatting( short val_b, - const std::basic_string &s, + std::basic_string pattern, short val_e, + bool case_sensitive, bool for_each ) { - if (s.empty()) + if (pattern.empty()) return; std::basic_string base = itsString.str(); + if (!case_sensitive) + { + ToLower(pattern); + ToLower(base); + } FormatPos fp; - - for (size_t i = base.find(s); i != std::basic_string::npos; i = base.find(s, i)) + for (size_t i = base.find(pattern); i != std::basic_string::npos; i = base.find(pattern, i)) { fp.Value = val_b; fp.Position = i; itsFormat.remove(fp); - i += s.length(); + i += pattern.length(); fp.Value = val_e; fp.Position = i; itsFormat.remove(fp);