no_case_string_parse.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_NO_CASE_STRING_PARSE_APR_18_2014_1125PM)
  7. #define BOOST_SPIRIT_X3_NO_CASE_STRING_PARSE_APR_18_2014_1125PM
  8. #include <boost/spirit/home/x3/char/char.hpp>
  9. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  10. namespace boost { namespace spirit { namespace x3 { namespace detail
  11. {
  12. template <typename Char, typename Encoding>
  13. struct no_case_string
  14. {
  15. typedef std::basic_string< Char > string_type;
  16. typedef typename string_type::const_iterator const_iterator;
  17. no_case_string(char_type const* str)
  18. : lower(str)
  19. , upper(str)
  20. {
  21. typename string_type::iterator loi = lower.begin();
  22. typename string_type::iterator upi = upper.begin();
  23. typedef typename Encoding::char_type encoded_char_type;
  24. Encoding encoding;
  25. for (; loi != lower.end(); ++loi, ++upi)
  26. {
  27. *loi = static_cast<char_type>(encoding.tolower(encoded_char_type(*loi)));
  28. *upi = static_cast<char_type>(encoding.toupper(encoded_char_type(*upi)));
  29. }
  30. }
  31. string_type lower;
  32. string_type upper;
  33. };
  34. template <typename String, typename Iterator, typename Attribute>
  35. inline bool no_case_string_parse(
  36. String const& str
  37. , Iterator& first, Iterator const& last, Attribute& attr)
  38. {
  39. typename String::const_iterator uc_i = str.upper.begin();
  40. typename String::const_iterator uc_last = str.upper.end();
  41. typename String::const_iterator lc_i = str.lower.begin();
  42. Iterator i = first;
  43. for (; uc_i != uc_last; ++uc_i, ++lc_i, ++i)
  44. if (i == last || ((*uc_i != *i) && (*lc_i != *i)))
  45. return false;
  46. x3::traits::move_to(first, i, attr);
  47. first = i;
  48. return true;
  49. }
  50. }}}}
  51. #endif