collator.hpp 8.1 KB

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