find_format.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Boost string_algo library find_format.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2003.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/ for updates, documentation, and revision history.
  8. #ifndef BOOST_STRING_FIND_FORMAT_DETAIL_HPP
  9. #define BOOST_STRING_FIND_FORMAT_DETAIL_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/range/iterator_range_core.hpp>
  12. #include <boost/range/const_iterator.hpp>
  13. #include <boost/range/iterator.hpp>
  14. #include <boost/algorithm/string/detail/find_format_store.hpp>
  15. #include <boost/algorithm/string/detail/replace_storage.hpp>
  16. namespace boost {
  17. namespace algorithm {
  18. namespace detail {
  19. // find_format_copy (iterator variant) implementation -------------------------------//
  20. template<
  21. typename OutputIteratorT,
  22. typename InputT,
  23. typename FormatterT,
  24. typename FindResultT,
  25. typename FormatResultT >
  26. inline OutputIteratorT find_format_copy_impl2(
  27. OutputIteratorT Output,
  28. const InputT& Input,
  29. FormatterT Formatter,
  30. const FindResultT& FindResult,
  31. const FormatResultT& FormatResult )
  32. {
  33. typedef find_format_store<
  34. BOOST_STRING_TYPENAME
  35. range_const_iterator<InputT>::type,
  36. FormatterT,
  37. FormatResultT > store_type;
  38. // Create store for the find result
  39. store_type M( FindResult, FormatResult, Formatter );
  40. if ( !M )
  41. {
  42. // Match not found - return original sequence
  43. Output = std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
  44. return Output;
  45. }
  46. // Copy the beginning of the sequence
  47. Output = std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
  48. // Format find result
  49. // Copy formatted result
  50. Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
  51. // Copy the rest of the sequence
  52. Output = std::copy( M.end(), ::boost::end(Input), Output );
  53. return Output;
  54. }
  55. template<
  56. typename OutputIteratorT,
  57. typename InputT,
  58. typename FormatterT,
  59. typename FindResultT >
  60. inline OutputIteratorT find_format_copy_impl(
  61. OutputIteratorT Output,
  62. const InputT& Input,
  63. FormatterT Formatter,
  64. const FindResultT& FindResult )
  65. {
  66. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  67. return ::boost::algorithm::detail::find_format_copy_impl2(
  68. Output,
  69. Input,
  70. Formatter,
  71. FindResult,
  72. Formatter(FindResult) );
  73. } else {
  74. return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
  75. }
  76. }
  77. // find_format_copy implementation --------------------------------------------------//
  78. template<
  79. typename InputT,
  80. typename FormatterT,
  81. typename FindResultT,
  82. typename FormatResultT >
  83. inline InputT find_format_copy_impl2(
  84. const InputT& Input,
  85. FormatterT Formatter,
  86. const FindResultT& FindResult,
  87. const FormatResultT& FormatResult)
  88. {
  89. typedef find_format_store<
  90. BOOST_STRING_TYPENAME
  91. range_const_iterator<InputT>::type,
  92. FormatterT,
  93. FormatResultT > store_type;
  94. // Create store for the find result
  95. store_type M( FindResult, FormatResult, Formatter );
  96. if ( !M )
  97. {
  98. // Match not found - return original sequence
  99. return InputT( Input );
  100. }
  101. InputT Output;
  102. // Copy the beginning of the sequence
  103. boost::algorithm::detail::insert( Output, ::boost::end(Output), ::boost::begin(Input), M.begin() );
  104. // Copy formatted result
  105. boost::algorithm::detail::insert( Output, ::boost::end(Output), M.format_result() );
  106. // Copy the rest of the sequence
  107. boost::algorithm::detail::insert( Output, ::boost::end(Output), M.end(), ::boost::end(Input) );
  108. return Output;
  109. }
  110. template<
  111. typename InputT,
  112. typename FormatterT,
  113. typename FindResultT >
  114. inline InputT find_format_copy_impl(
  115. const InputT& Input,
  116. FormatterT Formatter,
  117. const FindResultT& FindResult)
  118. {
  119. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  120. return ::boost::algorithm::detail::find_format_copy_impl2(
  121. Input,
  122. Formatter,
  123. FindResult,
  124. Formatter(FindResult) );
  125. } else {
  126. return Input;
  127. }
  128. }
  129. // replace implementation ----------------------------------------------------//
  130. template<
  131. typename InputT,
  132. typename FormatterT,
  133. typename FindResultT,
  134. typename FormatResultT >
  135. inline void find_format_impl2(
  136. InputT& Input,
  137. FormatterT Formatter,
  138. const FindResultT& FindResult,
  139. const FormatResultT& FormatResult)
  140. {
  141. typedef find_format_store<
  142. BOOST_STRING_TYPENAME
  143. range_iterator<InputT>::type,
  144. FormatterT,
  145. FormatResultT > store_type;
  146. // Create store for the find result
  147. store_type M( FindResult, FormatResult, Formatter );
  148. if ( !M )
  149. {
  150. // Search not found - return original sequence
  151. return;
  152. }
  153. // Replace match
  154. ::boost::algorithm::detail::replace( Input, M.begin(), M.end(), M.format_result() );
  155. }
  156. template<
  157. typename InputT,
  158. typename FormatterT,
  159. typename FindResultT >
  160. inline void find_format_impl(
  161. InputT& Input,
  162. FormatterT Formatter,
  163. const FindResultT& FindResult)
  164. {
  165. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  166. ::boost::algorithm::detail::find_format_impl2(
  167. Input,
  168. Formatter,
  169. FindResult,
  170. Formatter(FindResult) );
  171. }
  172. }
  173. } // namespace detail
  174. } // namespace algorithm
  175. } // namespace boost
  176. #endif // BOOST_STRING_FIND_FORMAT_DETAIL_HPP