6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Overloads and helpers for using MFC/ATL string types with Boost.Regex.
19 #ifndef BOOST_REGEX_MFC_HPP
20 #define BOOST_REGEX_MFC_HPP
22 #include <atlsimpstr.h>
23 #include <boost/regex.hpp>
28 // define the types used for TCHAR's:
29 typedef basic_regex<TCHAR> tregex;
30 typedef match_results<TCHAR const*> tmatch;
31 typedef regex_iterator<TCHAR const*> tregex_iterator;
32 typedef regex_token_iterator<TCHAR const*> tregex_token_iterator;
35 #define SIMPLE_STRING_PARAM class B, bool b
36 #define SIMPLE_STRING_ARG_LIST B, b
39 // define regex creation functions:
41 template <class B, bool b>
43 make_regex(const ATL::CSimpleStringT<B, b>& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal)
45 basic_regex<B> result(s.GetString(), s.GetString() + s.GetLength(), f);
49 // regex_match overloads:
51 template <class B, bool b, class A, class T>
52 inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
53 match_results<const B*, A>& what,
54 const basic_regex<B, T>& e,
55 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
57 return ::boost::regex_match(s.GetString(),
58 s.GetString() + s.GetLength(),
64 template <class B, bool b, class T>
65 inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
66 const basic_regex<B, T>& e,
67 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
69 return ::boost::regex_match(s.GetString(),
70 s.GetString() + s.GetLength(),
75 // regex_search overloads:
77 template <class B, bool b, class A, class T>
78 inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
79 match_results<const B*, A>& what,
80 const basic_regex<B, T>& e,
81 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
83 return ::boost::regex_search(s.GetString(),
84 s.GetString() + s.GetLength(),
90 template <class B, bool b, class T>
91 inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
92 const basic_regex<B, T>& e,
93 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
95 return ::boost::regex_search(s.GetString(),
96 s.GetString() + s.GetLength(),
101 // regex_iterator creation:
103 template <class B, bool b>
104 inline regex_iterator<B const*>
105 make_regex_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
107 regex_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, f);
111 template <class B, bool b>
112 inline regex_token_iterator<B const*>
113 make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, int sub = 0, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
115 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);
119 template <class B, bool b>
120 inline regex_token_iterator<B const*>
121 make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, const std::vector<int>& subs, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
123 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
127 template <class B, bool b, std::size_t N>
128 inline regex_token_iterator<B const*>
129 make_regex_token_iterator(const ATL::CSimpleStringT<B, b>& s, const basic_regex<B>& e, const int (& subs)[N], ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
131 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
135 template <class OutputIterator, class BidirectionalIterator, class traits,
137 OutputIterator regex_replace(OutputIterator out,
138 BidirectionalIterator first,
139 BidirectionalIterator last,
140 const basic_regex<B, traits>& e,
141 const ATL::CSimpleStringT<B, b>& fmt,
142 match_flag_type flags = match_default)
144 return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);
147 namespace BOOST_REGEX_DETAIL_NS{
149 template <class B, bool b>
150 class mfc_string_out_iterator
152 ATL::CSimpleStringT<B, b>* out;
154 mfc_string_out_iterator(ATL::CSimpleStringT<B, b>& s) : out(&s) {}
155 mfc_string_out_iterator& operator++() { return *this; }
156 mfc_string_out_iterator& operator++(int) { return *this; }
157 mfc_string_out_iterator& operator*() { return *this; }
158 mfc_string_out_iterator& operator=(B v)
163 typedef std::ptrdiff_t difference_type;
164 typedef B value_type;
165 typedef value_type* pointer;
166 typedef value_type& reference;
167 typedef std::output_iterator_tag iterator_category;
172 template <class traits, class B, bool b>
173 ATL::CSimpleStringT<B, b> regex_replace(const ATL::CSimpleStringT<B, b>& s,
174 const basic_regex<B, traits>& e,
175 const ATL::CSimpleStringT<B, b>& fmt,
176 match_flag_type flags = match_default)
178 ATL::CSimpleStringT<B, b> result(s.GetManager());
179 BOOST_REGEX_DETAIL_NS::mfc_string_out_iterator<B, b> i(result);
180 regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags);
184 } // namespace boost.