Chris@16
|
1 /*
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright (c) 2004
|
Chris@16
|
4 * John Maddock
|
Chris@16
|
5 *
|
Chris@16
|
6 * Use, modification and distribution are subject to the
|
Chris@16
|
7 * Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 *
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 /*
|
Chris@16
|
13 * LOCATION: see http://www.boost.org for most recent version.
|
Chris@16
|
14 * FILE mfc.hpp
|
Chris@16
|
15 * VERSION see <boost/version.hpp>
|
Chris@16
|
16 * DESCRIPTION: Overloads and helpers for using MFC/ATL string types with Boost.Regex.
|
Chris@16
|
17 */
|
Chris@16
|
18
|
Chris@16
|
19 #ifndef BOOST_REGEX_MFC_HPP
|
Chris@16
|
20 #define BOOST_REGEX_MFC_HPP
|
Chris@16
|
21
|
Chris@16
|
22 #include <atlsimpstr.h>
|
Chris@16
|
23 #include <boost/regex.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost{
|
Chris@16
|
26
|
Chris@16
|
27 //
|
Chris@16
|
28 // define the types used for TCHAR's:
|
Chris@16
|
29 typedef basic_regex<TCHAR> tregex;
|
Chris@16
|
30 typedef match_results<TCHAR const*> tmatch;
|
Chris@16
|
31 typedef regex_iterator<TCHAR const*> tregex_iterator;
|
Chris@16
|
32 typedef regex_token_iterator<TCHAR const*> tregex_token_iterator;
|
Chris@16
|
33
|
Chris@101
|
34 // Obsolete. Remove
|
Chris@16
|
35 #define SIMPLE_STRING_PARAM class B, bool b
|
Chris@16
|
36 #define SIMPLE_STRING_ARG_LIST B, b
|
Chris@16
|
37
|
Chris@16
|
38 //
|
Chris@16
|
39 // define regex creation functions:
|
Chris@16
|
40 //
|
Chris@101
|
41 template <class B, bool b>
|
Chris@16
|
42 inline basic_regex<B>
|
Chris@101
|
43 make_regex(const ATL::CSimpleStringT<B, b>& s, ::boost::regex_constants::syntax_option_type f = boost::regex_constants::normal)
|
Chris@16
|
44 {
|
Chris@16
|
45 basic_regex<B> result(s.GetString(), s.GetString() + s.GetLength(), f);
|
Chris@16
|
46 return result;
|
Chris@16
|
47 }
|
Chris@16
|
48 //
|
Chris@16
|
49 // regex_match overloads:
|
Chris@16
|
50 //
|
Chris@101
|
51 template <class B, bool b, class A, class T>
|
Chris@101
|
52 inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
|
Chris@16
|
53 match_results<const B*, A>& what,
|
Chris@16
|
54 const basic_regex<B, T>& e,
|
Chris@16
|
55 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
|
Chris@16
|
56 {
|
Chris@16
|
57 return ::boost::regex_match(s.GetString(),
|
Chris@16
|
58 s.GetString() + s.GetLength(),
|
Chris@16
|
59 what,
|
Chris@16
|
60 e,
|
Chris@16
|
61 f);
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@101
|
64 template <class B, bool b, class T>
|
Chris@101
|
65 inline bool regex_match(const ATL::CSimpleStringT<B, b>& s,
|
Chris@16
|
66 const basic_regex<B, T>& e,
|
Chris@16
|
67 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
|
Chris@16
|
68 {
|
Chris@16
|
69 return ::boost::regex_match(s.GetString(),
|
Chris@16
|
70 s.GetString() + s.GetLength(),
|
Chris@16
|
71 e,
|
Chris@16
|
72 f);
|
Chris@16
|
73 }
|
Chris@16
|
74 //
|
Chris@16
|
75 // regex_search overloads:
|
Chris@16
|
76 //
|
Chris@101
|
77 template <class B, bool b, class A, class T>
|
Chris@101
|
78 inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
|
Chris@16
|
79 match_results<const B*, A>& what,
|
Chris@16
|
80 const basic_regex<B, T>& e,
|
Chris@16
|
81 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
|
Chris@16
|
82 {
|
Chris@16
|
83 return ::boost::regex_search(s.GetString(),
|
Chris@16
|
84 s.GetString() + s.GetLength(),
|
Chris@16
|
85 what,
|
Chris@16
|
86 e,
|
Chris@16
|
87 f);
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@101
|
90 template <class B, bool b, class T>
|
Chris@101
|
91 inline bool regex_search(const ATL::CSimpleStringT<B, b>& s,
|
Chris@16
|
92 const basic_regex<B, T>& e,
|
Chris@16
|
93 boost::regex_constants::match_flag_type f = boost::regex_constants::match_default)
|
Chris@16
|
94 {
|
Chris@16
|
95 return ::boost::regex_search(s.GetString(),
|
Chris@16
|
96 s.GetString() + s.GetLength(),
|
Chris@16
|
97 e,
|
Chris@16
|
98 f);
|
Chris@16
|
99 }
|
Chris@16
|
100 //
|
Chris@16
|
101 // regex_iterator creation:
|
Chris@16
|
102 //
|
Chris@101
|
103 template <class B, bool b>
|
Chris@16
|
104 inline regex_iterator<B const*>
|
Chris@101
|
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)
|
Chris@16
|
106 {
|
Chris@16
|
107 regex_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, f);
|
Chris@16
|
108 return result;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@101
|
111 template <class B, bool b>
|
Chris@16
|
112 inline regex_token_iterator<B const*>
|
Chris@101
|
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)
|
Chris@16
|
114 {
|
Chris@16
|
115 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, sub, f);
|
Chris@16
|
116 return result;
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@101
|
119 template <class B, bool b>
|
Chris@16
|
120 inline regex_token_iterator<B const*>
|
Chris@101
|
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)
|
Chris@16
|
122 {
|
Chris@16
|
123 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
|
Chris@16
|
124 return result;
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@101
|
127 template <class B, bool b, std::size_t N>
|
Chris@16
|
128 inline regex_token_iterator<B const*>
|
Chris@101
|
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)
|
Chris@16
|
130 {
|
Chris@16
|
131 regex_token_iterator<B const*> result(s.GetString(), s.GetString() + s.GetLength(), e, subs, f);
|
Chris@16
|
132 return result;
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 template <class OutputIterator, class BidirectionalIterator, class traits,
|
Chris@101
|
136 class B, bool b>
|
Chris@16
|
137 OutputIterator regex_replace(OutputIterator out,
|
Chris@16
|
138 BidirectionalIterator first,
|
Chris@16
|
139 BidirectionalIterator last,
|
Chris@16
|
140 const basic_regex<B, traits>& e,
|
Chris@101
|
141 const ATL::CSimpleStringT<B, b>& fmt,
|
Chris@16
|
142 match_flag_type flags = match_default)
|
Chris@16
|
143 {
|
Chris@16
|
144 return ::boost::regex_replace(out, first, last, e, fmt.GetString(), flags);
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 namespace re_detail{
|
Chris@16
|
148
|
Chris@101
|
149 template <class B, bool b>
|
Chris@16
|
150 class mfc_string_out_iterator
|
Chris@16
|
151 {
|
Chris@101
|
152 ATL::CSimpleStringT<B, b>* out;
|
Chris@16
|
153 public:
|
Chris@101
|
154 mfc_string_out_iterator(ATL::CSimpleStringT<B, b>& s) : out(&s) {}
|
Chris@16
|
155 mfc_string_out_iterator& operator++() { return *this; }
|
Chris@16
|
156 mfc_string_out_iterator& operator++(int) { return *this; }
|
Chris@16
|
157 mfc_string_out_iterator& operator*() { return *this; }
|
Chris@16
|
158 mfc_string_out_iterator& operator=(B v)
|
Chris@16
|
159 {
|
Chris@16
|
160 out->AppendChar(v);
|
Chris@16
|
161 return *this;
|
Chris@16
|
162 }
|
Chris@16
|
163 typedef std::ptrdiff_t difference_type;
|
Chris@16
|
164 typedef B value_type;
|
Chris@16
|
165 typedef value_type* pointer;
|
Chris@16
|
166 typedef value_type& reference;
|
Chris@16
|
167 typedef std::output_iterator_tag iterator_category;
|
Chris@16
|
168 };
|
Chris@16
|
169
|
Chris@16
|
170 }
|
Chris@16
|
171
|
Chris@101
|
172 template <class traits, class B, bool b>
|
Chris@101
|
173 ATL::CSimpleStringT<B, b> regex_replace(const ATL::CSimpleStringT<B, b>& s,
|
Chris@16
|
174 const basic_regex<B, traits>& e,
|
Chris@101
|
175 const ATL::CSimpleStringT<B, b>& fmt,
|
Chris@16
|
176 match_flag_type flags = match_default)
|
Chris@16
|
177 {
|
Chris@101
|
178 ATL::CSimpleStringT<B, b> result(s.GetManager());
|
Chris@101
|
179 re_detail::mfc_string_out_iterator<B, b> i(result);
|
Chris@16
|
180 regex_replace(i, s.GetString(), s.GetString() + s.GetLength(), e, fmt.GetString(), flags);
|
Chris@16
|
181 return result;
|
Chris@16
|
182 }
|
Chris@16
|
183
|
Chris@16
|
184 } // namespace boost.
|
Chris@16
|
185
|
Chris@16
|
186 #endif
|