algorithm.hpp 13 KB

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