regex.hpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <memory> // allocator.
  12. #include <boost/function.hpp>
  13. #include <boost/iostreams/filter/aggregate.hpp>
  14. #include <boost/iostreams/pipeline.hpp>
  15. #include <boost/regex.hpp>
  16. namespace boost { namespace iostreams {
  17. template< typename Ch,
  18. typename Tr = regex_traits<Ch>,
  19. typename Alloc = std::allocator<Ch> >
  20. class basic_regex_filter : public aggregate_filter<Ch, Alloc> {
  21. private:
  22. typedef aggregate_filter<Ch, Alloc> base_type;
  23. public:
  24. typedef typename base_type::char_type char_type;
  25. typedef typename base_type::category category;
  26. typedef std::basic_string<Ch> string_type;
  27. typedef basic_regex<Ch, Tr> regex_type;
  28. typedef regex_constants::match_flag_type flag_type;
  29. typedef match_results<const Ch*> match_type;
  30. typedef function1<string_type, const match_type&> formatter;
  31. basic_regex_filter( const regex_type& re,
  32. const formatter& replace,
  33. flag_type flags = regex_constants::match_default )
  34. : re_(re), replace_(replace), flags_(flags) { }
  35. basic_regex_filter( const regex_type& re,
  36. const string_type& fmt,
  37. flag_type flags = regex_constants::match_default,
  38. flag_type fmt_flags = regex_constants::format_default )
  39. : re_(re), replace_(simple_formatter(fmt, fmt_flags)), flags_(flags) { }
  40. basic_regex_filter( const regex_type& re,
  41. const char_type* fmt,
  42. flag_type flags = regex_constants::match_default,
  43. flag_type fmt_flags = regex_constants::format_default )
  44. : re_(re), replace_(simple_formatter(fmt, fmt_flags)), flags_(flags) { }
  45. private:
  46. typedef typename base_type::vector_type vector_type;
  47. void do_filter(const vector_type& src, vector_type& dest)
  48. {
  49. typedef regex_iterator<const Ch*, Ch, Tr> iterator;
  50. if (src.empty())
  51. return;
  52. iterator first(&src[0], &src[0] + src.size(), re_, flags_);
  53. iterator last;
  54. const Ch* suffix = 0;
  55. for (; first != last; ++first) {
  56. dest.insert( dest.end(),
  57. first->prefix().first,
  58. first->prefix().second );
  59. string_type replacement = replace_(*first);
  60. dest.insert( dest.end(),
  61. replacement.begin(),
  62. replacement.end() );
  63. suffix = first->suffix().first;
  64. }
  65. if (suffix) {
  66. dest.insert(dest.end(), suffix, &src[0] + src.size());
  67. } else {
  68. dest.insert(dest.end(), &src[0], &src[0] + src.size());
  69. }
  70. }
  71. struct simple_formatter {
  72. simple_formatter(const string_type& fmt, flag_type fmt_flags)
  73. : fmt_(fmt), fmt_flags_(fmt_flags) { }
  74. string_type operator() (const match_type& match) const
  75. { return match.format(fmt_, fmt_flags_); }
  76. string_type fmt_;
  77. flag_type fmt_flags_;
  78. };
  79. regex_type re_;
  80. formatter replace_;
  81. flag_type flags_;
  82. };
  83. BOOST_IOSTREAMS_PIPABLE(basic_regex_filter, 3)
  84. typedef basic_regex_filter<char> regex_filter;
  85. typedef basic_regex_filter<wchar_t> wregex_filter;
  86. } } // End namespaces iostreams, boost.
  87. #endif // #ifndef BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED