regex.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 support/regex.hpp
  9. * \author Andrey Semashev
  10. * \date 18.07.2009
  11. *
  12. * This header enables Boost.Regex support for Boost.Log.
  13. */
  14. #ifndef BOOST_LOG_SUPPORT_REGEX_HPP_INCLUDED_
  15. #define BOOST_LOG_SUPPORT_REGEX_HPP_INCLUDED_
  16. #include <string>
  17. #include <boost/regex.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/utility/functional/matches.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. namespace aux {
  27. //! This tag type is used if an expression is recognized as a Boost.Regex expression
  28. struct boost_regex_expression_tag;
  29. //! The metafunction detects the matching expression kind and returns a tag that is used to specialize \c match_traits
  30. template< typename CharT, typename TraitsT >
  31. struct matching_expression_kind< boost::basic_regex< CharT, TraitsT > >
  32. {
  33. typedef boost_regex_expression_tag type;
  34. };
  35. //! The matching function implementation
  36. template< typename ExpressionT >
  37. struct match_traits< ExpressionT, boost_regex_expression_tag >
  38. {
  39. typedef ExpressionT compiled_type;
  40. static compiled_type compile(ExpressionT const& expr) { return expr; }
  41. template< typename StringT, typename CharT, typename TraitsT >
  42. static bool matches(StringT const& str, boost::basic_regex< CharT, TraitsT > const& expr, boost::regex_constants::match_flag_type flags = boost::regex_constants::match_default)
  43. {
  44. return boost::regex_match(str.begin(), str.end(), expr, flags);
  45. }
  46. template< typename CharT, typename StringTraitsT, typename AllocatorT, typename ReTraitsT >
  47. static bool matches(
  48. std::basic_string< CharT, StringTraitsT, AllocatorT > const& str,
  49. boost::basic_regex< CharT, ReTraitsT > const& expr,
  50. boost::regex_constants::match_flag_type flags = boost::regex_constants::match_default)
  51. {
  52. const CharT* p = str.c_str();
  53. return boost::regex_match(p, p + str.size(), expr, flags);
  54. }
  55. };
  56. } // namespace aux
  57. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  58. } // namespace boost
  59. #include <boost/log/detail/footer.hpp>
  60. #endif // BOOST_LOG_SUPPORT_REGEX_HPP_INCLUDED_