collator.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_COLLATOR_HPP_INCLUDED
  7. #define BOOST_LOCALE_COLLATOR_HPP_INCLUDED
  8. #include <boost/locale/config.hpp>
  9. #include <locale>
  10. #ifdef BOOST_MSVC
  11. # pragma warning(push)
  12. # pragma warning(disable : 4275 4251 4231 4660)
  13. #endif
  14. namespace boost { namespace locale {
  15. /// \defgroup collation Collation
  16. ///
  17. /// This module introduces collation related classes
  18. /// @{
  19. /// Unicode collation level types
  20. enum class collate_level {
  21. primary = 0, ///< 1st collation level: base letters
  22. secondary = 1, ///< 2nd collation level: letters and accents
  23. tertiary = 2, ///< 3rd collation level: letters, accents and case
  24. quaternary = 3, ///< 4th collation level: letters, accents, case and punctuation
  25. identical = 4 ///< identical collation level: include code-point comparison
  26. };
  27. class BOOST_DEPRECATED("Use collate_level") collator_base {
  28. public:
  29. using level_type = collate_level;
  30. static constexpr auto primary = collate_level::primary;
  31. static constexpr auto secondary = collate_level::secondary;
  32. static constexpr auto tertiary = collate_level::tertiary;
  33. static constexpr auto quaternary = collate_level::quaternary;
  34. static constexpr auto identical = collate_level::identical;
  35. };
  36. /// \brief Collation facet.
  37. ///
  38. /// It reimplements standard C++ std::collate,
  39. /// allowing usage of std::locale for direct string comparison
  40. template<typename CharType>
  41. class collator : public std::collate<CharType> {
  42. public:
  43. /// Type of the underlying character
  44. typedef CharType char_type;
  45. /// Type of string used with this facet
  46. typedef std::basic_string<CharType> string_type;
  47. /// Compare two strings in rage [b1,e1), [b2,e2) according using a collation level \a level. Calls do_compare
  48. ///
  49. /// Returns -1 if the first of the two strings sorts before the seconds, returns 1 if sorts after and 0 if
  50. /// they considered equal.
  51. int compare(collate_level level,
  52. const char_type* b1,
  53. const char_type* e1,
  54. const char_type* b2,
  55. const char_type* e2) const
  56. {
  57. return do_compare(level, b1, e1, b2, e2);
  58. }
  59. /// Create a binary string that can be compared to other in order to get collation order. The string is created
  60. /// for text in range [b,e). It is useful for collation of multiple strings for text.
  61. ///
  62. /// The transformation follows these rules:
  63. /// \code
  64. /// compare(level,b1,e1,b2,e2) == sign( transform(level,b1,e1).compare(transform(level,b2,e2)) );
  65. /// \endcode
  66. ///
  67. /// Calls do_transform
  68. string_type transform(collate_level level, const char_type* b, const char_type* e) const
  69. {
  70. return do_transform(level, b, e);
  71. }
  72. /// Calculate a hash of a text in range [b,e). The value can be used for collation sensitive string comparison.
  73. ///
  74. /// If compare(level,b1,e1,b2,e2) == 0 then hash(level,b1,e1) == hash(level,b2,e2)
  75. ///
  76. /// Calls do_hash
  77. long hash(collate_level level, const char_type* b, const char_type* e) const { return do_hash(level, b, e); }
  78. /// Compare two strings \a l and \a r using collation level \a level
  79. ///
  80. /// Returns -1 if the first of the two strings sorts before the seconds, returns 1 if sorts after and 0 if
  81. /// they considered equal.
  82. int compare(collate_level level, const string_type& l, const string_type& r) const
  83. {
  84. return do_compare(level, l.data(), l.data() + l.size(), r.data(), r.data() + r.size());
  85. }
  86. /// Calculate a hash that can be used for collation sensitive string comparison of a string \a s
  87. ///
  88. /// If compare(level,s1,s2) == 0 then hash(level,s1) == hash(level,s2)
  89. long hash(collate_level level, const string_type& s) const
  90. {
  91. return do_hash(level, s.data(), s.data() + s.size());
  92. }
  93. /// Create a binary string from string \a s, that can be compared to other, useful for collation of multiple
  94. /// strings.
  95. ///
  96. /// The transformation follows these rules:
  97. /// \code
  98. /// compare(level,s1,s2) == sign( transform(level,s1).compare(transform(level,s2)) );
  99. /// \endcode
  100. string_type transform(collate_level level, const string_type& s) const
  101. {
  102. return do_transform(level, s.data(), s.data() + s.size());
  103. }
  104. protected:
  105. /// constructor of the collator object
  106. collator(size_t refs = 0) : std::collate<CharType>(refs) {}
  107. /// This function is used to override default collation function that does not take in account collation level.
  108. /// Uses primary level
  109. int
  110. do_compare(const char_type* b1, const char_type* e1, const char_type* b2, const char_type* e2) const override
  111. {
  112. return do_compare(collate_level::identical, b1, e1, b2, e2);
  113. }
  114. /// This function is used to override default collation function that does not take in account collation level.
  115. /// Uses primary level
  116. string_type do_transform(const char_type* b, const char_type* e) const override
  117. {
  118. return do_transform(collate_level::identical, b, e);
  119. }
  120. /// This function is used to override default collation function that does not take in account collation level.
  121. /// Uses primary level
  122. long do_hash(const char_type* b, const char_type* e) const override
  123. {
  124. return do_hash(collate_level::identical, b, e);
  125. }
  126. /// Actual function that performs comparison between the strings. For details see compare member function. Can
  127. /// be overridden.
  128. virtual int do_compare(collate_level level,
  129. const char_type* b1,
  130. const char_type* e1,
  131. const char_type* b2,
  132. const char_type* e2) const = 0;
  133. /// Actual function that performs transformation. For details see transform member function. Can be overridden.
  134. virtual string_type do_transform(collate_level level, const char_type* b, const char_type* e) const = 0;
  135. /// Actual function that calculates hash. For details see hash member function. Can be overridden.
  136. virtual long do_hash(collate_level level, const char_type* b, const char_type* e) const = 0;
  137. };
  138. /// \brief This class can be used in STL algorithms and containers for comparison of strings
  139. /// with a level other than primary
  140. ///
  141. /// For example:
  142. ///
  143. /// \code
  144. /// std::map<std::string,std::string,comparator<char,collate_level::secondary> > data;
  145. /// \endcode
  146. ///
  147. /// Would create a map the keys of which are sorted using secondary collation level
  148. template<typename CharType, collate_level default_level = collate_level::identical>
  149. struct comparator {
  150. public:
  151. /// Create a comparator class for locale \a l and with collation leval \a level
  152. ///
  153. /// \throws std::bad_cast: \a l does not have \ref collator facet installed
  154. comparator(const std::locale& l = std::locale(), collate_level level = default_level) :
  155. locale_(l), level_(level)
  156. {}
  157. /// Compare two strings -- equivalent to return left < right according to collation rules
  158. bool operator()(const std::basic_string<CharType>& left, const std::basic_string<CharType>& right) const
  159. {
  160. return std::use_facet<collator<CharType>>(locale_).compare(level_, left, right) < 0;
  161. }
  162. private:
  163. std::locale locale_;
  164. collate_level level_;
  165. };
  166. ///@}
  167. }} // namespace boost::locale
  168. #ifdef BOOST_MSVC
  169. # pragma warning(pop)
  170. #endif
  171. ///
  172. /// \example collate.cpp
  173. /// Example of using collation functions
  174. ///
  175. #endif