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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 // For equal-relevance matches, we sort alphabetically, so that providers | 82 // For equal-relevance matches, we sort alphabetically, so that providers |
83 // who return multiple elements at the same priority get a "stable" sort | 83 // who return multiple elements at the same priority get a "stable" sort |
84 // across multiple updates. | 84 // across multiple updates. |
85 if (elem1.relevance == elem2.relevance) | 85 if (elem1.relevance == elem2.relevance) |
86 return elem1.contents > elem2.contents; | 86 return elem1.contents > elem2.contents; |
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, |
Peter Kasting
2011/04/01 00:09:09
Nit: Do we still use this function and the next?
| |
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.destination_url != elem2.destination_url) ? |
98 (elem1.destination_url < elem2.destination_url) : | 98 (elem1.destination_url < elem2.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.destination_url == elem2.destination_url; |
106 } | 106 } |
107 | 107 |
108 // static | 108 // static |
109 bool AutocompleteMatch::NormalizedSortFunc(const AutocompleteMatch& elem1, | |
110 const AutocompleteMatch& elem2) { | |
111 // Sort identical normalized_urls together. Place the most relevant matches | |
112 // first, so that when we call std::unique(), these are the ones that get | |
113 // preserved. | |
114 return (elem1.normalized_url != elem2.normalized_url) ? | |
115 (elem1.normalized_url < elem2.normalized_url) : | |
116 MoreRelevant(elem1, elem2); | |
117 } | |
118 | |
119 // static | |
120 bool AutocompleteMatch::NormalizedEqual(const AutocompleteMatch& elem1, | |
121 const AutocompleteMatch& elem2) { | |
122 return elem1.normalized_url == elem2.normalized_url; | |
123 } | |
124 | |
125 // static | |
126 bool AutocompleteMatch::KeywordsEqual(const AutocompleteMatch& elem1, | |
127 const AutocompleteMatch& elem2) { | |
128 return elem1.keyword == elem2.keyword; | |
129 } | |
130 | |
131 // static | |
109 void AutocompleteMatch::ClassifyMatchInString( | 132 void AutocompleteMatch::ClassifyMatchInString( |
110 const string16& find_text, | 133 const string16& find_text, |
111 const string16& text, | 134 const string16& text, |
112 int style, | 135 int style, |
113 ACMatchClassifications* classification) { | 136 ACMatchClassifications* classification) { |
114 ClassifyLocationInString(text.find(find_text), find_text.length(), | 137 ClassifyLocationInString(text.find(find_text), find_text.length(), |
115 text.length(), style, classification); | 138 text.length(), style, classification); |
116 } | 139 } |
117 | 140 |
118 void AutocompleteMatch::ClassifyLocationInString( | 141 void AutocompleteMatch::ClassifyLocationInString( |
(...skipping 25 matching lines...) Expand all Loading... | |
144 classification->push_back(ACMatchClassification(match_location, | 167 classification->push_back(ACMatchClassification(match_location, |
145 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 168 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
146 | 169 |
147 // Mark post-match portion of string (if any). | 170 // Mark post-match portion of string (if any). |
148 const size_t after_match(match_location + match_length); | 171 const size_t after_match(match_location + match_length); |
149 if (after_match < overall_length) { | 172 if (after_match < overall_length) { |
150 classification->push_back(ACMatchClassification(after_match, style)); | 173 classification->push_back(ACMatchClassification(after_match, style)); |
151 } | 174 } |
152 } | 175 } |
153 | 176 |
177 | |
178 void AutocompleteMatch::NormalizeDestination() { | |
179 static const char prefix[] = "www."; | |
180 static const int prefix_len = strlen(prefix); | |
181 | |
182 if (destination_url.is_valid() && normalized_url.is_empty()) { | |
Peter Kasting
2011/04/01 00:09:09
Nit: I would combine this check with the compare()
| |
183 std::string host = destination_url.host(); | |
184 | |
185 if (host.compare(0, prefix_len, prefix) == 0) { | |
186 host.erase(0, prefix_len); | |
Peter Kasting
2011/04/01 00:09:09
Nit: You can eliminate this and use host.substr(..
| |
187 | |
188 GURL::Replacements replace_host; | |
189 replace_host.SetHostStr(host); | |
190 normalized_url = destination_url.ReplaceComponents(replace_host); | |
191 } else { | |
192 normalized_url = destination_url; | |
193 } | |
194 } | |
195 } | |
196 | |
154 #ifndef NDEBUG | 197 #ifndef NDEBUG |
155 void AutocompleteMatch::Validate() const { | 198 void AutocompleteMatch::Validate() const { |
156 ValidateClassifications(contents, contents_class); | 199 ValidateClassifications(contents, contents_class); |
157 ValidateClassifications(description, description_class); | 200 ValidateClassifications(description, description_class); |
158 } | 201 } |
159 | 202 |
160 void AutocompleteMatch::ValidateClassifications( | 203 void AutocompleteMatch::ValidateClassifications( |
161 const string16& text, | 204 const string16& text, |
162 const ACMatchClassifications& classifications) const { | 205 const ACMatchClassifications& classifications) const { |
163 if (text.empty()) { | 206 if (text.empty()) { |
(...skipping 10 matching lines...) Expand all Loading... | |
174 // The classifications should always be sorted. | 217 // The classifications should always be sorted. |
175 size_t last_offset = classifications[0].offset; | 218 size_t last_offset = classifications[0].offset; |
176 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 219 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
177 i != classifications.end(); ++i) { | 220 i != classifications.end(); ++i) { |
178 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 221 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
179 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 222 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
180 last_offset = i->offset; | 223 last_offset = i->offset; |
181 } | 224 } |
182 } | 225 } |
183 #endif | 226 #endif |
OLD | NEW |