string_generate.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 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 Char, typename Traits
  70. , typename Allocator>
  71. inline bool string_generate(OutputIterator& sink
  72. , std::basic_string<Char, Traits, Allocator> const& str)
  73. {
  74. return string_generate(sink, str.c_str(), pass_through_filter());
  75. }
  76. template <typename OutputIterator, typename Container>
  77. inline bool string_generate(OutputIterator& sink
  78. , Container const& c)
  79. {
  80. return string_generate(sink, c, pass_through_filter());
  81. }
  82. ///////////////////////////////////////////////////////////////////////////
  83. // generate a string given by a pointer, converting according using a
  84. // given character class and case tag
  85. template <typename OutputIterator, typename Char, typename CharEncoding
  86. , typename Tag>
  87. inline bool string_generate(OutputIterator& sink
  88. , Char const* str
  89. , CharEncoding, Tag)
  90. {
  91. return string_generate(sink, str, encoding_filter<CharEncoding, Tag>());
  92. }
  93. template <typename OutputIterator, typename Char
  94. , typename CharEncoding, typename Tag
  95. , typename Traits, typename Allocator>
  96. inline bool string_generate(OutputIterator& sink
  97. , std::basic_string<Char, Traits, Allocator> const& str
  98. , CharEncoding, Tag)
  99. {
  100. return string_generate(sink, str.c_str()
  101. , encoding_filter<CharEncoding, Tag>());
  102. }
  103. template <typename OutputIterator, typename Container
  104. , typename CharEncoding, typename Tag>
  105. inline bool
  106. string_generate(OutputIterator& sink
  107. , Container const& c
  108. , CharEncoding, Tag)
  109. {
  110. return string_generate(sink, c, encoding_filter<CharEncoding, Tag>());
  111. }
  112. ///////////////////////////////////////////////////////////////////////////
  113. template <typename OutputIterator, typename Char>
  114. inline bool string_generate(OutputIterator& sink
  115. , Char const* str
  116. , unused_type, unused_type)
  117. {
  118. return string_generate(sink, str, pass_through_filter());
  119. }
  120. template <typename OutputIterator, typename Char, typename Traits
  121. , typename Allocator>
  122. inline bool string_generate(OutputIterator& sink
  123. , std::basic_string<Char, Traits, Allocator> const& str
  124. , unused_type, unused_type)
  125. {
  126. return string_generate(sink, str.c_str(), pass_through_filter());
  127. }
  128. template <typename OutputIterator, typename Container>
  129. inline bool string_generate(OutputIterator& sink
  130. , Container const& c
  131. , unused_type, unused_type)
  132. {
  133. return string_generate(sink, c, pass_through_filter());
  134. }
  135. }}}}
  136. #endif