2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "TextSearch.h"
25 CTextSearch::CTextSearch(const CStdString &strSearchTerms, bool bCaseSensitive /* = false */, TextSearchDefault defaultSearchMode /* = SEARCH_DEFAULT_OR */)
27 m_bCaseSensitive = bCaseSensitive;
28 ExtractSearchTerms(strSearchTerms, defaultSearchMode);
31 CTextSearch::~CTextSearch(void)
38 bool CTextSearch::IsValid(void) const
40 return m_AND.size() > 0 || m_OR.size() > 0 || m_NOT.size() > 0;
43 bool CTextSearch::Search(const CStdString &strHaystack) const
45 if (strHaystack.empty() || !IsValid())
48 CStdString strSearch(strHaystack);
49 if (!m_bCaseSensitive)
50 strSearch = strSearch.ToLower();
52 /* check whether any of the NOT terms matches and return false if there's a match */
53 for (unsigned int iNotPtr = 0; iNotPtr < m_NOT.size(); iNotPtr++)
55 if (strSearch.Find(m_NOT.at(iNotPtr)) != -1)
59 /* check whether at least one of the OR terms matches and return false if there's no match found */
60 bool bFound(m_OR.size() == 0);
61 for (unsigned int iOrPtr = 0; iOrPtr < m_OR.size(); iOrPtr++)
63 if (strSearch.Find(m_OR.at(iOrPtr)) != -1)
72 /* check whether all of the AND terms match and return false if one of them wasn't found */
73 for (unsigned int iAndPtr = 0; iAndPtr < m_AND.size(); iAndPtr++)
75 if (strSearch.Find(m_AND[iAndPtr]) == -1)
79 /* all ok, return true */
83 void CTextSearch::GetAndCutNextTerm(CStdString &strSearchTerm, CStdString &strNextTerm)
85 CStdString strFindNext(" ");
87 if (StringUtils::EndsWith(strSearchTerm, "\""))
89 strSearchTerm.erase(0, 1);
93 int iNextPos = strSearchTerm.Find(strFindNext);
96 strNextTerm = strSearchTerm.Left(iNextPos);
97 strSearchTerm.erase(0, iNextPos + 1);
101 strNextTerm = strSearchTerm;
102 strSearchTerm.clear();
106 void CTextSearch::ExtractSearchTerms(const CStdString &strSearchTerm, TextSearchDefault defaultSearchMode)
108 CStdString strParsedSearchTerm(strSearchTerm);
109 StringUtils::Trim(strParsedSearchTerm);
111 if (!m_bCaseSensitive)
112 strParsedSearchTerm = strParsedSearchTerm.ToLower();
114 bool bNextAND(defaultSearchMode == SEARCH_DEFAULT_AND);
115 bool bNextOR(defaultSearchMode == SEARCH_DEFAULT_OR);
116 bool bNextNOT(defaultSearchMode == SEARCH_DEFAULT_NOT);
118 while (strParsedSearchTerm.length() > 0)
120 StringUtils::TrimLeft(strParsedSearchTerm);
122 if (StringUtils::StartsWith(strParsedSearchTerm, "!") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "not"))
125 GetAndCutNextTerm(strParsedSearchTerm, strDummy);
128 else if (StringUtils::StartsWith(strParsedSearchTerm, "+") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "and"))
131 GetAndCutNextTerm(strParsedSearchTerm, strDummy);
134 else if (StringUtils::StartsWith(strParsedSearchTerm, "|") || StringUtils::StartsWithNoCase(strParsedSearchTerm, "or"))
137 GetAndCutNextTerm(strParsedSearchTerm, strDummy);
143 GetAndCutNextTerm(strParsedSearchTerm, strTerm);
144 if (strTerm.length() > 0)
147 m_AND.push_back(strTerm);
149 m_OR.push_back(strTerm);
151 m_NOT.push_back(strTerm);
158 bNextAND = (defaultSearchMode == SEARCH_DEFAULT_AND);
159 bNextOR = (defaultSearchMode == SEARCH_DEFAULT_OR);
160 bNextNOT = (defaultSearchMode == SEARCH_DEFAULT_NOT);
163 StringUtils::TrimLeft(strParsedSearchTerm);