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
38 #define SIMPLE_STRING_PARAM class B
39 #define SIMPLE_STRING_ARG_LIST B
43 // define regex creation functions:
45 template <SIMPLE_STRING_PARAM
>
47 make_regex(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
, ::boost::regex_constants::syntax_option_type f
= boost::regex_constants::normal
)
49 basic_regex
<B
> result(s
.GetString(), s
.GetString() + s
.GetLength(), f
);
53 // regex_match overloads:
55 template <SIMPLE_STRING_PARAM
, class A
, class T
>
56 inline bool regex_match(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
,
57 match_results
<const B
*, A
>& what
,
58 const basic_regex
<B
, T
>& e
,
59 boost::regex_constants::match_flag_type f
= boost::regex_constants::match_default
)
61 return ::boost::regex_match(s
.GetString(),
62 s
.GetString() + s
.GetLength(),
68 template <SIMPLE_STRING_PARAM
, class T
>
69 inline bool regex_match(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
,
70 const basic_regex
<B
, T
>& e
,
71 boost::regex_constants::match_flag_type f
= boost::regex_constants::match_default
)
73 return ::boost::regex_match(s
.GetString(),
74 s
.GetString() + s
.GetLength(),
79 // regex_search overloads:
81 template <SIMPLE_STRING_PARAM
, class A
, class T
>
82 inline bool regex_search(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
,
83 match_results
<const B
*, A
>& what
,
84 const basic_regex
<B
, T
>& e
,
85 boost::regex_constants::match_flag_type f
= boost::regex_constants::match_default
)
87 return ::boost::regex_search(s
.GetString(),
88 s
.GetString() + s
.GetLength(),
94 template <SIMPLE_STRING_PARAM
, class T
>
95 inline bool regex_search(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
,
96 const basic_regex
<B
, T
>& e
,
97 boost::regex_constants::match_flag_type f
= boost::regex_constants::match_default
)
99 return ::boost::regex_search(s
.GetString(),
100 s
.GetString() + s
.GetLength(),
105 // regex_iterator creation:
107 template <SIMPLE_STRING_PARAM
>
108 inline regex_iterator
<B
const*>
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
)
111 regex_iterator
<B
const*> result(s
.GetString(), s
.GetString() + s
.GetLength(), e
, f
);
115 template <SIMPLE_STRING_PARAM
>
116 inline regex_token_iterator
<B
const*>
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
)
119 regex_token_iterator
<B
const*> result(s
.GetString(), s
.GetString() + s
.GetLength(), e
, sub
, f
);
123 template <SIMPLE_STRING_PARAM
>
124 inline regex_token_iterator
<B
const*>
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
)
127 regex_token_iterator
<B
const*> result(s
.GetString(), s
.GetString() + s
.GetLength(), e
, subs
, f
);
131 template <SIMPLE_STRING_PARAM
, std::size_t N
>
132 inline regex_token_iterator
<B
const*>
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
)
135 regex_token_iterator
<B
const*> result(s
.GetString(), s
.GetString() + s
.GetLength(), e
, subs
, f
);
139 template <class OutputIterator
, class BidirectionalIterator
, class traits
,
141 OutputIterator
regex_replace(OutputIterator out
,
142 BidirectionalIterator first
,
143 BidirectionalIterator last
,
144 const basic_regex
<B
, traits
>& e
,
145 const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& fmt
,
146 match_flag_type flags
= match_default
)
148 return ::boost::regex_replace(out
, first
, last
, e
, fmt
.GetString(), flags
);
153 template <SIMPLE_STRING_PARAM
>
154 class mfc_string_out_iterator
156 ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>* out
;
158 mfc_string_out_iterator(ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
) : out(&s
) {}
159 mfc_string_out_iterator
& operator++() { return *this; }
160 mfc_string_out_iterator
& operator++(int) { return *this; }
161 mfc_string_out_iterator
& operator*() { return *this; }
162 mfc_string_out_iterator
& operator=(B v
)
167 typedef std::ptrdiff_t difference_type
;
168 typedef B value_type
;
169 typedef value_type
* pointer
;
170 typedef value_type
& reference
;
171 typedef std::output_iterator_tag iterator_category
;
176 template <class traits
, SIMPLE_STRING_PARAM
>
177 ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
> regex_replace(const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& s
,
178 const basic_regex
<B
, traits
>& e
,
179 const ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
>& fmt
,
180 match_flag_type flags
= match_default
)
182 ATL::CSimpleStringT
<SIMPLE_STRING_ARG_LIST
> result(s
.GetManager());
183 re_detail::mfc_string_out_iterator
<SIMPLE_STRING_ARG_LIST
> i(result
);
184 regex_replace(i
, s
.GetString(), s
.GetString() + s
.GetLength(), e
, fmt
.GetString(), flags
);
188 } // namespace boost.