Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "chrome/browser/autocomplete/autocomplete_match.h" | 6 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 7 #include "grit/theme_resources.h" | 7 #include "grit/theme_resources.h" |
| 8 | 8 |
| 9 // AutocompleteMatch ---------------------------------------------------------- | 9 // AutocompleteMatch ---------------------------------------------------------- |
| 10 | 10 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 | 87 |
| 88 return elem1.relevance > elem2.relevance; | 88 return elem1.relevance > elem2.relevance; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // static | 91 // static |
| 92 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 92 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
| 93 const AutocompleteMatch& elem2) { | 93 const AutocompleteMatch& elem2) { |
| 94 // Sort identical destination_urls together. Place the most relevant matches | 94 // Sort identical destination_urls together. Place the most relevant matches |
| 95 // first, so that when we call std::unique(), these are the ones that get | 95 // first, so that when we call std::unique(), these are the ones that get |
| 96 // preserved. | 96 // preserved. |
| 97 return (elem1.destination_url != elem2.destination_url) ? | 97 return (elem1.stripped_destination_url != elem2.stripped_destination_url) ? |
| 98 (elem1.destination_url < elem2.destination_url) : | 98 (elem1.stripped_destination_url < elem2.stripped_destination_url) : |
| 99 MoreRelevant(elem1, elem2); | 99 MoreRelevant(elem1, elem2); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // static | 102 // static |
| 103 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 103 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
| 104 const AutocompleteMatch& elem2) { | 104 const AutocompleteMatch& elem2) { |
| 105 return elem1.destination_url == elem2.destination_url; | 105 return elem1.stripped_destination_url == elem2.stripped_destination_url; |
| 106 } | 106 } |
| 107 | 107 |
| 108 // static | 108 // static |
| 109 void AutocompleteMatch::ClassifyMatchInString( | 109 void AutocompleteMatch::ClassifyMatchInString( |
| 110 const string16& find_text, | 110 const string16& find_text, |
| 111 const string16& text, | 111 const string16& text, |
| 112 int style, | 112 int style, |
| 113 ACMatchClassifications* classification) { | 113 ACMatchClassifications* classification) { |
| 114 ClassifyLocationInString(text.find(find_text), find_text.length(), | 114 ClassifyLocationInString(text.find(find_text), find_text.length(), |
| 115 text.length(), style, classification); | 115 text.length(), style, classification); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 144 classification->push_back(ACMatchClassification(match_location, | 144 classification->push_back(ACMatchClassification(match_location, |
| 145 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 145 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
| 146 | 146 |
| 147 // Mark post-match portion of string (if any). | 147 // Mark post-match portion of string (if any). |
| 148 const size_t after_match(match_location + match_length); | 148 const size_t after_match(match_location + match_length); |
| 149 if (after_match < overall_length) { | 149 if (after_match < overall_length) { |
| 150 classification->push_back(ACMatchClassification(after_match, style)); | 150 classification->push_back(ACMatchClassification(after_match, style)); |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 | 153 |
| 154 void AutocompleteMatch::ComputeStrippedDestinationURL() { | |
| 155 static const char prefix[] = "www."; | |
| 156 static const size_t prefix_len = strlen(prefix); | |
|
Peter Kasting
2011/04/11 23:17:33
Nit: Use arraysize(prefix) - 1 so this is a compil
| |
| 157 | |
| 158 if (!stripped_destination_url.is_empty()) | |
|
Peter Kasting
2011/04/11 23:17:33
Nit: This check seems unnecessary
| |
| 159 return; | |
| 160 | |
| 161 std::string host = destination_url.host(); | |
| 162 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { | |
| 163 host = host.substr(prefix_len); | |
| 164 GURL::Replacements replace_host; | |
| 165 replace_host.SetHostStr(host); | |
| 166 stripped_destination_url = | |
| 167 destination_url.ReplaceComponents(replace_host); | |
| 168 } else { | |
| 169 stripped_destination_url = destination_url; | |
| 170 } | |
| 171 } | |
| 172 | |
| 154 #ifndef NDEBUG | 173 #ifndef NDEBUG |
| 155 void AutocompleteMatch::Validate() const { | 174 void AutocompleteMatch::Validate() const { |
| 156 ValidateClassifications(contents, contents_class); | 175 ValidateClassifications(contents, contents_class); |
| 157 ValidateClassifications(description, description_class); | 176 ValidateClassifications(description, description_class); |
| 158 } | 177 } |
| 159 | 178 |
| 160 void AutocompleteMatch::ValidateClassifications( | 179 void AutocompleteMatch::ValidateClassifications( |
| 161 const string16& text, | 180 const string16& text, |
| 162 const ACMatchClassifications& classifications) const { | 181 const ACMatchClassifications& classifications) const { |
| 163 if (text.empty()) { | 182 if (text.empty()) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 174 // The classifications should always be sorted. | 193 // The classifications should always be sorted. |
| 175 size_t last_offset = classifications[0].offset; | 194 size_t last_offset = classifications[0].offset; |
| 176 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 195 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
| 177 i != classifications.end(); ++i) { | 196 i != classifications.end(); ++i) { |
| 178 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 197 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
| 179 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 198 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
| 180 last_offset = i->offset; | 199 last_offset = i->offset; |
| 181 } | 200 } |
| 182 } | 201 } |
| 183 #endif | 202 #endif |
| OLD | NEW |