facets.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_LOCALE_BOUNDARY_FACETS_HPP_INCLUDED
  9. #define BOOST_LOCALE_BOUNDARY_FACETS_HPP_INCLUDED
  10. #include <boost/locale/config.hpp>
  11. #include <boost/locale/boundary/types.hpp>
  12. #ifdef BOOST_MSVC
  13. # pragma warning(push)
  14. # pragma warning(disable : 4275 4251 4231 4660)
  15. #endif
  16. #include <locale>
  17. #include <vector>
  18. namespace boost {
  19. namespace locale {
  20. ///
  21. /// \brief This namespae contains all operations required for boundary analysis of text
  22. ///
  23. namespace boundary {
  24. ///
  25. /// \addtogroup boundary
  26. ///
  27. /// @{
  28. ///
  29. ///
  30. /// \brief This structure is used for representing boundary point
  31. /// that follows the offset.
  32. ///
  33. struct break_info {
  34. ///
  35. /// Create empty break point at beginning
  36. ///
  37. break_info() :
  38. offset(0),
  39. rule(0)
  40. {
  41. }
  42. ///
  43. /// Create empty break point at offset v.
  44. /// it is useful for order comparison with other points.
  45. ///
  46. break_info(size_t v) :
  47. offset(v),
  48. rule(0)
  49. {
  50. }
  51. ///
  52. /// Offset from the beggining of the text where a break occurs.
  53. ///
  54. size_t offset;
  55. ///
  56. /// The identification of this break point according to
  57. /// various break types
  58. ///
  59. rule_type rule;
  60. ///
  61. /// Compare two break points' offset. Allows to search with
  62. /// standard algorithms over the index.
  63. ///
  64. bool operator<(break_info const &other) const
  65. {
  66. return offset < other.offset;
  67. }
  68. };
  69. ///
  70. /// This type holds the analysis of the text - all its break points
  71. /// with marks
  72. ///
  73. typedef std::vector<break_info> index_type;
  74. template<typename CharType>
  75. class boundary_indexing;
  76. #ifdef BOOST_LOCALE_DOXYGEN
  77. ///
  78. /// \brief This facet generates an index for boundary analysis
  79. /// for a given text.
  80. ///
  81. /// It is specialized for 4 types of characters \c char_t, \c wchar_t, \c char16_t and \c char32_t
  82. ///
  83. template<typename Char>
  84. class BOOST_LOCALE_DECL boundary_indexing : public std::locale::facet {
  85. public:
  86. ///
  87. /// Default constructor typical for facets
  88. ///
  89. boundary_indexing(size_t refs=0) : std::locale::facet(refs)
  90. {
  91. }
  92. ///
  93. /// Create index for boundary type \a t for text in range [begin,end)
  94. ///
  95. /// The returned value is an index of type \ref index_type. Note that this
  96. /// index is never empty, even if the range [begin,end) is empty it consists
  97. /// of at least one boundary point with the offset 0.
  98. ///
  99. virtual index_type map(boundary_type t,Char const *begin,Char const *end) const = 0;
  100. ///
  101. /// Identification of this facet
  102. ///
  103. static std::locale::id id;
  104. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  105. std::locale::id& __get_id (void) const { return id; }
  106. #endif
  107. };
  108. #else
  109. template<>
  110. class BOOST_LOCALE_DECL boundary_indexing<char> : public std::locale::facet {
  111. public:
  112. boundary_indexing(size_t refs=0) : std::locale::facet(refs)
  113. {
  114. }
  115. virtual index_type map(boundary_type t,char const *begin,char const *end) const = 0;
  116. static std::locale::id id;
  117. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  118. std::locale::id& __get_id (void) const { return id; }
  119. #endif
  120. };
  121. template<>
  122. class BOOST_LOCALE_DECL boundary_indexing<wchar_t> : public std::locale::facet {
  123. public:
  124. boundary_indexing(size_t refs=0) : std::locale::facet(refs)
  125. {
  126. }
  127. virtual index_type map(boundary_type t,wchar_t const *begin,wchar_t const *end) const = 0;
  128. static std::locale::id id;
  129. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  130. std::locale::id& __get_id (void) const { return id; }
  131. #endif
  132. };
  133. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  134. template<>
  135. class BOOST_LOCALE_DECL boundary_indexing<char16_t> : public std::locale::facet {
  136. public:
  137. boundary_indexing(size_t refs=0) : std::locale::facet(refs)
  138. {
  139. }
  140. virtual index_type map(boundary_type t,char16_t const *begin,char16_t const *end) const = 0;
  141. static std::locale::id id;
  142. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  143. std::locale::id& __get_id (void) const { return id; }
  144. #endif
  145. };
  146. #endif
  147. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  148. template<>
  149. class BOOST_LOCALE_DECL boundary_indexing<char32_t> : public std::locale::facet {
  150. public:
  151. boundary_indexing(size_t refs=0) : std::locale::facet(refs)
  152. {
  153. }
  154. virtual index_type map(boundary_type t,char32_t const *begin,char32_t const *end) const = 0;
  155. static std::locale::id id;
  156. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  157. std::locale::id& __get_id (void) const { return id; }
  158. #endif
  159. };
  160. #endif
  161. #endif
  162. ///
  163. /// @}
  164. ///
  165. } // boundary
  166. } // locale
  167. } // boost
  168. #ifdef BOOST_MSVC
  169. #pragma warning(pop)
  170. #endif
  171. #endif
  172. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4