facets.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <boost/locale/detail/facet_id.hpp>
  10. #include <boost/locale/detail/is_supported_char.hpp>
  11. #include <locale>
  12. #include <vector>
  13. #ifdef BOOST_MSVC
  14. # pragma warning(push)
  15. # pragma warning(disable : 4275 4251 4231 4660)
  16. #endif
  17. namespace boost { namespace locale {
  18. /// \brief This namespace contains all operations required for boundary analysis of text
  19. namespace boundary {
  20. /// \addtogroup boundary
  21. ///
  22. /// @{
  23. /// \brief This structure is used for representing boundary points
  24. /// that follow the offset.
  25. struct break_info {
  26. /// Create empty break point at beginning
  27. break_info() : offset(0), rule(0) {}
  28. /// Create an empty break point at offset v.
  29. /// it is useful for order comparison with other points.
  30. break_info(size_t v) : offset(v), rule(0) {}
  31. /// Offset from the beginning of the text where a break occurs.
  32. size_t offset;
  33. /// The identification of this break point according to
  34. /// various break types
  35. rule_type rule;
  36. /// Compare two break points' offset. Allows to search with
  37. /// standard algorithms over the index.
  38. bool operator<(const break_info& other) const { return offset < other.offset; }
  39. };
  40. /// This type holds the analysis of the text - all its break points
  41. /// with marks
  42. typedef std::vector<break_info> index_type;
  43. /// \brief This facet generates an index for boundary analysis of a given text.
  44. ///
  45. /// It is implemented for supported character types, at least \c char, \c wchar_t
  46. template<typename Char>
  47. class BOOST_SYMBOL_VISIBLE boundary_indexing : public std::locale::facet,
  48. public boost::locale::detail::facet_id<boundary_indexing<Char>> {
  49. BOOST_LOCALE_ASSERT_IS_SUPPORTED(Char);
  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. };
  60. /// @}
  61. } // namespace boundary
  62. }} // namespace boost::locale
  63. #ifdef BOOST_MSVC
  64. # pragma warning(pop)
  65. #endif
  66. #endif