string_generate.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  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. #if !defined(BOOST_SPIRIT_KARMA_STRING_GENERATE_FEB_23_2007_1232PM)
  6. #define BOOST_SPIRIT_KARMA_STRING_GENERATE_FEB_23_2007_1232PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <string>
  11. #include <boost/spirit/home/support/char_class.hpp>
  12. #include <boost/spirit/home/karma/detail/generate_to.hpp>
  13. #include <boost/range/const_iterator.hpp>
  14. namespace boost { namespace spirit { namespace karma { namespace detail
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // pass through character transformation
  18. struct pass_through_filter
  19. {
  20. template <typename Char>
  21. Char operator()(Char ch) const
  22. {
  23. return ch;
  24. }
  25. };
  26. template <typename CharEncoding, typename Tag>
  27. struct encoding_filter
  28. {
  29. template <typename Char>
  30. Char operator()(Char ch) const
  31. {
  32. return spirit::char_class::convert<CharEncoding>::to(Tag(), ch);
  33. }
  34. };
  35. ///////////////////////////////////////////////////////////////////////////
  36. // generate a string given by a std::string, applying the given filter
  37. template <typename OutputIterator, typename Char, typename Filter>
  38. inline bool string_generate(OutputIterator& sink, Char const* str
  39. , Filter filter)
  40. {
  41. for (Char ch = *str; ch != 0; ch = *++str)
  42. {
  43. *sink = filter(ch);
  44. ++sink;
  45. }
  46. return detail::sink_is_good(sink);
  47. }
  48. template <typename OutputIterator, typename Container, typename Filter>
  49. inline bool string_generate(OutputIterator& sink
  50. , Container const& c, Filter filter)
  51. {
  52. typedef typename traits::container_iterator<Container const>::type
  53. iterator;
  54. const iterator end = boost::end(c);
  55. for (iterator it = boost::begin(c); it != end; ++it)
  56. {
  57. *sink = filter(*it);
  58. ++sink;
  59. }
  60. return detail::sink_is_good(sink);
  61. }
  62. ///////////////////////////////////////////////////////////////////////////
  63. // generate a string without any transformation
  64. template <typename OutputIterator, typename Char>
  65. inline bool string_generate(OutputIterator& sink, Char const* str)
  66. {
  67. return string_generate(sink, str, pass_through_filter());
  68. }
  69. template <typename OutputIterator, typename Container>
  70. inline bool string_generate(OutputIterator& sink
  71. , Container const& c)
  72. {
  73. return string_generate(sink, c, pass_through_filter());
  74. }
  75. ///////////////////////////////////////////////////////////////////////////
  76. // generate a string given by a pointer, converting according using a
  77. // given character class and case tag
  78. template <typename OutputIterator, typename Char, typename CharEncoding
  79. , typename Tag>
  80. inline bool string_generate(OutputIterator& sink
  81. , Char const* str
  82. , CharEncoding, Tag)
  83. {
  84. return string_generate(sink, str, encoding_filter<CharEncoding, Tag>());
  85. }
  86. template <typename OutputIterator, typename Container
  87. , typename CharEncoding, typename Tag>
  88. inline bool
  89. string_generate(OutputIterator& sink
  90. , Container const& c
  91. , CharEncoding, Tag)
  92. {
  93. return string_generate(sink, c, encoding_filter<CharEncoding, Tag>());
  94. }
  95. ///////////////////////////////////////////////////////////////////////////
  96. template <typename OutputIterator, typename Char>
  97. inline bool string_generate(OutputIterator& sink
  98. , Char const* str
  99. , unused_type, unused_type)
  100. {
  101. return string_generate(sink, str);
  102. }
  103. template <typename OutputIterator, typename Container>
  104. inline bool string_generate(OutputIterator& sink
  105. , Container const& c
  106. , unused_type, unused_type)
  107. {
  108. return string_generate(sink, c);
  109. }
  110. }}}}
  111. #endif