fix doc example typo
[boost.git] / boost / regex / mfc.hpp
blob02502f950480d7eacd71ef517d41ea852674e161
1 /*
3 * Copyright (c) 2004
4 * John Maddock
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.
14 * FILE mfc.hpp
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>
25 namespace boost{
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;
34 #if _MSC_VER >= 1310
35 #define SIMPLE_STRING_PARAM class B, bool b
36 #define SIMPLE_STRING_ARG_LIST B, b
37 #else
38 #define SIMPLE_STRING_PARAM class B
39 #define SIMPLE_STRING_ARG_LIST B
40 #endif
43 // define regex creation functions:
45 template <SIMPLE_STRING_PARAM>
46 inline basic_regex<B>
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);
50 return result;
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(),
63 what,
65 f);
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(),
76 f);
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(),
89 what,
91 f);
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);
112 return result;
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);
120 return result;
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);
128 return result;
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);
136 return result;
139 template <class OutputIterator, class BidirectionalIterator, class traits,
140 SIMPLE_STRING_PARAM>
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);
151 namespace re_detail{
153 template <SIMPLE_STRING_PARAM>
154 class mfc_string_out_iterator
156 ATL::CSimpleStringT<SIMPLE_STRING_ARG_LIST>* out;
157 public:
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)
164 out->AppendChar(v);
165 return *this;
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);
185 return result;
188 } // namespace boost.
190 #endif