3 * Copyright (c) 1998-2002
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: Declares template class sub_match.
19 #ifndef BOOST_REGEX_V4_SUB_MATCH_HPP
20 #define BOOST_REGEX_V4_SUB_MATCH_HPP
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
28 template <class BidiIterator>
29 struct sub_match : public std::pair<BidiIterator, BidiIterator>
31 typedef typename re_detail::regex_iterator_traits<BidiIterator>::value_type value_type;
32 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
33 typedef std::ptrdiff_t difference_type;
35 typedef typename re_detail::regex_iterator_traits<BidiIterator>::difference_type difference_type;
37 typedef BidiIterator iterator_type;
38 typedef BidiIterator iterator;
39 typedef BidiIterator const_iterator;
43 sub_match() : std::pair<BidiIterator, BidiIterator>(), matched(false) {}
44 sub_match(BidiIterator i) : std::pair<BidiIterator, BidiIterator>(i, i), matched(false) {}
46 operator std::basic_string<value_type> ()const
50 difference_type BOOST_REGEX_CALL length()const
52 difference_type n = boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
55 std::basic_string<value_type> str()const
57 std::basic_string<value_type> result;
58 std::size_t len = boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
60 BidiIterator i = this->first;
61 while(i != this->second)
68 int compare(const sub_match& s)const
70 if(matched != s.matched)
71 return static_cast<int>(matched) - static_cast<int>(s.matched);
72 return str().compare(s.str());
75 bool operator==(const sub_match& that)const
76 { return compare(that) == 0; }
77 bool BOOST_REGEX_CALL operator !=(const sub_match& that)const
78 { return compare(that) != 0; }
79 bool operator<(const sub_match& that)const
80 { return compare(that) < 0; }
81 bool operator>(const sub_match& that)const
82 { return compare(that) > 0; }
83 bool operator<=(const sub_match& that)const
84 { return compare(that) <= 0; }
85 bool operator>=(const sub_match& that)const
86 { return compare(that) >= 0; }
88 #ifdef BOOST_REGEX_MATCH_EXTRA
89 typedef std::vector<sub_match<BidiIterator> > capture_sequence_type;
91 const capture_sequence_type& captures()const
94 m_captures.reset(new capture_sequence_type());
98 // Private implementation API: DO NOT USE!
100 capture_sequence_type& get_captures()const
103 m_captures.reset(new capture_sequence_type());
108 mutable boost::scoped_ptr<capture_sequence_type> m_captures;
112 sub_match(const sub_match& that, bool
113 #ifdef BOOST_REGEX_MATCH_EXTRA
118 : std::pair<BidiIterator, BidiIterator>(that),
119 matched(that.matched)
121 #ifdef BOOST_REGEX_MATCH_EXTRA
124 m_captures.reset(new capture_sequence_type(*(that.m_captures)));
127 sub_match& operator=(const sub_match& that)
129 this->first = that.first;
130 this->second = that.second;
131 matched = that.matched;
132 #ifdef BOOST_REGEX_MATCH_EXTRA
134 get_captures() = *(that.m_captures);
140 #ifdef BOOST_OLD_REGEX_H
142 // the following are deprecated, do not use!!
145 operator unsigned int()const;
146 operator short()const
148 return (short)(int)(*this);
150 operator unsigned short()const
152 return (unsigned short)(unsigned int)(*this);
157 // comparison to std::basic_string<> part 1:
158 template <class RandomAccessIterator, class traits, class Allocator>
159 inline bool operator == (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
160 const sub_match<RandomAccessIterator>& m)
161 { return s.compare(m.str()) == 0; }
162 template <class RandomAccessIterator, class traits, class Allocator>
163 inline bool operator != (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
164 const sub_match<RandomAccessIterator>& m)
165 { return s.compare(m.str()) != 0; }
166 template <class RandomAccessIterator, class traits, class Allocator>
167 inline bool operator < (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
168 const sub_match<RandomAccessIterator>& m)
169 { return s.compare(m.str()) < 0; }
170 template <class RandomAccessIterator, class traits, class Allocator>
171 inline bool operator <= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
172 const sub_match<RandomAccessIterator>& m)
173 { return s.compare(m.str()) <= 0; }
174 template <class RandomAccessIterator, class traits, class Allocator>
175 inline bool operator >= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
176 const sub_match<RandomAccessIterator>& m)
177 { return s.compare(m.str()) >= 0; }
178 template <class RandomAccessIterator, class traits, class Allocator>
179 inline bool operator > (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
180 const sub_match<RandomAccessIterator>& m)
181 { return s.compare(m.str()) > 0; }
182 // comparison to std::basic_string<> part 2:
183 template <class RandomAccessIterator, class traits, class Allocator>
184 inline bool operator == (const sub_match<RandomAccessIterator>& m,
185 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
186 { return m.str().compare(s) == 0; }
187 template <class RandomAccessIterator, class traits, class Allocator>
188 inline bool operator != (const sub_match<RandomAccessIterator>& m,
189 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
190 { return m.str().compare(s) != 0; }
191 template <class RandomAccessIterator, class traits, class Allocator>
192 inline bool operator < (const sub_match<RandomAccessIterator>& m,
193 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
194 { return m.str().compare(s) < 0; }
195 template <class RandomAccessIterator, class traits, class Allocator>
196 inline bool operator > (const sub_match<RandomAccessIterator>& m,
197 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
198 { return m.str().compare(s) > 0; }
199 template <class RandomAccessIterator, class traits, class Allocator>
200 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
201 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
202 { return m.str().compare(s) <= 0; }
203 template <class RandomAccessIterator, class traits, class Allocator>
204 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
205 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
206 { return m.str().compare(s) >= 0; }
207 // comparison to const charT* part 1:
208 template <class RandomAccessIterator>
209 inline bool operator == (const sub_match<RandomAccessIterator>& m,
210 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
211 { return m.str().compare(s) == 0; }
212 template <class RandomAccessIterator>
213 inline bool operator != (const sub_match<RandomAccessIterator>& m,
214 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
215 { return m.str().compare(s) != 0; }
216 template <class RandomAccessIterator>
217 inline bool operator > (const sub_match<RandomAccessIterator>& m,
218 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
219 { return m.str().compare(s) > 0; }
220 template <class RandomAccessIterator>
221 inline bool operator < (const sub_match<RandomAccessIterator>& m,
222 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
223 { return m.str().compare(s) < 0; }
224 template <class RandomAccessIterator>
225 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
226 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
227 { return m.str().compare(s) >= 0; }
228 template <class RandomAccessIterator>
229 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
230 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
231 { return m.str().compare(s) <= 0; }
232 // comparison to const charT* part 2:
233 template <class RandomAccessIterator>
234 inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
235 const sub_match<RandomAccessIterator>& m)
236 { return m.str().compare(s) == 0; }
237 template <class RandomAccessIterator>
238 inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
239 const sub_match<RandomAccessIterator>& m)
240 { return m.str().compare(s) != 0; }
241 template <class RandomAccessIterator>
242 inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
243 const sub_match<RandomAccessIterator>& m)
244 { return m.str().compare(s) > 0; }
245 template <class RandomAccessIterator>
246 inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
247 const sub_match<RandomAccessIterator>& m)
248 { return m.str().compare(s) < 0; }
249 template <class RandomAccessIterator>
250 inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
251 const sub_match<RandomAccessIterator>& m)
252 { return m.str().compare(s) >= 0; }
253 template <class RandomAccessIterator>
254 inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
255 const sub_match<RandomAccessIterator>& m)
256 { return m.str().compare(s) <= 0; }
258 // comparison to const charT& part 1:
259 template <class RandomAccessIterator>
260 inline bool operator == (const sub_match<RandomAccessIterator>& m,
261 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
262 { return m.str().compare(0, m.length(), &s, 1) == 0; }
263 template <class RandomAccessIterator>
264 inline bool operator != (const sub_match<RandomAccessIterator>& m,
265 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
266 { return m.str().compare(0, m.length(), &s, 1) != 0; }
267 template <class RandomAccessIterator>
268 inline bool operator > (const sub_match<RandomAccessIterator>& m,
269 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
270 { return m.str().compare(0, m.length(), &s, 1) > 0; }
271 template <class RandomAccessIterator>
272 inline bool operator < (const sub_match<RandomAccessIterator>& m,
273 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
274 { return m.str().compare(0, m.length(), &s, 1) < 0; }
275 template <class RandomAccessIterator>
276 inline bool operator >= (const sub_match<RandomAccessIterator>& m,
277 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
278 { return m.str().compare(0, m.length(), &s, 1) >= 0; }
279 template <class RandomAccessIterator>
280 inline bool operator <= (const sub_match<RandomAccessIterator>& m,
281 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
282 { return m.str().compare(0, m.length(), &s, 1) <= 0; }
283 // comparison to const charT* part 2:
284 template <class RandomAccessIterator>
285 inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
286 const sub_match<RandomAccessIterator>& m)
287 { return m.str().compare(0, m.length(), &s, 1) == 0; }
288 template <class RandomAccessIterator>
289 inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
290 const sub_match<RandomAccessIterator>& m)
291 { return m.str().compare(0, m.length(), &s, 1) != 0; }
292 template <class RandomAccessIterator>
293 inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
294 const sub_match<RandomAccessIterator>& m)
295 { return m.str().compare(0, m.length(), &s, 1) > 0; }
296 template <class RandomAccessIterator>
297 inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
298 const sub_match<RandomAccessIterator>& m)
299 { return m.str().compare(0, m.length(), &s, 1) < 0; }
300 template <class RandomAccessIterator>
301 inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
302 const sub_match<RandomAccessIterator>& m)
303 { return m.str().compare(0, m.length(), &s, 1) >= 0; }
304 template <class RandomAccessIterator>
305 inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
306 const sub_match<RandomAccessIterator>& m)
307 { return m.str().compare(0, m.length(), &s, 1) <= 0; }
309 // addition operators:
310 template <class RandomAccessIterator, class traits, class Allocator>
311 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
312 operator + (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
313 const sub_match<RandomAccessIterator>& m)
315 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
316 result.reserve(s.size() + m.length() + 1);
317 return result.append(s).append(m.first, m.second);
319 template <class RandomAccessIterator, class traits, class Allocator>
320 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
321 operator + (const sub_match<RandomAccessIterator>& m,
322 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
324 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
325 result.reserve(s.size() + m.length() + 1);
326 return result.append(m.first, m.second).append(s);
328 #if !(defined(__GNUC__) && defined(BOOST_NO_STD_LOCALE))
329 template <class RandomAccessIterator>
330 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
331 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
332 const sub_match<RandomAccessIterator>& m)
334 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
335 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
336 return result.append(s).append(m.first, m.second);
338 template <class RandomAccessIterator>
339 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
340 operator + (const sub_match<RandomAccessIterator>& m,
341 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
343 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
344 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
345 return result.append(m.first, m.second).append(s);
348 // worwaround versions:
349 template <class RandomAccessIterator>
350 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
351 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
352 const sub_match<RandomAccessIterator>& m)
356 template <class RandomAccessIterator>
357 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
358 operator + (const sub_match<RandomAccessIterator>& m,
359 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
364 template <class RandomAccessIterator>
365 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
366 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
367 const sub_match<RandomAccessIterator>& m)
369 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
370 result.reserve(m.length() + 2);
371 return result.append(1, s).append(m.first, m.second);
373 template <class RandomAccessIterator>
374 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
375 operator + (const sub_match<RandomAccessIterator>& m,
376 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
378 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
379 result.reserve(m.length() + 2);
380 return result.append(m.first, m.second).append(1, s);
382 template <class RandomAccessIterator>
383 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
384 operator + (const sub_match<RandomAccessIterator>& m1,
385 const sub_match<RandomAccessIterator>& m2)
387 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
388 result.reserve(m1.length() + m2.length() + 1);
389 return result.append(m1.first, m1.second).append(m2.first, m2.second);
391 #ifndef BOOST_NO_STD_LOCALE
392 template <class charT, class traits, class RandomAccessIterator>
393 std::basic_ostream<charT, traits>&
394 operator << (std::basic_ostream<charT, traits>& os,
395 const sub_match<RandomAccessIterator>& s)
397 return (os << s.str());
400 template <class RandomAccessIterator>
401 std::ostream& operator << (std::ostream& os,
402 const sub_match<RandomAccessIterator>& s)
404 return (os << s.str());
408 #ifdef BOOST_OLD_REGEX_H
410 template <class BidiIterator, class charT>
411 int do_toi(BidiIterator i, BidiIterator j, char c, int radix)
415 int result = std::strtol(s.c_str(), &p, radix);
416 if(*p)raise_regex_exception("Bad sub-expression");
422 template <class I, class charT>
423 int do_toi(I& i, I j, charT c)
426 while((i != j) && (isdigit(*i)))
428 result = result*10 + (*i - '0');
436 template <class BidiIterator>
437 sub_match<BidiIterator>::operator int()const
439 BidiIterator i = first;
440 BidiIterator j = second;
441 if(i == j)raise_regex_exception("Bad sub-expression");
443 if((i != j) && (*i == '-'))
448 neg *= re_detail::do_toi(i, j, *i);
449 if(i != j)raise_regex_exception("Bad sub-expression");
452 template <class BidiIterator>
453 sub_match<BidiIterator>::operator unsigned int()const
455 BidiIterator i = first;
456 BidiIterator j = second;
458 raise_regex_exception("Bad sub-expression");
459 return re_detail::do_toi(i, j, *first);
465 #ifdef BOOST_HAS_ABI_HEADERS
466 # include BOOST_ABI_SUFFIX