string_search.h 4.2 KB

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