contains.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file contains.hpp
  9. * \author Andrey Semashev
  10. * \date 30.03.2008
  11. *
  12. * This header contains a predicate for checking if the provided string contains a substring.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_CONTAINS_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_FUNCTIONAL_CONTAINS_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <boost/log/detail/header.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. namespace boost {
  22. BOOST_LOG_OPEN_NAMESPACE
  23. //! The \c contains functor
  24. struct contains_fun
  25. {
  26. typedef bool result_type;
  27. template< typename T, typename U >
  28. bool operator() (T const& left, U const& right) const
  29. {
  30. typedef typename T::const_iterator left_iterator;
  31. typedef typename U::const_iterator right_iterator;
  32. typename U::size_type const right_size = right.size();
  33. if (left.size() >= right_size)
  34. {
  35. const left_iterator search_end = left.end() - right_size + 1;
  36. const right_iterator right_end = right.end();
  37. for (left_iterator it = left.begin(); it != search_end; ++it)
  38. {
  39. left_iterator left_it = it;
  40. right_iterator right_it = right.begin();
  41. for (; right_it != right_end; ++left_it, ++right_it)
  42. {
  43. if (*left_it != *right_it)
  44. break;
  45. }
  46. if (right_it == right_end)
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. };
  53. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  54. } // namespace boost
  55. #include <boost/log/detail/footer.hpp>
  56. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_CONTAINS_HPP_INCLUDED_