no_case.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_SUPPORT_NO_CASE_SEPT_24_2014_1125PM)
  7. #define BOOST_SPIRIT_X3_SUPPORT_NO_CASE_SEPT_24_2014_1125PM
  8. #include <boost/spirit/home/x3/support/unused.hpp>
  9. #include <boost/spirit/home/x3/support/context.hpp>
  10. #include <boost/spirit/home/x3/char/char_class_tags.hpp>
  11. namespace boost { namespace spirit { namespace x3
  12. {
  13. struct no_case_tag {};
  14. template <typename Encoding>
  15. struct case_compare
  16. {
  17. template <typename Char, typename CharSet>
  18. bool in_set(Char ch, CharSet const& set)
  19. {
  20. return set.test(ch);
  21. }
  22. template <typename Char>
  23. int32_t operator()(Char lc, Char rc) const
  24. {
  25. return lc - rc;
  26. }
  27. template <typename CharClassTag>
  28. CharClassTag get_char_class_tag(CharClassTag tag) const
  29. {
  30. return tag;
  31. }
  32. };
  33. template <typename Encoding>
  34. struct no_case_compare
  35. {
  36. template <typename Char, typename CharSet>
  37. bool in_set(Char ch_, CharSet const& set)
  38. {
  39. using char_type = typename Encoding::classify_type;
  40. auto ch = char_type(ch_);
  41. return set.test(ch)
  42. || set.test(Encoding::islower(ch)
  43. ? Encoding::toupper(ch) : Encoding::tolower(ch));
  44. }
  45. template <typename Char>
  46. int32_t operator()(Char lc_, Char const rc_) const
  47. {
  48. using char_type = typename Encoding::classify_type;
  49. auto lc = char_type(lc_);
  50. auto rc = char_type(rc_);
  51. return Encoding::islower(rc)
  52. ? Encoding::tolower(lc) - rc : Encoding::toupper(lc) - rc;
  53. }
  54. template <typename CharClassTag>
  55. CharClassTag get_char_class_tag(CharClassTag tag) const
  56. {
  57. return tag;
  58. }
  59. alpha_tag get_char_class_tag(lower_tag ) const
  60. {
  61. return {};
  62. }
  63. alpha_tag get_char_class_tag(upper_tag ) const
  64. {
  65. return {};
  66. }
  67. };
  68. template <typename Encoding>
  69. case_compare<Encoding> get_case_compare_impl(unused_type const&)
  70. {
  71. return {};
  72. }
  73. template <typename Encoding>
  74. no_case_compare<Encoding> get_case_compare_impl(no_case_tag const&)
  75. {
  76. return {};
  77. }
  78. template <typename Encoding, typename Context>
  79. inline decltype(auto) get_case_compare(Context const& context)
  80. {
  81. return get_case_compare_impl<Encoding>(x3::get<no_case_tag>(context));
  82. }
  83. auto const no_case_compare_ = no_case_tag{};
  84. }}}
  85. #endif