find_format.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.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. inline OutputIteratorT find_format_copy_impl(
  26. OutputIteratorT Output,
  27. const InputT& Input,
  28. FormatterT Formatter,
  29. const FindResultT& FindResult )
  30. {
  31. return find_format_copy_impl2(
  32. Output,
  33. Input,
  34. Formatter,
  35. FindResult,
  36. Formatter(FindResult) );
  37. }
  38. template<
  39. typename OutputIteratorT,
  40. typename InputT,
  41. typename FormatterT,
  42. typename FindResultT,
  43. typename FormatResultT >
  44. inline OutputIteratorT find_format_copy_impl2(
  45. OutputIteratorT Output,
  46. const InputT& Input,
  47. FormatterT Formatter,
  48. const FindResultT& FindResult,
  49. const FormatResultT& FormatResult )
  50. {
  51. typedef find_format_store<
  52. BOOST_STRING_TYPENAME
  53. range_const_iterator<InputT>::type,
  54. FormatterT,
  55. FormatResultT > store_type;
  56. // Create store for the find result
  57. store_type M( FindResult, FormatResult, Formatter );
  58. if ( !M )
  59. {
  60. // Match not found - return original sequence
  61. std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
  62. return Output;
  63. }
  64. // Copy the beginning of the sequence
  65. std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
  66. // Format find result
  67. // Copy formated result
  68. std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
  69. // Copy the rest of the sequence
  70. std::copy( M.end(), ::boost::end(Input), Output );
  71. return Output;
  72. }
  73. // find_format_copy implementation --------------------------------------------------//
  74. template<
  75. typename InputT,
  76. typename FormatterT,
  77. typename FindResultT >
  78. inline InputT find_format_copy_impl(
  79. const InputT& Input,
  80. FormatterT Formatter,
  81. const FindResultT& FindResult)
  82. {
  83. return find_format_copy_impl2(
  84. Input,
  85. Formatter,
  86. FindResult,
  87. Formatter(FindResult) );
  88. }
  89. template<
  90. typename InputT,
  91. typename FormatterT,
  92. typename FindResultT,
  93. typename FormatResultT >
  94. inline InputT find_format_copy_impl2(
  95. const InputT& Input,
  96. FormatterT Formatter,
  97. const FindResultT& FindResult,
  98. const FormatResultT& FormatResult)
  99. {
  100. typedef find_format_store<
  101. BOOST_STRING_TYPENAME
  102. range_const_iterator<InputT>::type,
  103. FormatterT,
  104. FormatResultT > store_type;
  105. // Create store for the find result
  106. store_type M( FindResult, FormatResult, Formatter );
  107. if ( !M )
  108. {
  109. // Match not found - return original sequence
  110. return InputT( Input );
  111. }
  112. InputT Output;
  113. // Copy the beginning of the sequence
  114. insert( Output, ::boost::end(Output), ::boost::begin(Input), M.begin() );
  115. // Copy formated result
  116. insert( Output, ::boost::end(Output), M.format_result() );
  117. // Copy the rest of the sequence
  118. insert( Output, ::boost::end(Output), M.end(), ::boost::end(Input) );
  119. return Output;
  120. }
  121. // replace implementation ----------------------------------------------------//
  122. template<
  123. typename InputT,
  124. typename FormatterT,
  125. typename FindResultT >
  126. inline void find_format_impl(
  127. InputT& Input,
  128. FormatterT Formatter,
  129. const FindResultT& FindResult)
  130. {
  131. find_format_impl2(
  132. Input,
  133. Formatter,
  134. FindResult,
  135. Formatter(FindResult) );
  136. }
  137. template<
  138. typename InputT,
  139. typename FormatterT,
  140. typename FindResultT,
  141. typename FormatResultT >
  142. inline void find_format_impl2(
  143. InputT& Input,
  144. FormatterT Formatter,
  145. const FindResultT& FindResult,
  146. const FormatResultT& FormatResult)
  147. {
  148. typedef find_format_store<
  149. BOOST_STRING_TYPENAME
  150. range_iterator<InputT>::type,
  151. FormatterT,
  152. FormatResultT > store_type;
  153. // Create store for the find result
  154. store_type M( FindResult, FormatResult, Formatter );
  155. if ( !M )
  156. {
  157. // Search not found - return original sequence
  158. return;
  159. }
  160. // Replace match
  161. replace( Input, M.begin(), M.end(), M.format_result() );
  162. }
  163. } // namespace detail
  164. } // namespace algorithm
  165. } // namespace boost
  166. #endif // BOOST_STRING_FIND_FORMAT_DETAIL_HPP