facets.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_BOUNDARY_FACETS_HPP_INCLUDED
  7. #define BOOST_LOCALE_BOUNDARY_FACETS_HPP_INCLUDED
  8. #include <boost/locale/boundary/types.hpp>
  9. #include <locale>
  10. #include <vector>
  11. #ifdef BOOST_MSVC
  12. # pragma warning(push)
  13. # pragma warning(disable : 4275 4251 4231 4660)
  14. #endif
  15. namespace boost { namespace locale {
  16. /// \brief This namespace contains all operations required for boundary analysis of text
  17. namespace boundary {
  18. /// \addtogroup boundary
  19. ///
  20. /// @{
  21. /// \brief This structure is used for representing boundary points
  22. /// that follow the offset.
  23. struct break_info {
  24. /// Create empty break point at beginning
  25. break_info() : offset(0), rule(0) {}
  26. /// Create an empty break point at offset v.
  27. /// it is useful for order comparison with other points.
  28. break_info(size_t v) : offset(v), rule(0) {}
  29. /// Offset from the beginning of the text where a break occurs.
  30. size_t offset;
  31. /// The identification of this break point according to
  32. /// various break types
  33. rule_type rule;
  34. /// Compare two break points' offset. Allows to search with
  35. /// standard algorithms over the index.
  36. bool operator<(const break_info& other) const { return offset < other.offset; }
  37. };
  38. /// This type holds the analysis of the text - all its break points
  39. /// with marks
  40. typedef std::vector<break_info> index_type;
  41. template<typename CharType>
  42. class boundary_indexing;
  43. #ifdef BOOST_LOCALE_DOXYGEN
  44. /// \brief This facet generates an index for boundary analysis
  45. /// for a given text.
  46. ///
  47. /// It is specialized for 4 types of characters \c char_t, \c wchar_t, \c char16_t and \c char32_t
  48. template<typename Char>
  49. class BOOST_LOCALE_DECL boundary_indexing : public std::locale::facet {
  50. public:
  51. /// Default constructor typical for facets
  52. boundary_indexing(size_t refs = 0) : std::locale::facet(refs) {}
  53. /// Create index for boundary type \a t for text in range [begin,end)
  54. ///
  55. /// The returned value is an index of type \ref index_type. Note that this
  56. /// index is never empty, even if the range [begin,end) is empty it consists
  57. /// of at least one boundary point with the offset 0.
  58. virtual index_type map(boundary_type t, const Char* begin, const Char* end) const = 0;
  59. /// Identification of this facet
  60. static std::locale::id id;
  61. };
  62. #else
  63. template<>
  64. class BOOST_LOCALE_DECL boundary_indexing<char> : public std::locale::facet {
  65. public:
  66. boundary_indexing(size_t refs = 0) : std::locale::facet(refs) {}
  67. ~boundary_indexing();
  68. virtual index_type map(boundary_type t, const char* begin, const char* end) const = 0;
  69. static std::locale::id id;
  70. };
  71. template<>
  72. class BOOST_LOCALE_DECL boundary_indexing<wchar_t> : public std::locale::facet {
  73. public:
  74. boundary_indexing(size_t refs = 0) : std::locale::facet(refs) {}
  75. ~boundary_indexing();
  76. virtual index_type map(boundary_type t, const wchar_t* begin, const wchar_t* end) const = 0;
  77. static std::locale::id id;
  78. };
  79. # ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  80. template<>
  81. class BOOST_LOCALE_DECL boundary_indexing<char16_t> : public std::locale::facet {
  82. public:
  83. boundary_indexing(size_t refs = 0) : std::locale::facet(refs) {}
  84. ~boundary_indexing();
  85. virtual index_type map(boundary_type t, const char16_t* begin, const char16_t* end) const = 0;
  86. static std::locale::id id;
  87. };
  88. # endif
  89. # ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  90. template<>
  91. class BOOST_LOCALE_DECL boundary_indexing<char32_t> : public std::locale::facet {
  92. public:
  93. boundary_indexing(size_t refs = 0) : std::locale::facet(refs) {}
  94. ~boundary_indexing();
  95. virtual index_type map(boundary_type t, const char32_t* begin, const char32_t* end) const = 0;
  96. static std::locale::id id;
  97. };
  98. # endif
  99. #endif
  100. /// @}
  101. } // namespace boundary
  102. }} // namespace boost::locale
  103. #ifdef BOOST_MSVC
  104. # pragma warning(pop)
  105. #endif
  106. #endif