OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/history/query_parser.h" | 5 #include "chrome/browser/history/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" |
11 #include "base/i18n/case_conversion.h" | 11 #include "base/i18n/case_conversion.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Returns true if |mp1.first| is less than |mp2.first|. This is used to | 17 // Returns true if |mp1.first| is less than |mp2.first|. This is used to |
18 // sort match positions. | 18 // sort match positions. |
19 int CompareMatchPosition(const Snippet::MatchPosition& mp1, | 19 int CompareMatchPosition(const Snippet::MatchPosition& mp1, |
20 const Snippet::MatchPosition& mp2) { | 20 const Snippet::MatchPosition& mp2) { |
21 return mp1.first < mp2.first; | 21 return mp1.first < mp2.first; |
22 } | 22 } |
23 | 23 |
24 // Returns true if |mp2| intersects |mp1|. This is intended for use by | 24 // Returns true if |mp2| intersects |mp1|. This is intended for use by |
25 // CoalesceMatchesFrom and isn't meant as a general intersectpion comparison | 25 // CoalesceMatchesFrom and isn't meant as a general intersection comparison |
26 // function. | 26 // function. |
27 bool SnippetIntersects(const Snippet::MatchPosition& mp1, | 27 bool SnippetIntersects(const Snippet::MatchPosition& mp1, |
28 const Snippet::MatchPosition& mp2) { | 28 const Snippet::MatchPosition& mp2) { |
29 return mp2.first >= mp1.first && mp2.first <= mp1.second; | 29 return mp2.first >= mp1.first && mp2.first <= mp1.second; |
30 } | 30 } |
31 | 31 |
32 // Coalesces match positions in |matches| after index that intersect the match | 32 // Coalesces match positions in |matches| after index that intersect the match |
33 // position at |index|. | 33 // position at |index|. |
34 void CoalesceMatchesFrom(size_t index, Snippet::MatchPositions* matches) { | 34 void CoalesceMatchesFrom(size_t index, Snippet::MatchPositions* matches) { |
35 Snippet::MatchPosition& mp = (*matches)[index]; | 35 Snippet::MatchPosition& mp = (*matches)[index]; |
36 for (Snippet::MatchPositions::iterator i = matches->begin() + index + 1; | 36 for (Snippet::MatchPositions::iterator i = matches->begin() + index + 1; |
37 i != matches->end(); ) { | 37 i != matches->end(); ) { |
38 if (SnippetIntersects(mp, *i)) { | 38 if (SnippetIntersects(mp, *i)) { |
39 mp.second = i->second; | 39 mp.second = std::max(mp.second, i->second); |
40 i = matches->erase(i); | 40 i = matches->erase(i); |
41 } else { | 41 } else { |
42 return; | 42 return; |
43 } | 43 } |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 // Sorts the match positions in |matches| by their first index, then coalesces | 47 // Sorts the match positions in |matches| by their first index, then coalesces |
48 // any match positions that intersect each other. | 48 // any match positions that intersect each other. |
49 void CoalseAndSortMatchPositions(Snippet::MatchPositions* matches) { | 49 void CoalseAndSortMatchPositions(Snippet::MatchPositions* matches) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 bool QueryNodeWord::Matches(const string16& word, bool exact) const { | 119 bool QueryNodeWord::Matches(const string16& word, bool exact) const { |
120 if (exact || !QueryParser::IsWordLongEnoughForPrefixSearch(word_)) | 120 if (exact || !QueryParser::IsWordLongEnoughForPrefixSearch(word_)) |
121 return word == word_; | 121 return word == word_; |
122 return word.size() >= word_.size() && | 122 return word.size() >= word_.size() && |
123 (word_.compare(0, word_.size(), word, 0, word_.size()) == 0); | 123 (word_.compare(0, word_.size(), word, 0, word_.size()) == 0); |
124 } | 124 } |
125 | 125 |
126 bool QueryNodeWord::HasMatchIn(const std::vector<QueryWord>& words, | 126 bool QueryNodeWord::HasMatchIn(const std::vector<QueryWord>& words, |
127 Snippet::MatchPositions* match_positions) const { | 127 Snippet::MatchPositions* match_positions) const { |
| 128 bool matched = false; |
128 for (size_t i = 0; i < words.size(); ++i) { | 129 for (size_t i = 0; i < words.size(); ++i) { |
129 if (Matches(words[i].word, false)) { | 130 if (Matches(words[i].word, false)) { |
130 size_t match_start = words[i].position; | 131 size_t match_start = words[i].position; |
131 match_positions->push_back( | 132 match_positions->push_back( |
132 Snippet::MatchPosition(match_start, | 133 Snippet::MatchPosition(match_start, |
133 match_start + static_cast<int>(word_.size()))); | 134 match_start + static_cast<int>(word_.size()))); |
134 return true; | 135 matched = true; |
135 } | 136 } |
136 } | 137 } |
137 return false; | 138 return matched; |
138 } | 139 } |
139 | 140 |
140 void QueryNodeWord::AppendWords(std::vector<string16>* words) const { | 141 void QueryNodeWord::AppendWords(std::vector<string16>* words) const { |
141 words->push_back(word_); | 142 words->push_back(word_); |
142 } | 143 } |
143 | 144 |
144 // 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. |
145 class QueryNodeList : public QueryNode { | 146 class QueryNodeList : public QueryNode { |
146 public: | 147 public: |
147 typedef std::vector<QueryNode*> QueryNodeVector; | 148 typedef std::vector<QueryNode*> QueryNodeVector; |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 if (iter.IsWord()) { | 408 if (iter.IsWord()) { |
408 string16 word = iter.GetString(); | 409 string16 word = iter.GetString(); |
409 if (!word.empty()) { | 410 if (!word.empty()) { |
410 words->push_back(QueryWord()); | 411 words->push_back(QueryWord()); |
411 words->back().word = word; | 412 words->back().word = word; |
412 words->back().position = iter.prev(); | 413 words->back().position = iter.prev(); |
413 } | 414 } |
414 } | 415 } |
415 } | 416 } |
416 } | 417 } |
OLD | NEW |