string_search.h 3.5 KB

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