Index: components/query_parser/query_parser.cc |
diff --git a/components/query_parser/query_parser.cc b/components/query_parser/query_parser.cc |
index 96c73c2de5e9f719366cc98952b669c350ce54b5..c03ee0bfbf2cb4529b4111bdae28d6753df6d112 100644 |
--- a/components/query_parser/query_parser.cc |
+++ b/components/query_parser/query_parser.cc |
@@ -66,11 +66,13 @@ bool IsQueryQuote(wchar_t ch) { |
// A QueryNodeWord is a single word in the query. |
class QueryNodeWord : public QueryNode { |
public: |
- explicit QueryNodeWord(const base::string16& word); |
+ explicit QueryNodeWord(const base::string16& word, |
+ const MatchingAlgorithm matching_algorithm); |
~QueryNodeWord() override; |
const base::string16& word() const { return word_; } |
+ bool literal() const { return literal_; }; |
void set_literal(bool literal) { literal_ = literal; } |
// QueryNode: |
@@ -85,13 +87,16 @@ class QueryNodeWord : public QueryNode { |
private: |
base::string16 word_; |
bool literal_; |
+ const MatchingAlgorithm matching_algorithm_; |
DISALLOW_COPY_AND_ASSIGN(QueryNodeWord); |
}; |
-QueryNodeWord::QueryNodeWord(const base::string16& word) |
+QueryNodeWord::QueryNodeWord(const base::string16& word, |
+ const MatchingAlgorithm matching_algorithm) |
: word_(word), |
- literal_(false) {} |
+ literal_(false), |
+ matching_algorithm_(matching_algorithm) {} |
QueryNodeWord::~QueryNodeWord() {} |
@@ -99,7 +104,8 @@ int QueryNodeWord::AppendToSQLiteQuery(base::string16* query) const { |
query->append(word_); |
// Use prefix search if we're not literal and long enough. |
- if (!literal_ && QueryParser::IsWordLongEnoughForPrefixSearch(word_)) |
+ if (!literal_ && |
+ QueryParser::IsWordLongEnoughForPrefixSearch(word_, matching_algorithm_)) |
*query += L'*'; |
return 1; |
} |
@@ -109,7 +115,8 @@ bool QueryNodeWord::IsWord() const { |
} |
bool QueryNodeWord::Matches(const base::string16& word, bool exact) const { |
- if (exact || !QueryParser::IsWordLongEnoughForPrefixSearch(word_)) |
+ if (exact || |
+ !QueryParser::IsWordLongEnoughForPrefixSearch(word_, matching_algorithm_)) |
return word == word_; |
return word.size() >= word_.size() && |
(word_.compare(0, word_.size(), word, 0, word_.size()) == 0); |
@@ -315,7 +322,11 @@ bool QueryNodePhrase::HasMatchIn(const QueryWordVector& words) const { |
QueryParser::QueryParser() {} |
// static |
-bool QueryParser::IsWordLongEnoughForPrefixSearch(const base::string16& word) { |
+bool QueryParser::IsWordLongEnoughForPrefixSearch( |
+ const base::string16& word, const MatchingAlgorithm matching_algorithm) { |
+ if (matching_algorithm == MatchingAlgorithm::ALWAYS_PREFIX_SEARCH) |
+ return true; |
+ |
DCHECK(!word.empty()); |
size_t minimum_length = 3; |
// We intentionally exclude Hangul Jamos (both Conjoining and compatibility) |
@@ -327,25 +338,28 @@ bool QueryParser::IsWordLongEnoughForPrefixSearch(const base::string16& word) { |
} |
int QueryParser::ParseQuery(const base::string16& query, |
+ const MatchingAlgorithm matching_algorithm, |
base::string16* sqlite_query) { |
QueryNodeList root; |
- if (!ParseQueryImpl(query, &root)) |
+ if (!ParseQueryImpl(query, matching_algorithm, &root)) |
return 0; |
return root.AppendToSQLiteQuery(sqlite_query); |
} |
void QueryParser::ParseQueryWords(const base::string16& query, |
+ const MatchingAlgorithm matching_algorithm, |
std::vector<base::string16>* words) { |
QueryNodeList root; |
- if (!ParseQueryImpl(query, &root)) |
+ if (!ParseQueryImpl(query, matching_algorithm, &root)) |
return; |
root.AppendWords(words); |
} |
void QueryParser::ParseQueryNodes(const base::string16& query, |
+ const MatchingAlgorithm matching_algorithm, |
QueryNodeStarVector* nodes) { |
QueryNodeList root; |
- if (ParseQueryImpl(base::i18n::ToLower(query), &root)) |
+ if (ParseQueryImpl(base::i18n::ToLower(query), matching_algorithm, &root)) |
nodes->swap(*root.children()); |
} |
@@ -393,6 +407,7 @@ bool QueryParser::DoesQueryMatch(const QueryWordVector& query_words, |
} |
bool QueryParser::ParseQueryImpl(const base::string16& query, |
+ const MatchingAlgorithm matching_algorithm, |
QueryNodeList* root) { |
base::i18n::BreakIterator iter(query, base::i18n::BreakIterator::BREAK_WORD); |
// TODO(evanm): support a locale here |
@@ -410,7 +425,8 @@ bool QueryParser::ParseQueryImpl(const base::string16& query, |
// is not necessarily a word, but could also be a sequence of punctuation |
// or whitespace. |
if (iter.IsWord()) { |
- QueryNodeWord* word_node = new QueryNodeWord(iter.GetString()); |
+ QueryNodeWord* word_node = new QueryNodeWord(iter.GetString(), |
+ matching_algorithm); |
if (in_quotes) |
word_node->set_literal(true); |
query_stack.back()->AddChild(word_node); |