// A QueryNodeWord is a single word in the query.
class QueryNodeWord : public QueryNode {
public:
- explicit QueryNodeWord(const base::string16& word);
- virtual ~QueryNodeWord();
+ explicit QueryNodeWord(const base::string16& word,
+ 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:
- virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
- virtual bool IsWord() const OVERRIDE;
- virtual bool Matches(const base::string16& word, bool exact) const OVERRIDE;
- virtual bool HasMatchIn(
- const QueryWordVector& words,
- Snippet::MatchPositions* match_positions) const OVERRIDE;
- virtual bool HasMatchIn(
- const QueryWordVector& words) const OVERRIDE;
- virtual void AppendWords(std::vector<base::string16>* words) const OVERRIDE;
+ int AppendToSQLiteQuery(base::string16* query) const override;
+ bool IsWord() const override;
+ bool Matches(const base::string16& word, bool exact) const override;
+ bool HasMatchIn(const QueryWordVector& words,
+ Snippet::MatchPositions* match_positions) const override;
+ bool HasMatchIn(const QueryWordVector& words) const override;
+ void AppendWords(std::vector<base::string16>* words) const override;
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,
+ MatchingAlgorithm matching_algorithm)
: word_(word),
- literal_(false) {}
+ literal_(false),
+ matching_algorithm_(matching_algorithm) {}
QueryNodeWord::~QueryNodeWord() {}
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;
}
}
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);
class QueryNodeList : public QueryNode {
public:
QueryNodeList();
- virtual ~QueryNodeList();
+ ~QueryNodeList() override;
QueryNodeStarVector* children() { return &children_; }
void RemoveEmptySubnodes();
// QueryNode:
- virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
- virtual bool IsWord() const OVERRIDE;
- virtual bool Matches(const base::string16& word, bool exact) const OVERRIDE;
- virtual bool HasMatchIn(
- const QueryWordVector& words,
- Snippet::MatchPositions* match_positions) const OVERRIDE;
- virtual bool HasMatchIn(const QueryWordVector& words) const OVERRIDE;
- virtual void AppendWords(std::vector<base::string16>* words) const OVERRIDE;
+ int AppendToSQLiteQuery(base::string16* query) const override;
+ bool IsWord() const override;
+ bool Matches(const base::string16& word, bool exact) const override;
+ bool HasMatchIn(const QueryWordVector& words,
+ Snippet::MatchPositions* match_positions) const override;
+ bool HasMatchIn(const QueryWordVector& words) const override;
+ void AppendWords(std::vector<base::string16>* words) const override;
protected:
int AppendChildrenToString(base::string16* query) const;
class QueryNodePhrase : public QueryNodeList {
public:
QueryNodePhrase();
- virtual ~QueryNodePhrase();
+ ~QueryNodePhrase() override;
// QueryNodeList:
- virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
- virtual bool HasMatchIn(
- const QueryWordVector& words,
- Snippet::MatchPositions* match_positions) const OVERRIDE;
- virtual bool HasMatchIn(const QueryWordVector& words) const OVERRIDE;
+ int AppendToSQLiteQuery(base::string16* query) const override;
+ bool HasMatchIn(const QueryWordVector& words,
+ Snippet::MatchPositions* match_positions) const override;
+ bool HasMatchIn(const QueryWordVector& words) const override;
private:
bool MatchesAll(const QueryWordVector& words,
QueryParser::QueryParser() {}
// static
-bool QueryParser::IsWordLongEnoughForPrefixSearch(const base::string16& word) {
+bool QueryParser::IsWordLongEnoughForPrefixSearch(
+ const base::string16& word, 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)
}
int QueryParser::ParseQuery(const base::string16& query,
+ 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,
+ 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,
+ 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());
}
}
bool QueryParser::ParseQueryImpl(const base::string16& query,
+ MatchingAlgorithm matching_algorithm,
QueryNodeList* root) {
base::i18n::BreakIterator iter(query, base::i18n::BreakIterator::BREAK_WORD);
// TODO(evanm): support a locale here
// 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);