find_format_all.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Boost string_algo library find_format_all.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_ALL_DETAIL_HPP
  9. #define BOOST_STRING_FIND_FORMAT_ALL_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/value_type.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_all_copy (iterator variant) implementation ---------------------------//
  20. template<
  21. typename OutputIteratorT,
  22. typename InputT,
  23. typename FinderT,
  24. typename FormatterT,
  25. typename FindResultT,
  26. typename FormatResultT >
  27. inline OutputIteratorT find_format_all_copy_impl2(
  28. OutputIteratorT Output,
  29. const InputT& Input,
  30. FinderT Finder,
  31. FormatterT Formatter,
  32. const FindResultT& FindResult,
  33. const FormatResultT& FormatResult )
  34. {
  35. typedef BOOST_STRING_TYPENAME
  36. range_const_iterator<InputT>::type input_iterator_type;
  37. typedef find_format_store<
  38. input_iterator_type,
  39. FormatterT,
  40. FormatResultT > store_type;
  41. // Create store for the find result
  42. store_type M( FindResult, FormatResult, Formatter );
  43. // Initialize last match
  44. input_iterator_type LastMatch=::boost::begin(Input);
  45. // Iterate through all matches
  46. while( M )
  47. {
  48. // Copy the beginning of the sequence
  49. Output = std::copy( LastMatch, M.begin(), Output );
  50. // Copy formatted result
  51. Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
  52. // Proceed to the next match
  53. LastMatch=M.end();
  54. M=Finder( LastMatch, ::boost::end(Input) );
  55. }
  56. // Copy the rest of the sequence
  57. Output = std::copy( LastMatch, ::boost::end(Input), Output );
  58. return Output;
  59. }
  60. template<
  61. typename OutputIteratorT,
  62. typename InputT,
  63. typename FinderT,
  64. typename FormatterT,
  65. typename FindResultT >
  66. inline OutputIteratorT find_format_all_copy_impl(
  67. OutputIteratorT Output,
  68. const InputT& Input,
  69. FinderT Finder,
  70. FormatterT Formatter,
  71. const FindResultT& FindResult )
  72. {
  73. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  74. return ::boost::algorithm::detail::find_format_all_copy_impl2(
  75. Output,
  76. Input,
  77. Finder,
  78. Formatter,
  79. FindResult,
  80. Formatter(FindResult) );
  81. } else {
  82. return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
  83. }
  84. }
  85. // find_format_all_copy implementation ----------------------------------------------//
  86. template<
  87. typename InputT,
  88. typename FinderT,
  89. typename FormatterT,
  90. typename FindResultT,
  91. typename FormatResultT >
  92. inline InputT find_format_all_copy_impl2(
  93. const InputT& Input,
  94. FinderT Finder,
  95. FormatterT Formatter,
  96. const FindResultT& FindResult,
  97. const FormatResultT& FormatResult)
  98. {
  99. typedef BOOST_STRING_TYPENAME
  100. range_const_iterator<InputT>::type input_iterator_type;
  101. typedef find_format_store<
  102. input_iterator_type,
  103. FormatterT,
  104. FormatResultT > store_type;
  105. // Create store for the find result
  106. store_type M( FindResult, FormatResult, Formatter );
  107. // Initialize last match
  108. input_iterator_type LastMatch=::boost::begin(Input);
  109. // Output temporary
  110. InputT Output;
  111. // Iterate through all matches
  112. while( M )
  113. {
  114. // Copy the beginning of the sequence
  115. boost::algorithm::detail::insert( Output, ::boost::end(Output), LastMatch, M.begin() );
  116. // Copy formatted result
  117. boost::algorithm::detail::insert( Output, ::boost::end(Output), M.format_result() );
  118. // Proceed to the next match
  119. LastMatch=M.end();
  120. M=Finder( LastMatch, ::boost::end(Input) );
  121. }
  122. // Copy the rest of the sequence
  123. ::boost::algorithm::detail::insert( Output, ::boost::end(Output), LastMatch, ::boost::end(Input) );
  124. return Output;
  125. }
  126. template<
  127. typename InputT,
  128. typename FinderT,
  129. typename FormatterT,
  130. typename FindResultT >
  131. inline InputT find_format_all_copy_impl(
  132. const InputT& Input,
  133. FinderT Finder,
  134. FormatterT Formatter,
  135. const FindResultT& FindResult)
  136. {
  137. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  138. return ::boost::algorithm::detail::find_format_all_copy_impl2(
  139. Input,
  140. Finder,
  141. Formatter,
  142. FindResult,
  143. Formatter(FindResult) );
  144. } else {
  145. return Input;
  146. }
  147. }
  148. // find_format_all implementation ------------------------------------------------//
  149. template<
  150. typename InputT,
  151. typename FinderT,
  152. typename FormatterT,
  153. typename FindResultT,
  154. typename FormatResultT >
  155. inline void find_format_all_impl2(
  156. InputT& Input,
  157. FinderT Finder,
  158. FormatterT Formatter,
  159. FindResultT FindResult,
  160. FormatResultT FormatResult)
  161. {
  162. typedef BOOST_STRING_TYPENAME
  163. range_iterator<InputT>::type input_iterator_type;
  164. typedef find_format_store<
  165. input_iterator_type,
  166. FormatterT,
  167. FormatResultT > store_type;
  168. // Create store for the find result
  169. store_type M( FindResult, FormatResult, Formatter );
  170. // Instantiate replacement storage
  171. std::deque<
  172. BOOST_STRING_TYPENAME range_value<InputT>::type> Storage;
  173. // Initialize replacement iterators
  174. input_iterator_type InsertIt=::boost::begin(Input);
  175. input_iterator_type SearchIt=::boost::begin(Input);
  176. while( M )
  177. {
  178. // process the segment
  179. InsertIt=process_segment(
  180. Storage,
  181. Input,
  182. InsertIt,
  183. SearchIt,
  184. M.begin() );
  185. // Adjust search iterator
  186. SearchIt=M.end();
  187. // Copy formatted replace to the storage
  188. ::boost::algorithm::detail::copy_to_storage( Storage, M.format_result() );
  189. // Find range for a next match
  190. M=Finder( SearchIt, ::boost::end(Input) );
  191. }
  192. // process the last segment
  193. InsertIt=::boost::algorithm::detail::process_segment(
  194. Storage,
  195. Input,
  196. InsertIt,
  197. SearchIt,
  198. ::boost::end(Input) );
  199. if ( Storage.empty() )
  200. {
  201. // Truncate input
  202. ::boost::algorithm::detail::erase( Input, InsertIt, ::boost::end(Input) );
  203. }
  204. else
  205. {
  206. // Copy remaining data to the end of input
  207. ::boost::algorithm::detail::insert( Input, ::boost::end(Input), Storage.begin(), Storage.end() );
  208. }
  209. }
  210. template<
  211. typename InputT,
  212. typename FinderT,
  213. typename FormatterT,
  214. typename FindResultT >
  215. inline void find_format_all_impl(
  216. InputT& Input,
  217. FinderT Finder,
  218. FormatterT Formatter,
  219. FindResultT FindResult)
  220. {
  221. if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
  222. ::boost::algorithm::detail::find_format_all_impl2(
  223. Input,
  224. Finder,
  225. Formatter,
  226. FindResult,
  227. Formatter(FindResult) );
  228. }
  229. }
  230. } // namespace detail
  231. } // namespace algorithm
  232. } // namespace boost
  233. #endif // BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP