From 6c307cceb07c561d94de49a66e0d8729dd129dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Cadilhac?= Date: Sun, 12 Apr 2015 17:24:29 +0200 Subject: [PATCH] search engine: keep the regex compilation out of the loop --- src/search_engine.cpp | 154 ++++++++++++------------------------------ 1 file changed, 45 insertions(+), 109 deletions(-) diff --git a/src/search_engine.cpp b/src/search_engine.cpp index 2822aee8..cf237360 100644 --- a/src/search_engine.cpp +++ b/src/search_engine.cpp @@ -419,124 +419,60 @@ void SearchEngine::Search() bool any_found = 1; bool found = 1; - - LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the); - for (auto it = list.begin(); it != list.end(); ++it) + boost::regex rx[11]; + if (SearchMode != &SearchModes[2]) // match to pattern { - if (SearchMode != &SearchModes[2]) // match to pattern + for (int i = 0; i < 11; ++i) { - boost::regex rx; - if (!itsConstraints[0].empty()) - { - try - { - rx.assign(itsConstraints[0], Config.regex_type); - any_found = - boost::regex_search(it->getArtist(), rx) - || boost::regex_search(it->getAlbumArtist(), rx) - || boost::regex_search(it->getTitle(), rx) - || boost::regex_search(it->getAlbum(), rx) - || boost::regex_search(it->getName(), rx) - || boost::regex_search(it->getComposer(), rx) - || boost::regex_search(it->getPerformer(), rx) - || boost::regex_search(it->getGenre(), rx) - || boost::regex_search(it->getDate(), rx) - || boost::regex_search(it->getComment(), rx); - } - catch (boost::bad_expression &) { } - } - - if (found && !itsConstraints[1].empty()) - { - try - { - rx.assign(itsConstraints[1], Config.regex_type); - found = boost::regex_search(it->getArtist(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[2].empty()) - { - try - { - rx.assign(itsConstraints[2], Config.regex_type); - found = boost::regex_search(it->getAlbumArtist(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[3].empty()) - { - try - { - rx.assign(itsConstraints[3], Config.regex_type); - found = boost::regex_search(it->getTitle(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[4].empty()) - { - try - { - rx.assign(itsConstraints[4], Config.regex_type); - found = boost::regex_search(it->getAlbum(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[5].empty()) - { - try - { - rx.assign(itsConstraints[5], Config.regex_type); - found = boost::regex_search(it->getName(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[6].empty()) - { - try - { - rx.assign(itsConstraints[6], Config.regex_type); - found = boost::regex_search(it->getComposer(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[7].empty()) - { - try - { - rx.assign(itsConstraints[7], Config.regex_type); - found = boost::regex_search(it->getPerformer(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && itsConstraints[8].empty()) - { - try - { - rx.assign(itsConstraints[8], Config.regex_type); - found = boost::regex_search(it->getGenre(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[9].empty()) + if (!itsConstraints[i].empty()) { try { - rx.assign(itsConstraints[9], Config.regex_type); - found = boost::regex_search(it->getDate(), rx); - } - catch (boost::bad_expression &) { } - } - if (found && !itsConstraints[10].empty()) - { - try - { - rx.assign(itsConstraints[10], Config.regex_type); - found = boost::regex_search(it->getComment(), rx); + rx[i].assign(itsConstraints[i], Config.regex_type); } catch (boost::bad_expression &) { } } } + } + + LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the); + for (auto it = list.begin(); it != list.end(); ++it) + { + if (SearchMode != &SearchModes[2]) // match to pattern + { + if (!rx[0].empty()) + any_found = + boost::regex_search(it->getArtist(), rx[0]) + || boost::regex_search(it->getAlbumArtist(), rx[0]) + || boost::regex_search(it->getTitle(), rx[0]) + || boost::regex_search(it->getAlbum(), rx[0]) + || boost::regex_search(it->getName(), rx[0]) + || boost::regex_search(it->getComposer(), rx[0]) + || boost::regex_search(it->getPerformer(), rx[0]) + || boost::regex_search(it->getGenre(), rx[0]) + || boost::regex_search(it->getDate(), rx[0]) + || boost::regex_search(it->getComment(), rx[0]); + if (found && !rx[1].empty()) + found = boost::regex_search(it->getArtist(), rx[1]); + if (found && !rx[2].empty()) + found = boost::regex_search(it->getAlbumArtist(), rx[2]); + if (found && !rx[3].empty()) + found = boost::regex_search(it->getTitle(), rx[3]); + if (found && !rx[4].empty()) + found = boost::regex_search(it->getAlbum(), rx[4]); + if (found && !rx[5].empty()) + found = boost::regex_search(it->getName(), rx[5]); + if (found && !rx[6].empty()) + found = boost::regex_search(it->getComposer(), rx[6]); + if (found && !rx[7].empty()) + found = boost::regex_search(it->getPerformer(), rx[7]); + if (found && !rx[8].empty()) + found = boost::regex_search(it->getGenre(), rx[8]); + if (found && !rx[9].empty()) + found = boost::regex_search(it->getDate(), rx[9]); + if (found && !rx[10].empty()) + found = boost::regex_search(it->getComment(), rx[10]); + } else // match only if values are equal { if (!itsConstraints[0].empty())