| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/query_parser/query_parser.h" | 5 #include "components/query_parser/query_parser.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 // Inheritance structure: | 61 // Inheritance structure: |
| 62 // Queries are represented as trees of QueryNodes. | 62 // Queries are represented as trees of QueryNodes. |
| 63 // QueryNodes are either a collection of subnodes (a QueryNodeList) | 63 // QueryNodes are either a collection of subnodes (a QueryNodeList) |
| 64 // or a single word (a QueryNodeWord). | 64 // or a single word (a QueryNodeWord). |
| 65 | 65 |
| 66 // A QueryNodeWord is a single word in the query. | 66 // A QueryNodeWord is a single word in the query. |
| 67 class QueryNodeWord : public QueryNode { | 67 class QueryNodeWord : public QueryNode { |
| 68 public: | 68 public: |
| 69 explicit QueryNodeWord(const base::string16& word); | 69 explicit QueryNodeWord(const base::string16& word); |
| 70 virtual ~QueryNodeWord(); | 70 ~QueryNodeWord() override; |
| 71 | 71 |
| 72 const base::string16& word() const { return word_; } | 72 const base::string16& word() const { return word_; } |
| 73 | 73 |
| 74 void set_literal(bool literal) { literal_ = literal; } | 74 void set_literal(bool literal) { literal_ = literal; } |
| 75 | 75 |
| 76 // QueryNode: | 76 // QueryNode: |
| 77 virtual int AppendToSQLiteQuery(base::string16* query) const override; | 77 int AppendToSQLiteQuery(base::string16* query) const override; |
| 78 virtual bool IsWord() const override; | 78 bool IsWord() const override; |
| 79 virtual bool Matches(const base::string16& word, bool exact) const override; | 79 bool Matches(const base::string16& word, bool exact) const override; |
| 80 virtual bool HasMatchIn( | 80 bool HasMatchIn(const QueryWordVector& words, |
| 81 const QueryWordVector& words, | 81 Snippet::MatchPositions* match_positions) const override; |
| 82 Snippet::MatchPositions* match_positions) const override; | 82 bool HasMatchIn(const QueryWordVector& words) const override; |
| 83 virtual bool HasMatchIn( | 83 void AppendWords(std::vector<base::string16>* words) const override; |
| 84 const QueryWordVector& words) const override; | |
| 85 virtual void AppendWords(std::vector<base::string16>* words) const override; | |
| 86 | 84 |
| 87 private: | 85 private: |
| 88 base::string16 word_; | 86 base::string16 word_; |
| 89 bool literal_; | 87 bool literal_; |
| 90 | 88 |
| 91 DISALLOW_COPY_AND_ASSIGN(QueryNodeWord); | 89 DISALLOW_COPY_AND_ASSIGN(QueryNodeWord); |
| 92 }; | 90 }; |
| 93 | 91 |
| 94 QueryNodeWord::QueryNodeWord(const base::string16& word) | 92 QueryNodeWord::QueryNodeWord(const base::string16& word) |
| 95 : word_(word), | 93 : word_(word), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 } | 139 } |
| 142 | 140 |
| 143 void QueryNodeWord::AppendWords(std::vector<base::string16>* words) const { | 141 void QueryNodeWord::AppendWords(std::vector<base::string16>* words) const { |
| 144 words->push_back(word_); | 142 words->push_back(word_); |
| 145 } | 143 } |
| 146 | 144 |
| 147 // A QueryNodeList has a collection of QueryNodes which are deleted in the end. | 145 // A QueryNodeList has a collection of QueryNodes which are deleted in the end. |
| 148 class QueryNodeList : public QueryNode { | 146 class QueryNodeList : public QueryNode { |
| 149 public: | 147 public: |
| 150 QueryNodeList(); | 148 QueryNodeList(); |
| 151 virtual ~QueryNodeList(); | 149 ~QueryNodeList() override; |
| 152 | 150 |
| 153 QueryNodeStarVector* children() { return &children_; } | 151 QueryNodeStarVector* children() { return &children_; } |
| 154 | 152 |
| 155 void AddChild(QueryNode* node); | 153 void AddChild(QueryNode* node); |
| 156 | 154 |
| 157 // Remove empty subnodes left over from other parsing. | 155 // Remove empty subnodes left over from other parsing. |
| 158 void RemoveEmptySubnodes(); | 156 void RemoveEmptySubnodes(); |
| 159 | 157 |
| 160 // QueryNode: | 158 // QueryNode: |
| 161 virtual int AppendToSQLiteQuery(base::string16* query) const override; | 159 int AppendToSQLiteQuery(base::string16* query) const override; |
| 162 virtual bool IsWord() const override; | 160 bool IsWord() const override; |
| 163 virtual bool Matches(const base::string16& word, bool exact) const override; | 161 bool Matches(const base::string16& word, bool exact) const override; |
| 164 virtual bool HasMatchIn( | 162 bool HasMatchIn(const QueryWordVector& words, |
| 165 const QueryWordVector& words, | 163 Snippet::MatchPositions* match_positions) const override; |
| 166 Snippet::MatchPositions* match_positions) const override; | 164 bool HasMatchIn(const QueryWordVector& words) const override; |
| 167 virtual bool HasMatchIn(const QueryWordVector& words) const override; | 165 void AppendWords(std::vector<base::string16>* words) const override; |
| 168 virtual void AppendWords(std::vector<base::string16>* words) const override; | |
| 169 | 166 |
| 170 protected: | 167 protected: |
| 171 int AppendChildrenToString(base::string16* query) const; | 168 int AppendChildrenToString(base::string16* query) const; |
| 172 | 169 |
| 173 QueryNodeStarVector children_; | 170 QueryNodeStarVector children_; |
| 174 | 171 |
| 175 private: | 172 private: |
| 176 DISALLOW_COPY_AND_ASSIGN(QueryNodeList); | 173 DISALLOW_COPY_AND_ASSIGN(QueryNodeList); |
| 177 }; | 174 }; |
| 178 | 175 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 query->push_back(L' '); | 235 query->push_back(L' '); |
| 239 num_words += (*node)->AppendToSQLiteQuery(query); | 236 num_words += (*node)->AppendToSQLiteQuery(query); |
| 240 } | 237 } |
| 241 return num_words; | 238 return num_words; |
| 242 } | 239 } |
| 243 | 240 |
| 244 // A QueryNodePhrase is a phrase query ("quoted"). | 241 // A QueryNodePhrase is a phrase query ("quoted"). |
| 245 class QueryNodePhrase : public QueryNodeList { | 242 class QueryNodePhrase : public QueryNodeList { |
| 246 public: | 243 public: |
| 247 QueryNodePhrase(); | 244 QueryNodePhrase(); |
| 248 virtual ~QueryNodePhrase(); | 245 ~QueryNodePhrase() override; |
| 249 | 246 |
| 250 // QueryNodeList: | 247 // QueryNodeList: |
| 251 virtual int AppendToSQLiteQuery(base::string16* query) const override; | 248 int AppendToSQLiteQuery(base::string16* query) const override; |
| 252 virtual bool HasMatchIn( | 249 bool HasMatchIn(const QueryWordVector& words, |
| 253 const QueryWordVector& words, | 250 Snippet::MatchPositions* match_positions) const override; |
| 254 Snippet::MatchPositions* match_positions) const override; | 251 bool HasMatchIn(const QueryWordVector& words) const override; |
| 255 virtual bool HasMatchIn(const QueryWordVector& words) const override; | |
| 256 | 252 |
| 257 private: | 253 private: |
| 258 bool MatchesAll(const QueryWordVector& words, | 254 bool MatchesAll(const QueryWordVector& words, |
| 259 const QueryWord** first_word, | 255 const QueryWord** first_word, |
| 260 const QueryWord** last_word) const; | 256 const QueryWord** last_word) const; |
| 261 DISALLOW_COPY_AND_ASSIGN(QueryNodePhrase); | 257 DISALLOW_COPY_AND_ASSIGN(QueryNodePhrase); |
| 262 }; | 258 }; |
| 263 | 259 |
| 264 QueryNodePhrase::QueryNodePhrase() {} | 260 QueryNodePhrase::QueryNodePhrase() {} |
| 265 | 261 |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 void QueryParser::SortAndCoalesceMatchPositions( | 459 void QueryParser::SortAndCoalesceMatchPositions( |
| 464 Snippet::MatchPositions* matches) { | 460 Snippet::MatchPositions* matches) { |
| 465 std::sort(matches->begin(), matches->end(), &CompareMatchPosition); | 461 std::sort(matches->begin(), matches->end(), &CompareMatchPosition); |
| 466 // WARNING: we don't use iterator here as CoalesceMatchesFrom may remove | 462 // WARNING: we don't use iterator here as CoalesceMatchesFrom may remove |
| 467 // from matches. | 463 // from matches. |
| 468 for (size_t i = 0; i < matches->size(); ++i) | 464 for (size_t i = 0; i < matches->size(); ++i) |
| 469 CoalesceMatchesFrom(i, matches); | 465 CoalesceMatchesFrom(i, matches); |
| 470 } | 466 } |
| 471 | 467 |
| 472 } // namespace query_parser | 468 } // namespace query_parser |
| OLD | NEW |