find_format_all.hpp 10 KB

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