1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file sub_match.hpp
3 /// Contains the definition of the class template sub_match\<\>
4 /// and associated helper functions
5 //
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
11 #define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
12
13 // MS compatible compilers support #pragma once
14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
15 # pragma once
16 #endif
17
18 #include <iosfwd>
19 #include <string>
20 #include <utility>
21 #include <iterator>
22 #include <algorithm>
23 #include <boost/iterator/iterator_traits.hpp>
24 #include <boost/xpressive/detail/detail_fwd.hpp>
25
26 //{{AFX_DOC_COMMENT
27 ///////////////////////////////////////////////////////////////////////////////
28 // This is a hack to get Doxygen to show the inheritance relation between
29 // sub_match<T> and std::pair<T,T>.
30 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
31 /// INTERNAL ONLY
32 namespace std
33 {
34 /// INTERNAL ONLY
35 template<typename, typename> struct pair {};
36 }
37 #endif
38 //}}AFX_DOC_COMMENT
39
40 namespace boost { namespace xpressive
41 {
42
43 ///////////////////////////////////////////////////////////////////////////////
44 // sub_match
45 //
46 /// \brief Class template sub_match denotes the sequence of characters matched by a particular marked sub-expression.
47 ///
48 /// When the marked sub-expression denoted by an object of type sub_match\<\> participated in a
49 /// regular expression match then member matched evaluates to true, and members first and second
50 /// denote the range of characters [first,second) which formed that match. Otherwise matched is false,
51 /// and members first and second contained undefined values.
52 ///
53 /// If an object of type sub_match\<\> represents sub-expression 0 - that is to say the whole match -
54 /// then member matched is always true, unless a partial match was obtained as a result of the flag
55 /// match_partial being passed to a regular expression algorithm, in which case member matched is
56 /// false, and members first and second represent the character range that formed the partial match.
57 template<typename BidiIter>
58 struct sub_match
59 : std::pair<BidiIter, BidiIter>
60 {
61 private:
62 /// INTERNAL ONLY
63 ///
64 struct dummy { int i_; };
65 typedef int dummy::*bool_type;
66
67 public:
68 typedef typename iterator_value<BidiIter>::type value_type;
69 typedef typename iterator_difference<BidiIter>::type difference_type;
70 typedef typename detail::string_type<value_type>::type string_type;
71 typedef BidiIter iterator;
72
sub_matchboost::xpressive::sub_match73 sub_match()
74 : std::pair<BidiIter, BidiIter>()
75 , matched(false)
76 {
77 }
78
sub_matchboost::xpressive::sub_match79 sub_match(BidiIter first, BidiIter second, bool matched_ = false)
80 : std::pair<BidiIter, BidiIter>(first, second)
81 , matched(matched_)
82 {
83 }
84
strboost::xpressive::sub_match85 string_type str() const
86 {
87 return this->matched ? string_type(this->first, this->second) : string_type();
88 }
89
operator string_typeboost::xpressive::sub_match90 operator string_type() const
91 {
92 return this->matched ? string_type(this->first, this->second) : string_type();
93 }
94
lengthboost::xpressive::sub_match95 difference_type length() const
96 {
97 return this->matched ? std::distance(this->first, this->second) : 0;
98 }
99
operator bool_typeboost::xpressive::sub_match100 operator bool_type() const
101 {
102 return this->matched ? &dummy::i_ : 0;
103 }
104
operator !boost::xpressive::sub_match105 bool operator !() const
106 {
107 return !this->matched;
108 }
109
110 /// \brief Performs a lexicographic string comparison
111 /// \param str the string against which to compare
112 /// \return the results of (*this).str().compare(str)
compareboost::xpressive::sub_match113 int compare(string_type const &str) const
114 {
115 return this->str().compare(str);
116 }
117
118 /// \overload
119 ///
compareboost::xpressive::sub_match120 int compare(sub_match const &sub) const
121 {
122 return this->str().compare(sub.str());
123 }
124
125 /// \overload
126 ///
compareboost::xpressive::sub_match127 int compare(value_type const *ptr) const
128 {
129 return this->str().compare(ptr);
130 }
131
132 /// \brief true if this sub-match participated in the full match.
133 bool matched;
134 };
135
136 ///////////////////////////////////////////////////////////////////////////////
137 /// \brief insertion operator for sending sub-matches to ostreams
138 /// \param sout output stream.
139 /// \param sub sub_match object to be written to the stream.
140 /// \return sout \<\< sub.str()
141 template<typename BidiIter, typename Char, typename Traits>
operator <<(std::basic_ostream<Char,Traits> & sout,sub_match<BidiIter> const & sub)142 inline std::basic_ostream<Char, Traits> &operator <<
143 (
144 std::basic_ostream<Char, Traits> &sout
145 , sub_match<BidiIter> const &sub
146 )
147 {
148 typedef typename iterator_value<BidiIter>::type char_type;
149 if(sub.matched)
150 {
151 std::ostream_iterator<char_type, Char, Traits> iout(sout);
152 std::copy(sub.first, sub.second, iout);
153 }
154 return sout;
155 }
156
157
158 // BUGBUG make these more efficient
159
160 template<typename BidiIter>
operator ==(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)161 bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
162 {
163 return lhs.compare(rhs) == 0;
164 }
165
166 template<typename BidiIter>
operator !=(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)167 bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
168 {
169 return lhs.compare(rhs) != 0;
170 }
171
172 template<typename BidiIter>
operator <(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)173 bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
174 {
175 return lhs.compare(rhs) < 0;
176 }
177
178 template<typename BidiIter>
operator <=(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)179 bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
180 {
181 return lhs.compare(rhs) <= 0;
182 }
183
184 template<typename BidiIter>
operator >=(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)185 bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
186 {
187 return lhs.compare(rhs) >= 0;
188 }
189
190 template<typename BidiIter>
operator >(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)191 bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
192 {
193 return lhs.compare(rhs) > 0;
194 }
195
196 template<typename BidiIter>
operator ==(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)197 bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
198 {
199 return lhs == rhs.str();
200 }
201
202 template<typename BidiIter>
operator !=(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)203 bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
204 {
205 return lhs != rhs.str();
206 }
207
208 template<typename BidiIter>
operator <(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)209 bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
210 {
211 return lhs < rhs.str();
212 }
213
214 template<typename BidiIter>
operator >(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)215 bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
216 {
217 return lhs> rhs.str();
218 }
219
220 template<typename BidiIter>
operator >=(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)221 bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
222 {
223 return lhs >= rhs.str();
224 }
225
226 template<typename BidiIter>
operator <=(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)227 bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
228 {
229 return lhs <= rhs.str();
230 }
231
232 template<typename BidiIter>
operator ==(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)233 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
234 {
235 return lhs.str() == rhs;
236 }
237
238 template<typename BidiIter>
operator !=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)239 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
240 {
241 return lhs.str() != rhs;
242 }
243
244 template<typename BidiIter>
operator <(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)245 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
246 {
247 return lhs.str() < rhs;
248 }
249
250 template<typename BidiIter>
operator >(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)251 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
252 {
253 return lhs.str() > rhs;
254 }
255
256 template<typename BidiIter>
operator >=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)257 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
258 {
259 return lhs.str() >= rhs;
260 }
261
262 template<typename BidiIter>
operator <=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)263 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
264 {
265 return lhs.str() <= rhs;
266 }
267
268 template<typename BidiIter>
operator ==(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)269 bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
270 {
271 return lhs == rhs.str();
272 }
273
274 template<typename BidiIter>
operator !=(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)275 bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
276 {
277 return lhs != rhs.str();
278 }
279
280 template<typename BidiIter>
operator <(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)281 bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
282 {
283 return lhs < rhs.str();
284 }
285
286 template<typename BidiIter>
operator >(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)287 bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
288 {
289 return lhs> rhs.str();
290 }
291
292 template<typename BidiIter>
operator >=(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)293 bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
294 {
295 return lhs >= rhs.str();
296 }
297
298 template<typename BidiIter>
operator <=(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)299 bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
300 {
301 return lhs <= rhs.str();
302 }
303
304 template<typename BidiIter>
operator ==(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)305 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
306 {
307 return lhs.str() == rhs;
308 }
309
310 template<typename BidiIter>
operator !=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)311 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
312 {
313 return lhs.str() != rhs;
314 }
315
316 template<typename BidiIter>
operator <(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)317 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
318 {
319 return lhs.str() < rhs;
320 }
321
322 template<typename BidiIter>
operator >(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)323 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
324 {
325 return lhs.str() > rhs;
326 }
327
328 template<typename BidiIter>
operator >=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)329 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
330 {
331 return lhs.str() >= rhs;
332 }
333
334 template<typename BidiIter>
operator <=(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)335 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
336 {
337 return lhs.str() <= rhs;
338 }
339
340 // Operator+ convenience function
341 template<typename BidiIter>
342 typename sub_match<BidiIter>::string_type
operator +(sub_match<BidiIter> const & lhs,sub_match<BidiIter> const & rhs)343 operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
344 {
345 return lhs.str() + rhs.str();
346 }
347
348 template<typename BidiIter>
349 typename sub_match<BidiIter>::string_type
operator +(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const & rhs)350 operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
351 {
352 return lhs.str() + rhs;
353 }
354
355 template<typename BidiIter>
356 typename sub_match<BidiIter>::string_type
operator +(typename iterator_value<BidiIter>::type const & lhs,sub_match<BidiIter> const & rhs)357 operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
358 {
359 return lhs + rhs.str();
360 }
361
362 template<typename BidiIter>
363 typename sub_match<BidiIter>::string_type
operator +(sub_match<BidiIter> const & lhs,typename iterator_value<BidiIter>::type const * rhs)364 operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
365 {
366 return lhs.str() + rhs;
367 }
368
369 template<typename BidiIter>
370 typename sub_match<BidiIter>::string_type
operator +(typename iterator_value<BidiIter>::type const * lhs,sub_match<BidiIter> const & rhs)371 operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
372 {
373 return lhs + rhs.str();
374 }
375
376 template<typename BidiIter>
377 typename sub_match<BidiIter>::string_type
operator +(sub_match<BidiIter> const & lhs,typename sub_match<BidiIter>::string_type const & rhs)378 operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs)
379 {
380 return lhs.str() + rhs;
381 }
382
383 template<typename BidiIter>
384 typename sub_match<BidiIter>::string_type
operator +(typename sub_match<BidiIter>::string_type const & lhs,sub_match<BidiIter> const & rhs)385 operator + (typename sub_match<BidiIter>::string_type const &lhs, sub_match<BidiIter> const &rhs)
386 {
387 return lhs + rhs.str();
388 }
389
390 }} // namespace boost::xpressive
391
392 #endif
393