6 * Use, modification and distribution are subject to the
\r
7 * Boost Software License, Version 1.0. (See accompanying file
\r
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
13 * LOCATION: see http://www.boost.org for most recent version.
\r
15 * VERSION see <boost/version.hpp>
\r
16 * DESCRIPTION: Overloads and helpers for using MFC/ATL string types with Boost.Regex.
\r
19 #ifndef BOOST_REGEX_MFC_HPP
\r
20 #define BOOST_REGEX_MFC_HPP
\r
22 #include <atlsimpstr.h>
\r
23 #include <boost/regex.hpp>
\r
28 // define the types used for TCHAR's:
\r
29 typedef basic_regex<TCHAR> tregex;
\r
30 typedef match_results<TCHAR const*> tmatch;
\r
31 typedef regex_iterator<TCHAR const*> tregex_iterator;
\r
32 typedef regex_token_iterator<TCHAR const*> tregex_token_iterator;
\r
34 #if _MSC_VER >= 1310
\r
35 #define SIMPLE_STRING_PARAM class B, bool b
\r
36 #define SIMPLE_STRING_ARG_LIST B, b
\r
38 #define SIMPLE_STRING_PARAM class B
\r
39 #define SIMPLE_STRING_ARG_LIST B
\r
43 // define regex creation functions:
\r
45 template <SIMPLE_STRING_PARAM>
\r
46 inline basic_regex<B>
\r
47 make_regex(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal)
\r
49 basic_regex<B> result(s.GetString(), s.GetString() + s.GetLength(), f);
\r
53 // regex_match overloads:
\r
55 template <SIMPLE_STRING_PARAM, class A, class T>
\r
56 inline bool regex_match(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
\r
57 match_results<const B*, A>& what,
\r
58 const basic_regex<B, T>& e,
\r
59 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
61 return ::boost::regex_match(s.GetString(),
\r
62 s.GetString() + s.GetLength(),
\r
68 template <SIMPLE_STRING_PARAM, class T>
\r
69 inline bool regex_match(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
\r
70 const basic_regex<B, T>& e,
\r
71 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
73 return ::boost::regex_match(s.GetString(),
\r
74 s.GetString() + s.GetLength(),
\r
79 // regex_search overloads:
\r
81 template <SIMPLE_STRING_PARAM, class A, class T>
\r
82 inline bool regex_search(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
\r
83 match_results<const B*, A>& what,
\r
84 const basic_regex<B, T>& e,
\r
85 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
87 return ::boost::regex_search(s.GetString(),
\r
88 s.GetString() + s.GetLength(),
\r
94 template <SIMPLE_STRING_PARAM, class T>
\r
95 inline bool regex_search(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
\r
96 const basic_regex<B, T>& e,
\r
97 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
99 return ::boost::regex_search(s.GetString(),
\r
100 s.GetString() + s.GetLength(),
\r
105 // regex_iterator creation:
\r
107 template <SIMPLE_STRING_PARAM>
\r
108 inline regex_iterator<B const*>
\r
109 make_regex_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
111 regex_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, f);
\r
115 template <SIMPLE_STRING_PARAM>
\r
116 inline regex_token_iterator<B const*>
\r
117 make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, int sub = 0, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
119 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);
\r
123 template <SIMPLE_STRING_PARAM>
\r
124 inline regex_token_iterator<B const*>
\r
125 make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, const std::vector<int>& subs, ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
127 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
\r
131 template <SIMPLE_STRING_PARAM, std::size_t N>
\r
132 inline regex_token_iterator<B const*>
\r
133 make_regex_token_iterator(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s, const basic_regex<B>& e, const int (& subs)[N], ::boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
\r
135 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
\r
139 template <class OutputIterator, class BidirectionalIterator, class traits,
\r
140 SIMPLE_STRING_PARAM>
\r
141 OutputIterator regex_replace(OutputIterator out,
\r
142 BidirectionalIterator first,
\r
143 BidirectionalIterator last,
\r
144 const basic_regex<B, traits>& e,
\r
145 const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& fmt,
\r
146 match_flag_type flags = match_default)
\r
148 return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);
\r
151 namespace re_detail{
\r
153 template <SIMPLE_STRING_PARAM>
\r
154 class mfc_string_out_iterator
\r
156 ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>* out;
\r
158 mfc_string_out_iterator(ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s) : out(&s) {}
\r
159 mfc_string_out_iterator& operator++() { return *this; }
\r
160 mfc_string_out_iterator& operator++(int) { return *this; }
\r
161 mfc_string_out_iterator& operator*() { return *this; }
\r
162 mfc_string_out_iterator& operator=(B v)
\r
164 out->AppendChar(v);
\r
167 typedef std::ptrdiff_t difference_type;
\r
168 typedef B value_type;
\r
169 typedef value_type* pointer;
\r
170 typedef value_type& reference;
\r
171 typedef std::output_iterator_tag iterator_category;
\r
176 template <class traits, SIMPLE_STRING_PARAM>
\r
177 ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST> regex_replace(const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& s,
\r
178 const basic_regex<B, traits>& e,
\r
179 const ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>& fmt,
\r
180 match_flag_type flags = match_default)
\r
182 ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST> result(s.GetManager());
\r
183 re_detail::mfc_string_out_iterator<SIMPLE_STRING_ARG_LIST> i(result);
\r
184 regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags);
\r
188 } // namespace boost.
\r