boundary_point.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_BOUNDARY_POINT_HPP_INCLUDED
  9. #define BOOST_LOCALE_BOUNDARY_BOUNDARY_POINT_HPP_INCLUDED
  10. #include <boost/locale/boundary/types.hpp>
  11. namespace boost {
  12. namespace locale {
  13. namespace boundary {
  14. ///
  15. /// \addtogroup boundary
  16. /// @{
  17. ///
  18. /// \brief This class represents a boundary point in the text.
  19. ///
  20. /// It represents a pair - an iterator and a rule that defines this
  21. /// point.
  22. ///
  23. /// This type of object is dereference by the iterators of boundary_point_index. Using a rule()
  24. /// member function you can get the reason why this specific boundary point was selected.
  25. ///
  26. /// For example, When you use a sentence boundary analysis, the (rule() & \ref sentence_term) != 0 means
  27. /// that this boundary point was selected because a sentence terminator (like .?!) was spotted
  28. /// and the (rule() & \ref sentence_sep)!=0 means that a separator like line feed or carriage
  29. /// return was observed.
  30. ///
  31. /// \note
  32. ///
  33. /// - The beginning of analyzed range is always considered a boundary point and its rule is always 0.
  34. /// - when using a word boundary analysis the returned rule relates to a chunk of text preceding
  35. /// this point.
  36. ///
  37. /// \see
  38. ///
  39. /// - \ref boundary_point_index
  40. /// - \ref segment
  41. /// - \ref segment_index
  42. ///
  43. template<typename IteratorType>
  44. class boundary_point {
  45. public:
  46. ///
  47. /// The type of the base iterator that iterates the original text
  48. ///
  49. typedef IteratorType iterator_type;
  50. ///
  51. /// Empty default constructor
  52. ///
  53. boundary_point() : rule_(0) {}
  54. ///
  55. /// Create a new boundary_point using iterator \p and a rule \a r
  56. ///
  57. boundary_point(iterator_type p,rule_type r) :
  58. iterator_(p),
  59. rule_(r)
  60. {
  61. }
  62. ///
  63. /// Set an new iterator value \a i
  64. ///
  65. void iterator(iterator_type i)
  66. {
  67. iterator_ = i;
  68. }
  69. ///
  70. /// Set an new rule value \a r
  71. ///
  72. void rule(rule_type r)
  73. {
  74. rule_ = r;
  75. }
  76. ///
  77. /// Fetch an iterator
  78. ///
  79. iterator_type iterator() const
  80. {
  81. return iterator_;
  82. }
  83. ///
  84. /// Fetch a rule
  85. ///
  86. rule_type rule() const
  87. {
  88. return rule_;
  89. }
  90. ///
  91. /// Check if two boundary points are the same
  92. ///
  93. bool operator==(boundary_point const &other) const
  94. {
  95. return iterator_ == other.iterator_ && rule_ = other.rule_;
  96. }
  97. ///
  98. /// Check if two boundary points are different
  99. ///
  100. bool operator!=(boundary_point const &other) const
  101. {
  102. return !(*this==other);
  103. }
  104. ///
  105. /// Check if the boundary point points to same location as an iterator \a other
  106. ///
  107. bool operator==(iterator_type const &other) const
  108. {
  109. return iterator_ == other;
  110. }
  111. ///
  112. /// Check if the boundary point points to different location from an iterator \a other
  113. ///
  114. bool operator!=(iterator_type const &other) const
  115. {
  116. return iterator_ != other;
  117. }
  118. ///
  119. /// Automatic cast to the iterator it represents
  120. ///
  121. operator iterator_type ()const
  122. {
  123. return iterator_;
  124. }
  125. private:
  126. iterator_type iterator_;
  127. rule_type rule_;
  128. };
  129. ///
  130. /// Check if the boundary point \a r points to same location as an iterator \a l
  131. ///
  132. template<typename BaseIterator>
  133. bool operator==(BaseIterator const &l,boundary_point<BaseIterator> const &r)
  134. {
  135. return r==l;
  136. }
  137. ///
  138. /// Check if the boundary point \a r points to different location from an iterator \a l
  139. ///
  140. template<typename BaseIterator>
  141. bool operator!=(BaseIterator const &l,boundary_point<BaseIterator> const &r)
  142. {
  143. return r!=l;
  144. }
  145. /// @}
  146. typedef boundary_point<std::string::const_iterator> sboundary_point; ///< convenience typedef
  147. typedef boundary_point<std::wstring::const_iterator> wsboundary_point; ///< convenience typedef
  148. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  149. typedef boundary_point<std::u16string::const_iterator> u16sboundary_point;///< convenience typedef
  150. #endif
  151. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  152. typedef boundary_point<std::u32string::const_iterator> u32sboundary_point;///< convenience typedef
  153. #endif
  154. typedef boundary_point<char const *> cboundary_point; ///< convenience typedef
  155. typedef boundary_point<wchar_t const *> wcboundary_point; ///< convenience typedef
  156. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  157. typedef boundary_point<char16_t const *> u16cboundary_point; ///< convenience typedef
  158. #endif
  159. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  160. typedef boundary_point<char32_t const *> u32cboundary_point; ///< convenience typedef
  161. #endif
  162. } // boundary
  163. } // locale
  164. } // boost
  165. #endif
  166. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4