algorithm.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. /// @file
  8. /// Addition to STL algorithms
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_UTILS_ALGORITHM_HPP
  11. #define BOOST_TEST_UTILS_ALGORITHM_HPP
  12. // STL
  13. #include <utility>
  14. #include <algorithm> // std::find
  15. #include <functional> // std::bind1st or std::bind
  16. #include <boost/test/detail/suppress_warnings.hpp>
  17. #ifdef BOOST_NO_CXX98_BINDERS
  18. #define BOOST_TEST_BIND1ST(F,A) std::bind( (F), (A), std::placeholders::_1 )
  19. #else
  20. #define BOOST_TEST_BIND1ST(F,A) std::bind1st( (F), (A) )
  21. #endif
  22. //____________________________________________________________________________//
  23. namespace boost {
  24. namespace unit_test {
  25. namespace utils {
  26. /// @brief this algorithm search through two collections for first mismatch position that get returned as a pair
  27. /// of iterators, first pointing to the mismatch position in first collection, second iterator in second one
  28. ///
  29. /// @param first1 - first collection begin iterator
  30. /// @param last1 - first collection end iterator
  31. /// @param first2 - second collection begin iterator
  32. /// @param last2 - second collection end iterator
  33. template <class InputIter1, class InputIter2>
  34. inline std::pair<InputIter1, InputIter2>
  35. mismatch( InputIter1 first1, InputIter1 last1,
  36. InputIter2 first2, InputIter2 last2 )
  37. {
  38. while( first1 != last1 && first2 != last2 && *first1 == *first2 ) {
  39. ++first1;
  40. ++first2;
  41. }
  42. return std::pair<InputIter1, InputIter2>(first1, first2);
  43. }
  44. //____________________________________________________________________________//
  45. /// @brief this algorithm search through two collections for first mismatch position that get returned as a pair
  46. /// of iterators, first pointing to the mismatch position in first collection, second iterator in second one. This algorithms
  47. /// uses supplied predicate for collection elements comparison
  48. ///
  49. /// @param first1 - first collection begin iterator
  50. /// @param last1 - first collection end iterator
  51. /// @param first2 - second collection begin iterator
  52. /// @param last2 - second collection end iterator
  53. /// @param pred - predicate to be used for search
  54. template <class InputIter1, class InputIter2, class Predicate>
  55. inline std::pair<InputIter1, InputIter2>
  56. mismatch( InputIter1 first1, InputIter1 last1,
  57. InputIter2 first2, InputIter2 last2,
  58. Predicate pred )
  59. {
  60. while( first1 != last1 && first2 != last2 && pred( *first1, *first2 ) ) {
  61. ++first1;
  62. ++first2;
  63. }
  64. return std::pair<InputIter1, InputIter2>(first1, first2);
  65. }
  66. //____________________________________________________________________________//
  67. /// @brief this algorithm search through first collection for first element that does not belong a second one
  68. ///
  69. /// @param first1 - first collection begin iterator
  70. /// @param last1 - first collection end iterator
  71. /// @param first2 - second collection begin iterator
  72. /// @param last2 - second collection end iterator
  73. template<class ForwardIterator1, class ForwardIterator2>
  74. inline ForwardIterator1
  75. find_first_not_of( ForwardIterator1 first1, ForwardIterator1 last1,
  76. ForwardIterator2 first2, ForwardIterator2 last2 )
  77. {
  78. while( first1 != last1 ) {
  79. if( std::find( first2, last2, *first1 ) == last2 )
  80. break;
  81. ++first1;
  82. }
  83. return first1;
  84. }
  85. //____________________________________________________________________________//
  86. /// @brief this algorithm search through first collection for first element that does not satisfy binary
  87. /// predicate in conjunction will any element in second collection
  88. ///
  89. /// @param first1 - first collection begin iterator
  90. /// @param last1 - first collection end iterator
  91. /// @param first2 - second collection begin iterator
  92. /// @param last2 - second collection end iterator
  93. /// @param pred - predicate to be used for search
  94. template<class ForwardIterator1, class ForwardIterator2, class Predicate>
  95. inline ForwardIterator1
  96. find_first_not_of( ForwardIterator1 first1, ForwardIterator1 last1,
  97. ForwardIterator2 first2, ForwardIterator2 last2,
  98. Predicate pred )
  99. {
  100. while( first1 != last1 ) {
  101. if( std::find_if( first2, last2, BOOST_TEST_BIND1ST( pred, *first1 ) ) == last2 )
  102. break;
  103. ++first1;
  104. }
  105. return first1;
  106. }
  107. //____________________________________________________________________________//
  108. /// @brief this algorithm search through first collection for last element that belongs to a second one
  109. ///
  110. /// @param first1 - first collection begin iterator
  111. /// @param last1 - first collection end iterator
  112. /// @param first2 - second collection begin iterator
  113. /// @param last2 - second collection end iterator
  114. template<class BidirectionalIterator1, class ForwardIterator2>
  115. inline BidirectionalIterator1
  116. find_last_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
  117. ForwardIterator2 first2, ForwardIterator2 last2 )
  118. {
  119. if( first1 == last1 || first2 == last2 )
  120. return last1;
  121. BidirectionalIterator1 it1 = last1;
  122. while( --it1 != first1 && std::find( first2, last2, *it1 ) == last2 ) {}
  123. return it1 == first1 && std::find( first2, last2, *it1 ) == last2 ? last1 : it1;
  124. }
  125. //____________________________________________________________________________//
  126. /// @brief this algorithm search through first collection for last element that satisfy binary
  127. /// predicate in conjunction will at least one element in second collection
  128. ///
  129. /// @param first1 - first collection begin iterator
  130. /// @param last1 - first collection end iterator
  131. /// @param first2 - second collection begin iterator
  132. /// @param last2 - second collection end iterator
  133. /// @param pred - predicate to be used for search
  134. template<class BidirectionalIterator1, class ForwardIterator2, class Predicate>
  135. inline BidirectionalIterator1
  136. find_last_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
  137. ForwardIterator2 first2, ForwardIterator2 last2,
  138. Predicate pred )
  139. {
  140. if( first1 == last1 || first2 == last2 )
  141. return last1;
  142. BidirectionalIterator1 it1 = last1;
  143. while( --it1 != first1 && std::find_if( first2, last2, BOOST_TEST_BIND1ST( pred, *it1 ) ) == last2 ) {}
  144. return it1 == first1 && std::find_if( first2, last2, BOOST_TEST_BIND1ST( pred, *it1 ) ) == last2 ? last1 : it1;
  145. }
  146. //____________________________________________________________________________//
  147. /// @brief this algorithm search through first collection for last element that does not belong to a second one
  148. ///
  149. /// @param first1 - first collection begin iterator
  150. /// @param last1 - first collection end iterator
  151. /// @param first2 - second collection begin iterator
  152. /// @param last2 - second collection end iterator
  153. template<class BidirectionalIterator1, class ForwardIterator2>
  154. inline BidirectionalIterator1
  155. find_last_not_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
  156. ForwardIterator2 first2, ForwardIterator2 last2 )
  157. {
  158. if( first1 == last1 || first2 == last2 )
  159. return last1;
  160. BidirectionalIterator1 it1 = last1;
  161. while( --it1 != first1 && std::find( first2, last2, *it1 ) != last2 ) {}
  162. return it1 == first1 && std::find( first2, last2, *it1 ) != last2 ? last1 : it1;
  163. }
  164. //____________________________________________________________________________//
  165. /// @brief this algorithm search through first collection for last element that does not satisfy binary
  166. /// predicate in conjunction will any element in second collection
  167. ///
  168. /// @param first1 - first collection begin iterator
  169. /// @param last1 - first collection end iterator
  170. /// @param first2 - second collection begin iterator
  171. /// @param last2 - second collection end iterator
  172. /// @param pred - predicate to be used for search
  173. template<class BidirectionalIterator1, class ForwardIterator2, class Predicate>
  174. inline BidirectionalIterator1
  175. find_last_not_of( BidirectionalIterator1 first1, BidirectionalIterator1 last1,
  176. ForwardIterator2 first2, ForwardIterator2 last2,
  177. Predicate pred )
  178. {
  179. if( first1 == last1 || first2 == last2 )
  180. return last1;
  181. BidirectionalIterator1 it1 = last1;
  182. while( --it1 != first1 && std::find_if( first2, last2, BOOST_TEST_BIND1ST( pred, *it1 ) ) != last2 ) {}
  183. return it1 == first1 && std::find_if( first2, last2, BOOST_TEST_BIND1ST( pred, *it1 ) ) == last2 ? last1 : it1;
  184. }
  185. //____________________________________________________________________________//
  186. /// @brief This algorithm replaces all occurrences of a set of substrings by another substrings
  187. ///
  188. /// @param str - string of operation
  189. /// @param first1 - iterator to the beginning of the substrings to replace
  190. /// @param last1 - iterator to the end of the substrings to replace
  191. /// @param first2 - iterator to the beginning of the substrings to replace with
  192. /// @param last2 - iterator to the end of the substrings to replace with
  193. template<class StringClass, class ForwardIterator>
  194. inline StringClass
  195. replace_all_occurrences_of( StringClass str,
  196. ForwardIterator first1, ForwardIterator last1,
  197. ForwardIterator first2, ForwardIterator last2)
  198. {
  199. for(; first1 != last1 && first2 != last2; ++first1, ++first2) {
  200. std::size_t found = str.find( *first1 );
  201. while( found != StringClass::npos ) {
  202. str.replace(found, first1->size(), *first2 );
  203. found = str.find( *first1, found + first2->size() );
  204. }
  205. }
  206. return str;
  207. }
  208. /// @brief This algorithm replaces all occurrences of a string with basic wildcards
  209. /// with another (optionally containing wildcards as well).
  210. ///
  211. /// @param str - string to transform
  212. /// @param it_string_to_find - iterator to the beginning of the substrings to replace
  213. /// @param it_string_to_find_end - iterator to the end of the substrings to replace
  214. /// @param it_string_to_replace - iterator to the beginning of the substrings to replace with
  215. /// @param it_string_to_replace_end - iterator to the end of the substrings to replace with
  216. ///
  217. /// The wildcard is the symbol '*'. Only a unique wildcard per string is supported. The replacement
  218. /// string may also contain a wildcard, in which case it is considered as a placeholder to the content
  219. /// of the wildcard in the source string.
  220. /// Example:
  221. /// - In order to replace the occurrences of @c 'time=\"some-variable-value\"' to a constant string,
  222. /// one may use @c 'time=\"*\"' as the string to search for, and 'time=\"0.0\"' as the replacement string.
  223. /// - In order to replace the occurrences of 'file.cpp(XX)' per 'file.cpp:XX', where XX is a variable to keep,
  224. /// on may use @c 'file.cpp(*)' as the string to search for, and 'file.cpp:*' as the replacement string.
  225. template<class StringClass, class ForwardIterator>
  226. inline StringClass
  227. replace_all_occurrences_with_wildcards(
  228. StringClass str,
  229. ForwardIterator it_string_to_find, ForwardIterator it_string_to_find_end,
  230. ForwardIterator it_string_to_replace, ForwardIterator it_string_to_replace_end)
  231. {
  232. for(; it_string_to_find != it_string_to_find_end && it_string_to_replace != it_string_to_replace_end;
  233. ++it_string_to_find, ++ it_string_to_replace) {
  234. std::size_t wildcard_pos = it_string_to_find->find("*");
  235. if(wildcard_pos == StringClass::npos) {
  236. ForwardIterator it_to_find_current_end(it_string_to_find);
  237. ForwardIterator it_to_replace_current_end(it_string_to_replace);
  238. str = replace_all_occurrences_of(
  239. str,
  240. it_string_to_find, ++it_to_find_current_end,
  241. it_string_to_replace, ++it_to_replace_current_end);
  242. continue;
  243. }
  244. std::size_t wildcard_pos_replace = it_string_to_replace->find("*");
  245. std::size_t found_begin = str.find( it_string_to_find->substr(0, wildcard_pos) );
  246. while( found_begin != StringClass::npos ) {
  247. std::size_t found_end = str.find(it_string_to_find->substr(wildcard_pos+1), found_begin + wildcard_pos + 1); // to simplify
  248. if( found_end != StringClass::npos ) {
  249. if( wildcard_pos_replace == StringClass::npos ) {
  250. StringClass replace_content = *it_string_to_replace;
  251. str.replace(
  252. found_begin,
  253. found_end + (it_string_to_find->size() - wildcard_pos - 1 ) - found_begin,
  254. replace_content);
  255. } else {
  256. StringClass replace_content =
  257. it_string_to_replace->substr(0, wildcard_pos_replace)
  258. + str.substr(found_begin + wildcard_pos,
  259. found_end - found_begin - wildcard_pos)
  260. + it_string_to_replace->substr(wildcard_pos_replace+1) ;
  261. str.replace(
  262. found_begin,
  263. found_end + (it_string_to_find->size() - wildcard_pos - 1 ) - found_begin,
  264. replace_content);
  265. }
  266. }
  267. // may adapt the restart to the replacement and be more efficient
  268. found_begin = str.find( it_string_to_find->substr(0, wildcard_pos), found_begin + 1 );
  269. }
  270. }
  271. return str;
  272. }
  273. } // namespace utils
  274. } // namespace unit_test
  275. } // namespace boost
  276. #include <boost/test/detail/enable_warnings.hpp>
  277. #endif // BOOST_TEST_UTILS_ALGORITHM_HPP