Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_match.cc

Issue 10911188: autocomplete: Add AutocompleteMatch::MergeClassifications() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: if one of the vectors is non-empty, return it Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/autocomplete/autocomplete_match.h" 5 #include "chrome/browser/autocomplete/autocomplete_match.h"
6 6
7 #include "base/i18n/time_formatting.h" 7 #include "base/i18n/time_formatting.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/string16.h" 11 #include "base/string16.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/time.h" 13 #include "base/time.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/autocomplete/autocomplete_provider.h" 15 #include "chrome/browser/autocomplete/autocomplete_provider.h"
16 #include "chrome/browser/search_engines/template_url.h" 16 #include "chrome/browser/search_engines/template_url.h"
17 #include "chrome/browser/search_engines/template_url_service.h" 17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h" 18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "content/public/common/url_constants.h" 19 #include "content/public/common/url_constants.h"
20 #include "grit/theme_resources.h" 20 #include "grit/theme_resources.h"
21 21
22 namespace {
23
24 bool IsTrivialClassification(const ACMatchClassifications& classifications) {
25 return classifications.empty() ||
26 ((classifications.size() == 1) &&
27 (classifications.back().style == ACMatchClassification::NONE));
28 }
29
30 } // namespace
31
22 // AutocompleteMatch ---------------------------------------------------------- 32 // AutocompleteMatch ----------------------------------------------------------
23 33
24 // static 34 // static
25 const char16 AutocompleteMatch::kInvalidChars[] = { 35 const char16 AutocompleteMatch::kInvalidChars[] = {
26 '\n', '\r', '\t', 36 '\n', '\r', '\t',
27 0x2028, // Line separator 37 0x2028, // Line separator
28 0x2029, // Paragraph separator 38 0x2029, // Paragraph separator
29 0 39 0
30 }; 40 };
31 41
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); 236 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM));
227 237
228 // Mark post-match portion of string (if any). 238 // Mark post-match portion of string (if any).
229 const size_t after_match(match_location + match_length); 239 const size_t after_match(match_location + match_length);
230 if (after_match < overall_length) { 240 if (after_match < overall_length) {
231 classification->push_back(ACMatchClassification(after_match, style)); 241 classification->push_back(ACMatchClassification(after_match, style));
232 } 242 }
233 } 243 }
234 244
235 // static 245 // static
246 AutocompleteMatch::ACMatchClassifications
247 AutocompleteMatch::MergeClassifications(
248 const ACMatchClassifications& classifications1,
Peter Kasting 2012/09/10 23:34:35 Nit: I would only indent these 4 and not 8? I ser
Daniel Erat 2012/09/10 23:46:05 Done.
249 const ACMatchClassifications& classifications2) {
250 // We must return the empty vector only if both inputs are truly empty.
Peter Kasting 2012/09/10 23:34:35 Nit: Comment should only be indented 2.
Daniel Erat 2012/09/10 23:46:05 Whoops. :-/ Sorry, done.
251 // The result of merging an empty vector with a single (0, NONE)
252 // classification is the latter one-entry vector.
253 if (IsTrivialClassification(classifications1))
254 return classifications2.empty() ? classifications1 : classifications2;
255 if (IsTrivialClassification(classifications2))
256 return classifications1;
257
258 ACMatchClassifications output;
259 for (ACMatchClassifications::const_iterator i = classifications1.begin(),
260 j = classifications2.begin(); i != classifications1.end();) {
261 AutocompleteMatch::AddLastClassificationIfNecessary(&output,
262 std::max(i->offset, j->offset), i->style | j->style);
263 const size_t next_i_offset = (i + 1) == classifications1.end() ?
264 static_cast<size_t>(-1) : (i + 1)->offset;
265 const size_t next_j_offset = (j + 1) == classifications2.end() ?
266 static_cast<size_t>(-1) : (j + 1)->offset;
267 if (next_i_offset >= next_j_offset)
268 ++j;
269 if (next_j_offset >= next_i_offset)
270 ++i;
271 }
272
273 return output;
274 }
275
276 // static
236 std::string AutocompleteMatch::ClassificationsToString( 277 std::string AutocompleteMatch::ClassificationsToString(
237 const ACMatchClassifications& classifications) { 278 const ACMatchClassifications& classifications) {
238 std::string serialized_classifications; 279 std::string serialized_classifications;
239 for (size_t i = 0; i < classifications.size(); ++i) { 280 for (size_t i = 0; i < classifications.size(); ++i) {
240 if (i) 281 if (i)
241 serialized_classifications += ','; 282 serialized_classifications += ',';
242 serialized_classifications += base::IntToString(classifications[i].offset) + 283 serialized_classifications += base::IntToString(classifications[i].offset) +
243 ',' + base::IntToString(classifications[i].style); 284 ',' + base::IntToString(classifications[i].style);
244 } 285 }
245 return serialized_classifications; 286 return serialized_classifications;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 << " is unsorted in relation to last offset of " << last_offset 443 << " is unsorted in relation to last offset of " << last_offset
403 << ". Provider: " << (provider ? provider->name() : "None") << "."; 444 << ". Provider: " << (provider ? provider->name() : "None") << ".";
404 DCHECK_LT(i->offset, text.length()) 445 DCHECK_LT(i->offset, text.length())
405 << " Classification of [" << i->offset << "," << text.length() 446 << " Classification of [" << i->offset << "," << text.length()
406 << "] is out of bounds for \"" << text << "\". Provider: " 447 << "] is out of bounds for \"" << text << "\". Provider: "
407 << (provider ? provider->name() : "None") << "."; 448 << (provider ? provider->name() : "None") << ".";
408 last_offset = i->offset; 449 last_offset = i->offset;
409 } 450 }
410 } 451 }
411 #endif 452 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698