]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v4/sub_match.hpp
update to boost 1.32.0
[lyx.git] / boost / boost / regex / v4 / sub_match.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr 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
46    operator std::basic_string<value_type> ()const
47    {
48       return str();
49    }
50    difference_type BOOST_REGEX_CALL length()const
51    {
52       difference_type n = boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
53       return n;
54    }
55    std::basic_string<value_type> str()const
56    {
57       std::basic_string<value_type> result;
58       std::size_t len = boost::re_detail::distance((BidiIterator)this->first, (BidiIterator)this->second);
59       result.reserve(len);
60       BidiIterator i = this->first;
61       while(i != this->second)
62       {
63          result.append(1, *i);
64          ++i;
65       }
66       return result;
67    }
68    int compare(const sub_match& s)const
69    {
70       if(matched != s.matched)
71          return static_cast<int>(matched) - static_cast<int>(s.matched);
72       return str().compare(s.str());
73    }
74
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; }
87
88 #ifdef BOOST_REGEX_MATCH_EXTRA
89    typedef std::vector<sub_match<BidiIterator> > capture_sequence_type;
90
91    const capture_sequence_type& captures()const
92    {
93       if(!m_captures) 
94          m_captures.reset(new capture_sequence_type());
95       return *m_captures;
96    }
97    //
98    // Private implementation API: DO NOT USE!
99    //
100    capture_sequence_type& get_captures()const
101    {
102       if(!m_captures) 
103          m_captures.reset(new capture_sequence_type());
104       return *m_captures;
105    }
106
107 private:
108    mutable boost::scoped_ptr<capture_sequence_type> m_captures;
109 public:
110
111 #endif
112    sub_match(const sub_match& that, bool 
113 #ifdef BOOST_REGEX_MATCH_EXTRA
114       deep_copy
115 #endif
116       = true
117       ) 
118       : std::pair<BidiIterator, BidiIterator>(that), 
119         matched(that.matched) 
120    {
121 #ifdef BOOST_REGEX_MATCH_EXTRA
122       if(that.m_captures)
123          if(deep_copy)
124             m_captures.reset(new capture_sequence_type(*(that.m_captures)));
125 #endif
126    }
127    sub_match& operator=(const sub_match& that)
128    {
129       this->first = that.first;
130       this->second = that.second;
131       matched = that.matched;
132 #ifdef BOOST_REGEX_MATCH_EXTRA
133       if(that.m_captures)
134          get_captures() = *(that.m_captures);
135 #endif
136       return *this;
137    }
138
139
140 #ifdef BOOST_OLD_REGEX_H
141    //
142    // the following are deprecated, do not use!!
143    //
144    operator int()const;
145    operator unsigned int()const;
146    operator short()const
147    {
148       return (short)(int)(*this);
149    }
150    operator unsigned short()const
151    {
152       return (unsigned short)(unsigned int)(*this);
153    }
154 #endif
155 };
156
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; }
257
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; }
308
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)
314 {
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);
318 }
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)
323 {
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);
327 }
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)
333 {
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);
337 }
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)
342 {
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);
346 }
347 #else
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)
353 {
354    return s + m.str();
355 }
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)
360 {
361    return m.str() + s;
362 }
363 #endif
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)
368 {
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);
372 }
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)
377 {
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);
381 }
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)
386 {
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);
390 }
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)
396 {
397    return (os << s.str());
398 }
399 #else
400 template <class RandomAccessIterator>
401 std::ostream& operator << (std::ostream& os,
402                            const sub_match<RandomAccessIterator>& s)
403 {
404    return (os << s.str());
405 }
406 #endif
407
408 #ifdef BOOST_OLD_REGEX_H
409 namespace re_detail{
410 template <class BidiIterator, class charT>
411 int do_toi(BidiIterator i, BidiIterator j, char c, int radix)
412 {
413    std::string s(i, j);
414    char* p;
415    int result = std::strtol(s.c_str(), &p, radix);
416    if(*p)raise_regex_exception("Bad sub-expression");
417    return result;
418 }
419
420 //
421 // helper:
422 template <class I, class charT>
423 int do_toi(I& i, I j, charT c)
424 {
425    int result = 0;
426    while((i != j) && (isdigit(*i)))
427    {
428       result = result*10 + (*i - '0');
429       ++i;
430    }
431    return result;
432 }
433 }
434
435
436 template <class BidiIterator>
437 sub_match<BidiIterator>::operator int()const
438 {
439    BidiIterator i = first;
440    BidiIterator j = second;
441    if(i == j)raise_regex_exception("Bad sub-expression");
442    int neg = 1;
443    if((i != j) && (*i == '-'))
444    {
445       neg = -1;
446       ++i;
447    }
448    neg *= re_detail::do_toi(i, j, *i);
449    if(i != j)raise_regex_exception("Bad sub-expression");
450    return neg;
451 }
452 template <class BidiIterator>
453 sub_match<BidiIterator>::operator unsigned int()const
454 {
455    BidiIterator i = first;
456    BidiIterator j = second;
457    if(i == j)
458       raise_regex_exception("Bad sub-expression");
459    return re_detail::do_toi(i, j, *first);
460 }
461 #endif
462
463 } // namespace boost
464
465 #ifdef BOOST_HAS_ABI_HEADERS
466 #  include BOOST_ABI_SUFFIX
467 #endif
468
469 #endif
470