source: NonGTP/Boost/boost/regex/v4/sub_match.hpp @ 857

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