]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v4/sub_match.hpp
Update to latest boost 1.34 svn.
[lyx.git] / boost / boost / regex / v4 / sub_match.hpp
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
26 namespace boost{
27
28 template <class BidiIterator>
29 struct 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
125 private:
126    mutable boost::scoped_ptr<capture_sequence_type> m_captures;
127 public:
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
175 typedef sub_match<const char*> csub_match;
176 typedef sub_match<std::string::const_iterator> ssub_match;
177 #ifndef BOOST_NO_WREGEX
178 typedef sub_match<const wchar_t*> wcsub_match;
179 typedef sub_match<std::wstring::const_iterator> wssub_match;
180 #endif
181
182 // comparison to std::basic_string<> part 1:
183 template <class RandomAccessIterator, class traits, class Allocator>
184 inline 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; }
187 template <class RandomAccessIterator, class traits, class Allocator>
188 inline 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; }
191 template <class RandomAccessIterator, class traits, class Allocator>
192 inline 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; }
195 template <class RandomAccessIterator, class traits, class Allocator>
196 inline 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; }
199 template <class RandomAccessIterator, class traits, class Allocator>
200 inline 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; }
203 template <class RandomAccessIterator, class traits, class Allocator>
204 inline 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:
208 template <class RandomAccessIterator, class traits, class Allocator>
209 inline 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; }
212 template <class RandomAccessIterator, class traits, class Allocator>
213 inline 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; }
216 template <class RandomAccessIterator, class traits, class Allocator>
217 inline 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; }
220 template <class RandomAccessIterator, class traits, class Allocator>
221 inline 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; }
224 template <class RandomAccessIterator, class traits, class Allocator>
225 inline 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; }
228 template <class RandomAccessIterator, class traits, class Allocator>
229 inline 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:
233 template <class RandomAccessIterator>
234 inline 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; }
237 template <class RandomAccessIterator>
238 inline 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; }
241 template <class RandomAccessIterator>
242 inline 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; }
245 template <class RandomAccessIterator>
246 inline 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; }
249 template <class RandomAccessIterator>
250 inline 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; }
253 template <class RandomAccessIterator>
254 inline 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:
258 template <class RandomAccessIterator>
259 inline 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; }
262 template <class RandomAccessIterator>
263 inline 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; }
266 template <class RandomAccessIterator>
267 inline 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; }
270 template <class RandomAccessIterator>
271 inline 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; }
274 template <class RandomAccessIterator>
275 inline 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; }
278 template <class RandomAccessIterator>
279 inline 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:
284 template <class RandomAccessIterator>
285 inline 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; }
288 template <class RandomAccessIterator>
289 inline 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; }
292 template <class RandomAccessIterator>
293 inline 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; }
296 template <class RandomAccessIterator>
297 inline 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; }
300 template <class RandomAccessIterator>
301 inline 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; }
304 template <class RandomAccessIterator>
305 inline 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:
309 template <class RandomAccessIterator>
310 inline 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; }
313 template <class RandomAccessIterator>
314 inline 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; }
317 template <class RandomAccessIterator>
318 inline 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; }
321 template <class RandomAccessIterator>
322 inline 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; }
325 template <class RandomAccessIterator>
326 inline 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; }
329 template <class RandomAccessIterator>
330 inline 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:
335 template <class RandomAccessIterator, class traits, class Allocator>
336 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> 
337 operator + (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 }
344 template <class RandomAccessIterator, class traits, class Allocator>
345 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> 
346 operator + (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))
354 template <class RandomAccessIterator>
355 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
356 operator + (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 }
363 template <class RandomAccessIterator>
364 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
365 operator + (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:
374 template <class RandomAccessIterator>
375 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
376 operator + (typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type const* s,
377                   const sub_match<RandomAccessIterator>& m)
378 {
379    return s + m.str();
380 }
381 template <class RandomAccessIterator>
382 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
383 operator + (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
389 template <class RandomAccessIterator>
390 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
391 operator + (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 }
398 template <class RandomAccessIterator>
399 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
400 operator + (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 }
407 template <class RandomAccessIterator>
408 inline std::basic_string<typename re_detail::regex_iterator_traits<RandomAccessIterator>::value_type> 
409 operator + (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
417 template <class charT, class traits, class RandomAccessIterator>
418 std::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
425 template <class RandomAccessIterator>
426 std::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
434 namespace re_detail{
435 template <class BidiIterator, class charT>
436 int 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:
447 template <class I, class charT>
448 int 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
461 template <class BidiIterator>
462 sub_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 }
477 template <class BidiIterator>
478 sub_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