1 // Copyright 2011 The Chromium Authors 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 BASE_I18N_STRING_SEARCH_H_ 6 #define BASE_I18N_STRING_SEARCH_H_ 7 8 #include <stddef.h> 9 10 #include <string> 11 12 #include "base/i18n/base_i18n_export.h" 13 #include "base/memory/raw_ptr.h" 14 15 struct UStringSearch; 16 17 namespace base { 18 namespace i18n { 19 20 // Returns true if |in_this| contains |find_this|. If |match_index| or 21 // |match_length| are non-NULL, they are assigned the start position and total 22 // length of the match. 23 // 24 // Only differences between base letters are taken into consideration. Case and 25 // accent differences are ignored. Please refer to 'primary level' in 26 // http://userguide.icu-project.org/collation/concepts for additional details. 27 BASE_I18N_EXPORT 28 bool StringSearchIgnoringCaseAndAccents(const std::u16string& find_this, 29 const std::u16string& in_this, 30 size_t* match_index, 31 size_t* match_length); 32 33 // Returns true if |in_this| contains |find_this|. If |match_index| or 34 // |match_length| are non-NULL, they are assigned the start position and total 35 // length of the match. 36 // 37 // When |case_sensitive| is false, only differences between base letters are 38 // taken into consideration. Case and accent differences are ignored. 39 // Please refer to 'primary level' in 40 // http://userguide.icu-project.org/collation/concepts for additional details. 41 // When |forward_search| is true, finds the first instance of |find_this|, 42 // otherwise finds the last instance 43 BASE_I18N_EXPORT 44 bool StringSearch(const std::u16string& find_this, 45 const std::u16string& in_this, 46 size_t* match_index, 47 size_t* match_length, 48 bool case_sensitive, 49 bool forward_search); 50 51 // This class is for speeding up multiple StringSearch() 52 // with the same |find_this| argument. |find_this| is passed as the constructor 53 // argument, and precomputation for searching is done only at that time. 54 class BASE_I18N_EXPORT FixedPatternStringSearch { 55 public: 56 explicit FixedPatternStringSearch(const std::u16string& find_this, 57 bool case_sensitive); 58 ~FixedPatternStringSearch(); 59 60 // Returns true if |in_this| contains |find_this|. If |match_index| or 61 // |match_length| are non-NULL, they are assigned the start position and total 62 // length of the match. 63 bool Search(const std::u16string& in_this, 64 size_t* match_index, 65 size_t* match_length, 66 bool forward_search); 67 68 private: 69 std::u16string find_this_; 70 raw_ptr<UStringSearch, DanglingUntriaged> search_; 71 }; 72 73 // This class is for speeding up multiple StringSearchIgnoringCaseAndAccents() 74 // with the same |find_this| argument. |find_this| is passed as the constructor 75 // argument, and precomputation for searching is done only at that time. 76 class BASE_I18N_EXPORT FixedPatternStringSearchIgnoringCaseAndAccents { 77 public: 78 explicit FixedPatternStringSearchIgnoringCaseAndAccents( 79 const std::u16string& find_this); 80 81 // Returns true if |in_this| contains |find_this|. If |match_index| or 82 // |match_length| are non-NULL, they are assigned the start position and total 83 // length of the match. 84 bool Search(const std::u16string& in_this, 85 size_t* match_index, 86 size_t* match_length); 87 88 private: 89 FixedPatternStringSearch base_search_; 90 }; 91 92 // This class is for performing all matches of `find_this` in `in_this`. 93 // `find_this` and `in_this` are passed as arguments in constructor. 94 class BASE_I18N_EXPORT RepeatingStringSearch { 95 public: 96 RepeatingStringSearch(const std::u16string& find_this, 97 const std::u16string& in_this, 98 bool case_sensitive); 99 ~RepeatingStringSearch(); 100 101 // Returns true if the next match exists. `match_index` and `match_length` are 102 // assigned the start position and total length of the match. 103 bool NextMatchResult(int& match_index, int& match_length); 104 105 private: 106 std::u16string find_this_; 107 std::u16string in_this_; 108 raw_ptr<UStringSearch> search_; 109 }; 110 111 } // namespace i18n 112 } // namespace base 113 114 #endif // BASE_I18N_STRING_SEARCH_H_ 115