| 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 |
| 11 AutocompleteMatch::AutocompleteMatch() | 11 AutocompleteMatch::AutocompleteMatch() |
| 12 : provider(NULL), | 12 : provider(NULL), |
| 13 relevance(0), | 13 relevance(0), |
| 14 deletable(false), | 14 deletable(false), |
| 15 inline_autocomplete_offset(string16::npos), | 15 inline_autocomplete_offset(std::wstring::npos), |
| 16 transition(PageTransition::GENERATED), | 16 transition(PageTransition::GENERATED), |
| 17 is_history_what_you_typed_match(false), | 17 is_history_what_you_typed_match(false), |
| 18 type(SEARCH_WHAT_YOU_TYPED), | 18 type(SEARCH_WHAT_YOU_TYPED), |
| 19 template_url(NULL), | 19 template_url(NULL), |
| 20 starred(false) { | 20 starred(false) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider, | 23 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider, |
| 24 int relevance, | 24 int relevance, |
| 25 bool deletable, | 25 bool deletable, |
| 26 Type type) | 26 Type type) |
| 27 : provider(provider), | 27 : provider(provider), |
| 28 relevance(relevance), | 28 relevance(relevance), |
| 29 deletable(deletable), | 29 deletable(deletable), |
| 30 inline_autocomplete_offset(string16::npos), | 30 inline_autocomplete_offset(std::wstring::npos), |
| 31 transition(PageTransition::TYPED), | 31 transition(PageTransition::TYPED), |
| 32 is_history_what_you_typed_match(false), | 32 is_history_what_you_typed_match(false), |
| 33 type(type), | 33 type(type), |
| 34 template_url(NULL), | 34 template_url(NULL), |
| 35 starred(false) { | 35 starred(false) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 AutocompleteMatch::~AutocompleteMatch() { | 38 AutocompleteMatch::~AutocompleteMatch() { |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 98 } |
| 99 | 99 |
| 100 // static | 100 // static |
| 101 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 101 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
| 102 const AutocompleteMatch& elem2) { | 102 const AutocompleteMatch& elem2) { |
| 103 return elem1.destination_url == elem2.destination_url; | 103 return elem1.destination_url == elem2.destination_url; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // static | 106 // static |
| 107 void AutocompleteMatch::ClassifyMatchInString( | 107 void AutocompleteMatch::ClassifyMatchInString( |
| 108 const string16& find_text, | 108 const std::wstring& find_text, |
| 109 const string16& text, | 109 const std::wstring& text, |
| 110 int style, | 110 int style, |
| 111 ACMatchClassifications* classification) { | 111 ACMatchClassifications* classification) { |
| 112 ClassifyLocationInString(text.find(find_text), find_text.length(), | 112 ClassifyLocationInString(text.find(find_text), find_text.length(), |
| 113 text.length(), style, classification); | 113 text.length(), style, classification); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void AutocompleteMatch::ClassifyLocationInString( | 116 void AutocompleteMatch::ClassifyLocationInString( |
| 117 size_t match_location, | 117 size_t match_location, |
| 118 size_t match_length, | 118 size_t match_length, |
| 119 size_t overall_length, | 119 size_t overall_length, |
| 120 int style, | 120 int style, |
| 121 ACMatchClassifications* classification) { | 121 ACMatchClassifications* classification) { |
| 122 classification->clear(); | 122 classification->clear(); |
| 123 | 123 |
| 124 // Don't classify anything about an empty string | 124 // Don't classify anything about an empty string |
| 125 // (AutocompleteMatch::Validate() checks this). | 125 // (AutocompleteMatch::Validate() checks this). |
| 126 if (overall_length == 0) | 126 if (overall_length == 0) |
| 127 return; | 127 return; |
| 128 | 128 |
| 129 // Mark pre-match portion of string (if any). | 129 // Mark pre-match portion of string (if any). |
| 130 if (match_location != 0) { | 130 if (match_location != 0) { |
| 131 classification->push_back(ACMatchClassification(0, style)); | 131 classification->push_back(ACMatchClassification(0, style)); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Mark matching portion of string. | 134 // Mark matching portion of string. |
| 135 if (match_location == string16::npos) { | 135 if (match_location == std::wstring::npos) { |
| 136 // No match, above classification will suffice for whole string. | 136 // No match, above classification will suffice for whole string. |
| 137 return; | 137 return; |
| 138 } | 138 } |
| 139 // Classifying an empty match makes no sense and will lead to validation | 139 // Classifying an empty match makes no sense and will lead to validation |
| 140 // errors later. | 140 // errors later. |
| 141 DCHECK(match_length > 0); | 141 DCHECK(match_length > 0); |
| 142 classification->push_back(ACMatchClassification(match_location, | 142 classification->push_back(ACMatchClassification(match_location, |
| 143 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 143 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
| 144 | 144 |
| 145 // Mark post-match portion of string (if any). | 145 // Mark post-match portion of string (if any). |
| 146 const size_t after_match(match_location + match_length); | 146 const size_t after_match(match_location + match_length); |
| 147 if (after_match < overall_length) { | 147 if (after_match < overall_length) { |
| 148 classification->push_back(ACMatchClassification(after_match, style)); | 148 classification->push_back(ACMatchClassification(after_match, style)); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 #ifndef NDEBUG | 152 #ifndef NDEBUG |
| 153 void AutocompleteMatch::Validate() const { | 153 void AutocompleteMatch::Validate() const { |
| 154 ValidateClassifications(contents, contents_class); | 154 ValidateClassifications(contents, contents_class); |
| 155 ValidateClassifications(description, description_class); | 155 ValidateClassifications(description, description_class); |
| 156 } | 156 } |
| 157 | 157 |
| 158 void AutocompleteMatch::ValidateClassifications( | 158 void AutocompleteMatch::ValidateClassifications( |
| 159 const string16& text, | 159 const std::wstring& text, |
| 160 const ACMatchClassifications& classifications) const { | 160 const ACMatchClassifications& classifications) const { |
| 161 if (text.empty()) { | 161 if (text.empty()) { |
| 162 DCHECK(classifications.size() == 0); | 162 DCHECK(classifications.size() == 0); |
| 163 return; | 163 return; |
| 164 } | 164 } |
| 165 | 165 |
| 166 // The classifications should always cover the whole string. | 166 // The classifications should always cover the whole string. |
| 167 DCHECK(classifications.size() > 0) << "No classification for text"; | 167 DCHECK(classifications.size() > 0) << "No classification for text"; |
| 168 DCHECK(classifications[0].offset == 0) << "Classification misses beginning"; | 168 DCHECK(classifications[0].offset == 0) << "Classification misses beginning"; |
| 169 if (classifications.size() == 1) | 169 if (classifications.size() == 1) |
| 170 return; | 170 return; |
| 171 | 171 |
| 172 // The classifications should always be sorted. | 172 // The classifications should always be sorted. |
| 173 size_t last_offset = classifications[0].offset; | 173 size_t last_offset = classifications[0].offset; |
| 174 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 174 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
| 175 i != classifications.end(); ++i) { | 175 i != classifications.end(); ++i) { |
| 176 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 176 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
| 177 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 177 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
| 178 last_offset = i->offset; | 178 last_offset = i->offset; |
| 179 } | 179 } |
| 180 } | 180 } |
| 181 #endif | 181 #endif |
| OLD | NEW |