blob: 621cc83dd6aeef44a8a42c7e4e5ab3e033056d84 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/*
2 *
3 * Copyright (c) 1998-2002
4 * John Maddock
5 *
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)
9 *
10 */
11
12 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE sub_match.cpp
15 * VERSION see <ndnboost/version.hpp>
16 * DESCRIPTION: Declares template class sub_match.
17 */
18
19#ifndef NDNBOOST_REGEX_V4_SUB_MATCH_HPP
20#define NDNBOOST_REGEX_V4_SUB_MATCH_HPP
21
22#ifdef NDNBOOST_MSVC
23#pragma warning(push)
24#pragma warning(disable: 4103)
25#endif
26#ifdef NDNBOOST_HAS_ABI_HEADERS
27# include NDNBOOST_ABI_PREFIX
28#endif
29#ifdef NDNBOOST_MSVC
30#pragma warning(pop)
31#endif
32
33namespace ndnboost{
34
35template <class BidiIterator>
36struct sub_match : public std::pair<BidiIterator, BidiIterator>
37{
38 typedef typename re_detail::regex_iterator_traits<BidiIterator>::value_type value_type;
39#if defined(NDNBOOST_NO_STD_ITERATOR_TRAITS) || defined(NDNBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
40 typedef std::ptrdiff_t difference_type;
41#else
42 typedef typename re_detail::regex_iterator_traits<BidiIterator>::difference_type difference_type;
43#endif
44 typedef BidiIterator iterator_type;
45 typedef BidiIterator iterator;
46 typedef BidiIterator const_iterator;
47
48 bool matched;
49
50 sub_match() : std::pair<BidiIterator, BidiIterator>(), matched(false) {}
51 sub_match(BidiIterator i) : std::pair<BidiIterator, BidiIterator>(i, i), matched(false) {}
52#if !defined(NDNBOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
53 && !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1310)\
54 && !NDNBOOST_WORKAROUND(__BORLANDC__, <= 0x0551)\
55 && !NDNBOOST_WORKAROUND(__DECCXX_VER, NDNBOOST_TESTED_AT(60590042))
56 template <class T, class A>
57 operator std::basic_string<value_type, T, A> ()const
58 {
59 return matched ? std::basic_string<value_type, T, A>(this->first, this->second) : std::basic_string<value_type, T, A>();
60 }
61#else
62 operator std::basic_string<value_type> ()const
63 {
64 return str();
65 }
66#endif
67 difference_type NDNBOOST_REGEX_CALL length()const
68 {
69 difference_type n = matched ? ::ndnboost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second) : 0;
70 return n;
71 }
72 std::basic_string<value_type> str()const
73 {
74 std::basic_string<value_type> result;
75 if(matched)
76 {
77 std::size_t len = ::ndnboost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
78 result.reserve(len);
79 BidiIterator i = this->first;
80 while(i != this->second)
81 {
82 result.append(1, *i);
83 ++i;
84 }
85 }
86 return result;
87 }
88 int compare(const sub_match& s)const
89 {
90 if(matched != s.matched)
91 return static_cast<int>(matched) - static_cast<int>(s.matched);
92 return str().compare(s.str());
93 }
94 int compare(const std::basic_string<value_type>& s)const
95 {
96 return str().compare(s);
97 }
98 int compare(const value_type* p)const
99 {
100 return str().compare(p);
101 }
102
103 bool operator==(const sub_match& that)const
104 { return compare(that) == 0; }
105 bool NDNBOOST_REGEX_CALL operator !=(const sub_match& that)const
106 { return compare(that) != 0; }
107 bool operator<(const sub_match& that)const
108 { return compare(that) < 0; }
109 bool operator>(const sub_match& that)const
110 { return compare(that) > 0; }
111 bool operator<=(const sub_match& that)const
112 { return compare(that) <= 0; }
113 bool operator>=(const sub_match& that)const
114 { return compare(that) >= 0; }
115
116#ifdef NDNBOOST_REGEX_MATCH_EXTRA
117 typedef std::vector<sub_match<BidiIterator> > capture_sequence_type;
118
119 const capture_sequence_type& captures()const
120 {
121 if(!m_captures)
122 m_captures.reset(new capture_sequence_type());
123 return *m_captures;
124 }
125 //
126 // Private implementation API: DO NOT USE!
127 //
128 capture_sequence_type& get_captures()const
129 {
130 if(!m_captures)
131 m_captures.reset(new capture_sequence_type());
132 return *m_captures;
133 }
134
135private:
136 mutable ndnboost::scoped_ptr<capture_sequence_type> m_captures;
137public:
138
139#endif
140 sub_match(const sub_match& that, bool
141#ifdef NDNBOOST_REGEX_MATCH_EXTRA
142 deep_copy
143#endif
144 = true
145 )
146 : std::pair<BidiIterator, BidiIterator>(that),
147 matched(that.matched)
148 {
149#ifdef NDNBOOST_REGEX_MATCH_EXTRA
150 if(that.m_captures)
151 if(deep_copy)
152 m_captures.reset(new capture_sequence_type(*(that.m_captures)));
153#endif
154 }
155 sub_match& operator=(const sub_match& that)
156 {
157 this->first = that.first;
158 this->second = that.second;
159 matched = that.matched;
160#ifdef NDNBOOST_REGEX_MATCH_EXTRA
161 if(that.m_captures)
162 get_captures() = *(that.m_captures);
163#endif
164 return *this;
165 }
166
167
168#ifdef NDNBOOST_OLD_REGEX_H
169 //
170 // the following are deprecated, do not use!!
171 //
172 operator int()const;
173 operator unsigned int()const;
174 operator short()const
175 {
176 return (short)(int)(*this);
177 }
178 operator unsigned short()const
179 {
180 return (unsigned short)(unsigned int)(*this);
181 }
182#endif
183};
184
185typedef sub_match<const char*> csub_match;
186typedef sub_match<std::string::const_iterator> ssub_match;
187#ifndef NDNBOOST_NO_WREGEX
188typedef sub_match<const wchar_t*> wcsub_match;
189typedef sub_match<std::wstring::const_iterator> wssub_match;
190#endif
191
192// comparison to std::basic_string<> part 1:
193template <class RandomAccessIterator, class traits, class Allocator>
194inline bool operator == (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
195 const sub_match<RandomAccessIterator>& m)
196{ return s.compare(m.str()) == 0; }
197template <class RandomAccessIterator, class traits, class Allocator>
198inline bool operator != (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
199 const sub_match<RandomAccessIterator>& m)
200{ return s.compare(m.str()) != 0; }
201template <class RandomAccessIterator, class traits, class Allocator>
202inline bool operator < (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
203 const sub_match<RandomAccessIterator>& m)
204{ return s.compare(m.str()) < 0; }
205template <class RandomAccessIterator, class traits, class Allocator>
206inline bool operator <= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
207 const sub_match<RandomAccessIterator>& m)
208{ return s.compare(m.str()) <= 0; }
209template <class RandomAccessIterator, class traits, class Allocator>
210inline bool operator >= (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
211 const sub_match<RandomAccessIterator>& m)
212{ return s.compare(m.str()) >= 0; }
213template <class RandomAccessIterator, class traits, class Allocator>
214inline bool operator > (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
215 const sub_match<RandomAccessIterator>& m)
216{ return s.compare(m.str()) > 0; }
217// comparison to std::basic_string<> part 2:
218template <class RandomAccessIterator, class traits, class Allocator>
219inline bool operator == (const sub_match<RandomAccessIterator>& m,
220 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
221{ return m.str().compare(s) == 0; }
222template <class RandomAccessIterator, class traits, class Allocator>
223inline bool operator != (const sub_match<RandomAccessIterator>& m,
224 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
225{ return m.str().compare(s) != 0; }
226template <class RandomAccessIterator, class traits, class Allocator>
227inline bool operator < (const sub_match<RandomAccessIterator>& m,
228 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
229{ return m.str().compare(s) < 0; }
230template <class RandomAccessIterator, class traits, class Allocator>
231inline bool operator > (const sub_match<RandomAccessIterator>& m,
232 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
233{ return m.str().compare(s) > 0; }
234template <class RandomAccessIterator, class traits, class Allocator>
235inline bool operator <= (const sub_match<RandomAccessIterator>& m,
236 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
237{ return m.str().compare(s) <= 0; }
238template <class RandomAccessIterator, class traits, class Allocator>
239inline bool operator >= (const sub_match<RandomAccessIterator>& m,
240 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
241{ return m.str().compare(s) >= 0; }
242// comparison to const charT* part 1:
243template <class RandomAccessIterator>
244inline bool operator == (const sub_match<RandomAccessIterator>& m,
245 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
246{ return m.str().compare(s) == 0; }
247template <class RandomAccessIterator>
248inline bool operator != (const sub_match<RandomAccessIterator>& m,
249 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
250{ return m.str().compare(s) != 0; }
251template <class RandomAccessIterator>
252inline bool operator > (const sub_match<RandomAccessIterator>& m,
253 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
254{ return m.str().compare(s) > 0; }
255template <class RandomAccessIterator>
256inline bool operator < (const sub_match<RandomAccessIterator>& m,
257 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
258{ return m.str().compare(s) < 0; }
259template <class RandomAccessIterator>
260inline bool operator >= (const sub_match<RandomAccessIterator>& m,
261 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
262{ return m.str().compare(s) >= 0; }
263template <class RandomAccessIterator>
264inline bool operator <= (const sub_match<RandomAccessIterator>& m,
265 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s)
266{ return m.str().compare(s) <= 0; }
267// comparison to const charT* part 2:
268template <class RandomAccessIterator>
269inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
270 const sub_match<RandomAccessIterator>& m)
271{ return m.str().compare(s) == 0; }
272template <class RandomAccessIterator>
273inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
274 const sub_match<RandomAccessIterator>& m)
275{ return m.str().compare(s) != 0; }
276template <class RandomAccessIterator>
277inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
278 const sub_match<RandomAccessIterator>& m)
279{ return m.str().compare(s) > 0; }
280template <class RandomAccessIterator>
281inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
282 const sub_match<RandomAccessIterator>& m)
283{ return m.str().compare(s) < 0; }
284template <class RandomAccessIterator>
285inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
286 const sub_match<RandomAccessIterator>& m)
287{ return m.str().compare(s) >= 0; }
288template <class RandomAccessIterator>
289inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
290 const sub_match<RandomAccessIterator>& m)
291{ return m.str().compare(s) <= 0; }
292
293// comparison to const charT& part 1:
294template <class RandomAccessIterator>
295inline bool operator == (const sub_match<RandomAccessIterator>& m,
296 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
297{ return m.str().compare(0, m.length(), &s, 1) == 0; }
298template <class RandomAccessIterator>
299inline bool operator != (const sub_match<RandomAccessIterator>& m,
300 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
301{ return m.str().compare(0, m.length(), &s, 1) != 0; }
302template <class RandomAccessIterator>
303inline bool operator > (const sub_match<RandomAccessIterator>& m,
304 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
305{ return m.str().compare(0, m.length(), &s, 1) > 0; }
306template <class RandomAccessIterator>
307inline bool operator < (const sub_match<RandomAccessIterator>& m,
308 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
309{ return m.str().compare(0, m.length(), &s, 1) < 0; }
310template <class RandomAccessIterator>
311inline bool operator >= (const sub_match<RandomAccessIterator>& m,
312 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
313{ return m.str().compare(0, m.length(), &s, 1) >= 0; }
314template <class RandomAccessIterator>
315inline bool operator <= (const sub_match<RandomAccessIterator>& m,
316 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
317{ return m.str().compare(0, m.length(), &s, 1) <= 0; }
318// comparison to const charT* part 2:
319template <class RandomAccessIterator>
320inline bool operator == (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
321 const sub_match<RandomAccessIterator>& m)
322{ return m.str().compare(0, m.length(), &s, 1) == 0; }
323template <class RandomAccessIterator>
324inline bool operator != (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
325 const sub_match<RandomAccessIterator>& m)
326{ return m.str().compare(0, m.length(), &s, 1) != 0; }
327template <class RandomAccessIterator>
328inline bool operator < (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
329 const sub_match<RandomAccessIterator>& m)
330{ return m.str().compare(0, m.length(), &s, 1) > 0; }
331template <class RandomAccessIterator>
332inline bool operator > (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
333 const sub_match<RandomAccessIterator>& m)
334{ return m.str().compare(0, m.length(), &s, 1) < 0; }
335template <class RandomAccessIterator>
336inline bool operator <= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
337 const sub_match<RandomAccessIterator>& m)
338{ return m.str().compare(0, m.length(), &s, 1) >= 0; }
339template <class RandomAccessIterator>
340inline bool operator >= (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
341 const sub_match<RandomAccessIterator>& m)
342{ return m.str().compare(0, m.length(), &s, 1) <= 0; }
343
344// addition operators:
345template <class RandomAccessIterator, class traits, class Allocator>
346inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
347operator + (const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
348 const sub_match<RandomAccessIterator>& m)
349{
350 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
351 result.reserve(s.size() + m.length() + 1);
352 return result.append(s).append(m.first, m.second);
353}
354template <class RandomAccessIterator, class traits, class Allocator>
355inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
356operator + (const sub_match<RandomAccessIterator>& m,
357 const std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
358{
359 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
360 result.reserve(s.size() + m.length() + 1);
361 return result.append(m.first, m.second).append(s);
362}
363#if !(defined(__GNUC__) && defined(NDNBOOST_NO_STD_LOCALE))
364template <class RandomAccessIterator>
365inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
366operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
367 const sub_match<RandomAccessIterator>& m)
368{
369 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
370 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
371 return result.append(s).append(m.first, m.second);
372}
373template <class RandomAccessIterator>
374inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
375operator + (const sub_match<RandomAccessIterator>& m,
376 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
377{
378 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
379 result.reserve(std::char_traits<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
380 return result.append(m.first, m.second).append(s);
381}
382#else
383// worwaround versions:
384template <class RandomAccessIterator>
385inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
386operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
387 const sub_match<RandomAccessIterator>& m)
388{
389 return s + m.str();
390}
391template <class RandomAccessIterator>
392inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
393operator + (const sub_match<RandomAccessIterator>& m,
394 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const * s)
395{
396 return m.str() + s;
397}
398#endif
399template <class RandomAccessIterator>
400inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
401operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s,
402 const sub_match<RandomAccessIterator>& m)
403{
404 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
405 result.reserve(m.length() + 2);
406 return result.append(1, s).append(m.first, m.second);
407}
408template <class RandomAccessIterator>
409inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
410operator + (const sub_match<RandomAccessIterator>& m,
411 typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const& s)
412{
413 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
414 result.reserve(m.length() + 2);
415 return result.append(m.first, m.second).append(1, s);
416}
417template <class RandomAccessIterator>
418inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type>
419operator + (const sub_match<RandomAccessIterator>& m1,
420 const sub_match<RandomAccessIterator>& m2)
421{
422 std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> result;
423 result.reserve(m1.length() + m2.length() + 1);
424 return result.append(m1.first, m1.second).append(m2.first, m2.second);
425}
426#ifndef NDNBOOST_NO_STD_LOCALE
427template <class charT, class traits, class RandomAccessIterator>
428std::basic_ostream<charT, traits>&
429 operator << (std::basic_ostream<charT, traits>& os,
430 const sub_match<RandomAccessIterator>& s)
431{
432 return (os << s.str());
433}
434#else
435template <class RandomAccessIterator>
436std::ostream& operator << (std::ostream& os,
437 const sub_match<RandomAccessIterator>& s)
438{
439 return (os << s.str());
440}
441#endif
442
443#ifdef NDNBOOST_OLD_REGEX_H
444namespace re_detail{
445template <class BidiIterator, class charT>
446int do_toi(BidiIterator i, BidiIterator j, char c, int radix)
447{
448 std::string s(i, j);
449 char* p;
450 int result = std::strtol(s.c_str(), &p, radix);
451 if(*p)raise_regex_exception("Bad sub-expression");
452 return result;
453}
454
455//
456// helper:
457template <class I, class charT>
458int do_toi(I& i, I j, charT c)
459{
460 int result = 0;
461 while((i != j) && (isdigit(*i)))
462 {
463 result = result*10 + (*i - '0');
464 ++i;
465 }
466 return result;
467}
468}
469
470
471template <class BidiIterator>
472sub_match<BidiIterator>::operator int()const
473{
474 BidiIterator i = first;
475 BidiIterator j = second;
476 if(i == j)raise_regex_exception("Bad sub-expression");
477 int neg = 1;
478 if((i != j) && (*i == '-'))
479 {
480 neg = -1;
481 ++i;
482 }
483 neg *= re_detail::do_toi(i, j, *i);
484 if(i != j)raise_regex_exception("Bad sub-expression");
485 return neg;
486}
487template <class BidiIterator>
488sub_match<BidiIterator>::operator unsigned int()const
489{
490 BidiIterator i = first;
491 BidiIterator j = second;
492 if(i == j)
493 raise_regex_exception("Bad sub-expression");
494 return re_detail::do_toi(i, j, *first);
495}
496#endif
497
498} // namespace ndnboost
499
500#ifdef NDNBOOST_MSVC
501#pragma warning(push)
502#pragma warning(disable: 4103)
503#endif
504#ifdef NDNBOOST_HAS_ABI_HEADERS
505# include NDNBOOST_ABI_SUFFIX
506#endif
507#ifdef NDNBOOST_MSVC
508#pragma warning(pop)
509#endif
510
511#endif
512