1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef UI_APP_LIST_SEARCH_TOKENIZED_STRING_MATCH_H_ 6 #define UI_APP_LIST_SEARCH_TOKENIZED_STRING_MATCH_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/strings/string16.h" 12 #include "ui/app_list/app_list_export.h" 13 #include "ui/gfx/range/range.h" 14 15 namespace app_list { 16 17 class TokenizedString; 18 19 // TokenizedStringMatch takes two tokenized strings: one as the text and 20 // the other one as the query. It matches the query against the text, 21 // calculates a relevance score between [0, 1] and marks the matched portions 22 // of text. A relevance of zero means the two are completely different to each 23 // other. The higher the relevance score, the better the two strings are 24 // matched. Matched portions of text are stored as index ranges. 25 class APP_LIST_EXPORT TokenizedStringMatch { 26 public: 27 typedef std::vector<gfx::Range> Hits; 28 29 TokenizedStringMatch(); 30 ~TokenizedStringMatch(); 31 32 // Calculates the relevance and hits. Returns true if the two strings are 33 // somewhat matched, i.e. relevance score is not zero. 34 bool Calculate(const TokenizedString& query, const TokenizedString& text); 35 36 // Convenience wrapper to calculate match from raw string input. 37 bool Calculate(const base::string16& query, const base::string16& text); 38 39 double relevance() const { return relevance_; } 40 const Hits& hits() const { return hits_; } 41 42 private: 43 // Score in range of [0,1] representing how well the query matches the text. 44 double relevance_; 45 46 // Char index ranges in |text| of where matches are found. 47 Hits hits_; 48 49 DISALLOW_COPY_AND_ASSIGN(TokenizedStringMatch); 50 }; 51 52 } // namespace app_list 53 54 #endif // UI_APP_LIST_SEARCH_TOKENIZED_STRING_MATCH_H_ 55