boundary_point.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_BOUNDARY_POINT_HPP_INCLUDED
  7. #define BOOST_LOCALE_BOUNDARY_BOUNDARY_POINT_HPP_INCLUDED
  8. #include <boost/locale/boundary/types.hpp>
  9. #include <string>
  10. namespace boost { namespace locale { namespace boundary {
  11. /// \addtogroup boundary
  12. /// @{
  13. /// \brief This class represents a boundary point in the text.
  14. ///
  15. /// It represents a pair - an iterator and a rule that defines this
  16. /// point.
  17. ///
  18. /// This type of object is dereferenced by the iterators of boundary_point_index. Using a rule()
  19. /// member function you can get the reason why this specific boundary point was selected.
  20. ///
  21. /// For example, when you use sentence boundary analysis, the (rule() & \ref sentence_term) != 0 means
  22. /// that this boundary point was selected because a sentence terminator (like .?!) was spotted
  23. /// and the (rule() & \ref sentence_sep)!=0 means that a separator like line feed or carriage
  24. /// return was observed.
  25. ///
  26. /// \note
  27. ///
  28. /// - The beginning of the analyzed range is always considered a boundary point and its rule is always 0.
  29. /// - When using word boundary analysis, the returned rule relates to a chunk of text preceding
  30. /// this point.
  31. ///
  32. /// \see
  33. ///
  34. /// - \ref boundary_point_index
  35. /// - \ref segment
  36. /// - \ref segment_index
  37. ///
  38. template<typename IteratorType>
  39. class boundary_point {
  40. public:
  41. /// The type of the base iterator that iterates the original text
  42. typedef IteratorType iterator_type;
  43. /// Empty default constructor
  44. boundary_point() : rule_(0) {}
  45. /// Create a new boundary_point using iterator \p and a rule \a r
  46. boundary_point(iterator_type p, rule_type r) : iterator_(p), rule_(r) {}
  47. /// Set an new iterator value \a i
  48. void iterator(iterator_type i) { iterator_ = i; }
  49. /// Fetch an iterator
  50. iterator_type iterator() const { return iterator_; }
  51. /// Set an new rule value \a r
  52. void rule(rule_type r) { rule_ = r; }
  53. /// Fetch a rule
  54. rule_type rule() const { return rule_; }
  55. /// Check if two boundary points are the same
  56. bool operator==(const boundary_point& other) const
  57. {
  58. return iterator_ == other.iterator_ && rule_ = other.rule_;
  59. }
  60. /// Check if two boundary points are different
  61. bool operator!=(const boundary_point& other) const { return !(*this == other); }
  62. /// Check if the boundary point points to same location as an iterator \a other
  63. bool operator==(const iterator_type& other) const { return iterator_ == other; }
  64. /// Check if the boundary point points to different location from an iterator \a other
  65. bool operator!=(const iterator_type& other) const { return iterator_ != other; }
  66. /// Automatic cast to the iterator it represents
  67. operator iterator_type() const { return iterator_; }
  68. private:
  69. iterator_type iterator_;
  70. rule_type rule_;
  71. };
  72. /// Check if the boundary point \a r points to same location as an iterator \a l
  73. template<typename BaseIterator>
  74. bool operator==(const BaseIterator& l, const boundary_point<BaseIterator>& r)
  75. {
  76. return r == l;
  77. }
  78. /// Check if the boundary point \a r points to different location from an iterator \a l
  79. template<typename BaseIterator>
  80. bool operator!=(const BaseIterator& l, const boundary_point<BaseIterator>& r)
  81. {
  82. return r != l;
  83. }
  84. /// @}
  85. typedef boundary_point<std::string::const_iterator> sboundary_point; ///< convenience typedef
  86. typedef boundary_point<std::wstring::const_iterator> wsboundary_point; ///< convenience typedef
  87. #ifndef BOOST_LOCALE_NO_CXX20_STRING8
  88. typedef boundary_point<std::u8string::const_iterator> u8sboundary_point; ///< convenience typedef
  89. #endif
  90. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  91. typedef boundary_point<std::u16string::const_iterator> u16sboundary_point; ///< convenience typedef
  92. #endif
  93. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  94. typedef boundary_point<std::u32string::const_iterator> u32sboundary_point; ///< convenience typedef
  95. #endif
  96. typedef boundary_point<const char*> cboundary_point; ///< convenience typedef
  97. typedef boundary_point<const wchar_t*> wcboundary_point; ///< convenience typedef
  98. #ifdef __cpp_char8_t
  99. typedef boundary_point<const char8_t*> u8cboundary_point; ///< convenience typedef
  100. #endif
  101. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  102. typedef boundary_point<const char16_t*> u16cboundary_point; ///< convenience typedef
  103. #endif
  104. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  105. typedef boundary_point<const char32_t*> u32cboundary_point; ///< convenience typedef
  106. #endif
  107. }}} // namespace boost::locale::boundary
  108. #endif