OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bookmarks/bookmark_index.h" | 5 #include "chrome/browser/bookmarks/bookmark_index.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <list> | 9 #include <list> |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 void BookmarkIndex::Remove(const BookmarkNode* node) { | 69 void BookmarkIndex::Remove(const BookmarkNode* node) { |
70 if (!node->is_url()) | 70 if (!node->is_url()) |
71 return; | 71 return; |
72 | 72 |
73 std::vector<string16> terms = ExtractQueryWords(node->GetTitle()); | 73 std::vector<string16> terms = ExtractQueryWords(node->GetTitle()); |
74 for (size_t i = 0; i < terms.size(); ++i) | 74 for (size_t i = 0; i < terms.size(); ++i) |
75 UnregisterNode(terms[i], node); | 75 UnregisterNode(terms[i], node); |
76 } | 76 } |
77 | 77 |
78 void BookmarkIndex::GetBookmarksWithTitlesMatching( | 78 void BookmarkIndex::GetBookmarksWithTitlesMatching( |
79 const string16& query, | 79 const base::string16& query, |
80 size_t max_count, | 80 size_t max_count, |
81 std::vector<BookmarkTitleMatch>* results) { | 81 std::vector<BookmarkTitleMatch>* results) { |
82 std::vector<string16> terms = ExtractQueryWords(query); | 82 std::vector<string16> terms = ExtractQueryWords(query); |
83 if (terms.empty()) | 83 if (terms.empty()) |
84 return; | 84 return; |
85 | 85 |
86 Matches matches; | 86 Matches matches; |
87 for (size_t i = 0; i < terms.size(); ++i) { | 87 for (size_t i = 0; i < terms.size(); ++i) { |
88 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) | 88 if (!GetBookmarksWithTitleMatchingTerm(terms[i], i == 0, &matches)) |
89 return; | 89 return; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // of QueryParser may filter it out. For example, the query | 156 // of QueryParser may filter it out. For example, the query |
157 // ["thi"] will match the bookmark titled [Thinking], but since | 157 // ["thi"] will match the bookmark titled [Thinking], but since |
158 // ["thi"] is quoted we don't want to do a prefix match. | 158 // ["thi"] is quoted we don't want to do a prefix match. |
159 if (parser->DoesQueryMatch(node->GetTitle(), query_nodes, | 159 if (parser->DoesQueryMatch(node->GetTitle(), query_nodes, |
160 &(title_match.match_positions))) { | 160 &(title_match.match_positions))) { |
161 title_match.node = node; | 161 title_match.node = node; |
162 results->push_back(title_match); | 162 results->push_back(title_match); |
163 } | 163 } |
164 } | 164 } |
165 | 165 |
166 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const string16& term, | 166 bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const base::string16& term
, |
167 bool first_term, | 167 bool first_term, |
168 Matches* matches) { | 168 Matches* matches) { |
169 Index::const_iterator i = index_.lower_bound(term); | 169 Index::const_iterator i = index_.lower_bound(term); |
170 if (i == index_.end()) | 170 if (i == index_.end()) |
171 return false; | 171 return false; |
172 | 172 |
173 if (!QueryParser::IsWordLongEnoughForPrefixSearch(term)) { | 173 if (!QueryParser::IsWordLongEnoughForPrefixSearch(term)) { |
174 // Term is too short for prefix match, compare using exact match. | 174 // Term is too short for prefix match, compare using exact match. |
175 if (i->first != term) | 175 if (i->first != term) |
176 return false; // No bookmarks with this term. | 176 return false; // No bookmarks with this term. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 if (!intersection.empty()) { | 238 if (!intersection.empty()) { |
239 result->push_back(Match()); | 239 result->push_back(Match()); |
240 Match& combined_match = result->back(); | 240 Match& combined_match = result->back(); |
241 combined_match.terms = match.terms; | 241 combined_match.terms = match.terms; |
242 combined_match.terms.push_back(index_i); | 242 combined_match.terms.push_back(index_i); |
243 combined_match.nodes.swap(intersection); | 243 combined_match.nodes.swap(intersection); |
244 } | 244 } |
245 } | 245 } |
246 } | 246 } |
247 | 247 |
248 std::vector<string16> BookmarkIndex::ExtractQueryWords(const string16& query) { | 248 std::vector<string16> BookmarkIndex::ExtractQueryWords(const base::string16& que
ry) { |
249 std::vector<string16> terms; | 249 std::vector<string16> terms; |
250 if (query.empty()) | 250 if (query.empty()) |
251 return std::vector<string16>(); | 251 return std::vector<string16>(); |
252 QueryParser parser; | 252 QueryParser parser; |
253 // TODO(brettw): use ICU normalization: | 253 // TODO(brettw): use ICU normalization: |
254 // http://userguide.icu-project.org/transforms/normalization | 254 // http://userguide.icu-project.org/transforms/normalization |
255 parser.ParseQueryWords(base::i18n::ToLower(query), &terms); | 255 parser.ParseQueryWords(base::i18n::ToLower(query), &terms); |
256 return terms; | 256 return terms; |
257 } | 257 } |
258 | 258 |
259 void BookmarkIndex::RegisterNode(const string16& term, | 259 void BookmarkIndex::RegisterNode(const base::string16& term, |
260 const BookmarkNode* node) { | 260 const BookmarkNode* node) { |
261 index_[term].insert(node); | 261 index_[term].insert(node); |
262 } | 262 } |
263 | 263 |
264 void BookmarkIndex::UnregisterNode(const string16& term, | 264 void BookmarkIndex::UnregisterNode(const base::string16& term, |
265 const BookmarkNode* node) { | 265 const BookmarkNode* node) { |
266 Index::iterator i = index_.find(term); | 266 Index::iterator i = index_.find(term); |
267 if (i == index_.end()) { | 267 if (i == index_.end()) { |
268 // We can get here if the node has the same term more than once. For | 268 // We can get here if the node has the same term more than once. For |
269 // example, a bookmark with the title 'foo foo' would end up here. | 269 // example, a bookmark with the title 'foo foo' would end up here. |
270 return; | 270 return; |
271 } | 271 } |
272 i->second.erase(node); | 272 i->second.erase(node); |
273 if (i->second.empty()) | 273 if (i->second.empty()) |
274 index_.erase(i); | 274 index_.erase(i); |
275 } | 275 } |
OLD | NEW |