1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
/*
*
* Copyright (c) 1998-2009
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE match_results.cpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Declares template class match_results.
*/
#ifndef BOOST_REGEX_V4_MATCH_RESULTS_HPP
#define BOOST_REGEX_V4_MATCH_RESULTS_HPP
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable: 4103)
#endif
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
namespace boost{
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4251 4231 4660)
#endif
namespace re_detail{
template <class charT>
class named_subexpressions;
}
template <class BidiIterator, class Allocator>
class match_results
{
private:
#ifndef BOOST_NO_STD_ALLOCATOR
typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
#else
typedef std::vector<sub_match<BidiIterator> > vector_type;
#endif
public:
typedef sub_match<BidiIterator> value_type;
#if !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
typedef typename Allocator::const_reference const_reference;
#else
typedef const value_type& const_reference;
#endif
typedef const_reference reference;
typedef typename vector_type::const_iterator const_iterator;
typedef const_iterator iterator;
typedef typename re_detail::regex_iterator_traits<
BidiIterator>::difference_type difference_type;
typedef typename Allocator::size_type size_type;
typedef Allocator allocator_type;
typedef typename re_detail::regex_iterator_traits<
BidiIterator>::value_type char_type;
typedef std::basic_string<char_type> string_type;
typedef re_detail::named_subexpressions_base<char_type> named_sub_type;
// construct/copy/destroy:
explicit match_results(const Allocator& a = Allocator())
#ifndef BOOST_NO_STD_ALLOCATOR
: m_subs(a), m_base(), m_last_closed_paren(0), m_is_singular(true) {}
#else
: m_subs(), m_base(), m_last_closed_paren(0), m_is_singular(true) { (void)a; }
#endif
match_results(const match_results& m)
: m_subs(m.m_subs), m_named_subs(m.m_named_subs), m_last_closed_paren(m.m_last_closed_paren), m_is_singular(m.m_is_singular)
{
if(!m_is_singular)
{
m_base = m.m_base;
m_null = m.m_null;
}
}
match_results& operator=(const match_results& m)
{
m_subs = m.m_subs;
m_named_subs = m.m_named_subs;
m_last_closed_paren = m.m_last_closed_paren;
m_is_singular = m.m_is_singular;
if(!m_is_singular)
{
m_base = m.m_base;
m_null = m.m_null;
}
return *this;
}
~match_results(){}
// size:
size_type size() const
{ return empty() ? 0 : m_subs.size() - 2; }
size_type max_size() const
{ return m_subs.max_size(); }
bool empty() const
{ return m_subs.size() < 2; }
// element access:
difference_type length(int sub = 0) const
{
if(m_is_singular)
raise_logic_error();
sub += 2;
if((sub < (int)m_subs.size()) && (sub > 0))
return m_subs[sub].length();
return 0;
}
difference_type length(const char_type* sub) const
{
if(m_is_singular)
raise_logic_error();
const char_type* sub_end = sub;
while(*sub_end) ++sub_end;
return length(named_subexpression_index(sub, sub_end));
}
template <class charT>
difference_type length(const charT* sub) const
{
if(m_is_singular)
raise_logic_error();
const charT* sub_end = sub;
while(*sub_end) ++sub_end;
return length(named_subexpression_index(sub, sub_end));
}
template <class charT, class Traits, class A>
difference_type length(const std::basic_string<charT, Traits, A>& sub) const
{
return length(sub.c_str());
}
difference_type position(size_type sub = 0) const
{
if(m_is_singular)
raise_logic_error();
sub += 2;
if(sub < m_subs.size())
{
const sub_match<BidiIterator>& s = m_subs[sub];
if(s.matched || (sub == 2))
{
return ::boost::re_detail::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
}
}
return ~static_cast<difference_type>(0);
}
difference_type position(const char_type* sub) const
{
const char_type* sub_end = sub;
while(*sub_end) ++sub_end;
return position(named_subexpression_index(sub, sub_end));
}
template <class charT>
difference_type position(const charT* sub) const
{
const charT* sub_end = sub;
while(*sub_end) ++sub_end;
return position(named_subexpression_index(sub, sub_end));
}
template <class charT, class Traits, class A>
difference_type position(const std::basic_string<charT, Traits, A>& sub) const
{
return position(sub.c_str());
}
string_type str(int sub = 0) const
{
if(m_is_singular)
raise_logic_error();
sub += 2;
string_type result;
if(sub < (int)m_subs.size() && (sub > 0))
{
const sub_match<BidiIterator>& s = m_subs[sub];
if(s.matched)
{
result = s.str();
}
}
return result;
}
string_type str(const char_type* sub) const
{
return (*this)[sub].str();
}
template <class Traits, class A>
string_type str(const std::basic_string<char_type, Traits, A>& sub) const
{
return (*this)[sub].str();
}
template <class charT>
string_type str(const charT* sub) const
{
return (*this)[sub].str();
}
template <class charT, class Traits, class A>
string_type str(const std::basic_string<charT, Traits, A>& sub) const
{
return (*this)[sub].str();
}
const_reference operator[](int sub) const
{
if(m_is_singular && m_subs.empty())
raise_logic_error();
sub += 2;
if(sub < (int)m_subs.size() && (sub >= 0))
{
return m_subs[sub];
}
return m_null;
}
//
// Named sub-expressions:
//
const_reference named_subexpression(const char_type* i, const char_type* j) const
{
if(m_is_singular)
raise_logic_error();
int index = m_named_subs->get_id(i, j);
return index > 0 ? (*this)[index] : m_null;
}
template <class charT>
const_reference named_subexpression(const charT* i, const charT* j) const
{
BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
if(i == j)
return m_null;
std::vector<char_type> s;
while(i != j)
s.insert(s.end(), *i++);
return named_subexpression(&*s.begin(), &*s.begin() + s.size());
}
int named_subexpression_index(const char_type* i, const char_type* j) const
{
if(m_is_singular)
raise_logic_error();
int index = m_named_subs->get_id(i, j);
return index > 0 ? index : -20;
}
template <class charT>
int named_subexpression_index(const charT* i, const charT* j) const
{
BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
if(i == j)
return -20;
std::vector<char_type> s;
while(i != j)
s.insert(s.end(), *i++);
return named_subexpression_index(&*s.begin(), &*s.begin() + s.size());
}
template <class Traits, class A>
const_reference operator[](const std::basic_string<char_type, Traits, A>& s) const
{
return named_subexpression(s.c_str(), s.c_str() + s.size());
}
const_reference operator[](const char_type* p) const
{
const char_type* e = p;
while(*e) ++e;
return named_subexpression(p, e);
}
template <class charT>
const_reference operator[](const charT* p) const
{
BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
if(*p == 0)
return m_null;
std::vector<char_type> s;
while(*p)
s.insert(s.end(), *p++);
return named_subexpression(&*s.begin(), &*s.begin() + s.size());
}
template <class charT, class Traits, class A>
const_reference operator[](const std::basic_string<charT, Traits, A>& ns) const
{
BOOST_STATIC_ASSERT(sizeof(charT) <= sizeof(char_type));
if(ns.empty())
return m_null;
std::vector<char_type> s;
for(unsigned i = 0; i < ns.size(); ++i)
s.insert(s.end(), ns[i]);
return named_subexpression(&*s.begin(), &*s.begin() + s.size());
}
const_reference prefix() const
{
if(m_is_singular)
raise_logic_error();
return (*this)[-1];
}
const_reference suffix() const
{
if(m_is_singular)
raise_logic_error();
return (*this)[-2];
}
const_iterator begin() const
{
return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
}
const_iterator end() const
{
return m_subs.end();
}
// format:
template <class OutputIterator, class Functor>
OutputIterator format(OutputIterator out,
Functor fmt,
match_flag_type flags = format_default) const
{
if(m_is_singular)
raise_logic_error();
typedef typename re_detail::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator>::type F;
F func(fmt);
return func(*this, out, flags);
}
template <class Functor>
string_type format(Functor fmt, match_flag_type flags = format_default) const
{
if(m_is_singular)
raise_logic_error();
std::basic_string<char_type> result;
re_detail::string_out_iterator<std::basic_string<char_type> > i(result);
typedef typename re_detail::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, re_detail::string_out_iterator<std::basic_string<char_type> > >::type F;
F func(fmt);
func(*this, i, flags);
return result;
}
// format with locale:
template <class OutputIterator, class Functor, class RegexT>
OutputIterator format(OutputIterator out,
Functor fmt,
match_flag_type flags,
const RegexT& re) const
{
if(m_is_singular)
raise_logic_error();
typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
typedef typename re_detail::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, OutputIterator, traits_type>::type F;
F func(fmt);
return func(*this, out, flags, re.get_traits());
}
template <class RegexT, class Functor>
string_type format(Functor fmt,
match_flag_type flags,
const RegexT& re) const
{
if(m_is_singular)
raise_logic_error();
typedef ::boost::regex_traits_wrapper<typename RegexT::traits_type> traits_type;
std::basic_string<char_type> result;
re_detail::string_out_iterator<std::basic_string<char_type> > i(result);
typedef typename re_detail::compute_functor_type<Functor, match_results<BidiIterator, Allocator>, re_detail::string_out_iterator<std::basic_string<char_type> >, traits_type >::type F;
F func(fmt);
func(*this, i, flags, re.get_traits());
return result;
}
const_reference get_last_closed_paren()const
{
if(m_is_singular)
raise_logic_error();
return m_last_closed_paren == 0 ? m_null : (*this)[m_last_closed_paren];
}
allocator_type get_allocator() const
{
#ifndef BOOST_NO_STD_ALLOCATOR
return m_subs.get_allocator();
#else
return allocator_type();
#endif
}
void swap(match_results& that)
{
std::swap(m_subs, that.m_subs);
std::swap(m_named_subs, that.m_named_subs);
std::swap(m_last_closed_paren, that.m_last_closed_paren);
std::swap(m_is_singular, that.m_is_singular);
if(m_is_singular)
{
if(!that.m_is_singular)
{
m_base = that.m_base;
m_null = that.m_null;
}
}
else if(that.m_is_singular)
{
that.m_base = m_base;
that.m_null = m_null;
}
else
{
std::swap(m_base, that.m_base);
std::swap(m_null, that.m_null);
}
}
bool operator==(const match_results& that)const
{
if(m_is_singular)
{
return that.m_is_singular;
}
else if(that.m_is_singular)
{
return false;
}
return (m_subs == that.m_subs) && (m_base == that.m_base) && (m_last_closed_paren == that.m_last_closed_paren);
}
bool operator!=(const match_results& that)const
{ return !(*this == that); }
#ifdef BOOST_REGEX_MATCH_EXTRA
typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
const capture_sequence_type& captures(int i)const
{
if(m_is_singular)
raise_logic_error();
return (*this)[i].captures();
}
#endif
//
// private access functions:
void BOOST_REGEX_CALL set_second(BidiIterator i)
{
BOOST_ASSERT(m_subs.size() > 2);
m_subs[2].second = i;
m_subs[2].matched = true;
m_subs[0].first = i;
m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
m_null.first = i;
m_null.second = i;
m_null.matched = false;
m_is_singular = false;
}
void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true, bool escape_k = false)
{
if(pos)
m_last_closed_paren = pos;
pos += 2;
BOOST_ASSERT(m_subs.size() > pos);
m_subs[pos].second = i;
m_subs[pos].matched = m;
if((pos == 2) && !escape_k)
{
m_subs[0].first = i;
m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
m_null.first = i;
m_null.second = i;
m_null.matched = false;
m_is_singular = false;
}
}
void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
{
value_type v(j);
size_type len = m_subs.size();
if(len > n + 2)
{
m_subs.erase(m_subs.begin()+n+2, m_subs.end());
std::fill(m_subs.begin(), m_subs.end(), v);
}
else
{
std::fill(m_subs.begin(), m_subs.end(), v);
if(n+2 != len)
m_subs.insert(m_subs.end(), n+2-len, v);
}
m_subs[1].first = i;
m_last_closed_paren = 0;
}
void BOOST_REGEX_CALL set_base(BidiIterator pos)
{
m_base = pos;
}
BidiIterator base()const
{
return m_base;
}
void BOOST_REGEX_CALL set_first(BidiIterator i)
{
BOOST_ASSERT(m_subs.size() > 2);
// set up prefix:
m_subs[1].second = i;
m_subs[1].matched = (m_subs[1].first != i);
// set up $0:
m_subs[2].first = i;
// zero out everything else:
for(size_type n = 3; n < m_subs.size(); ++n)
{
m_subs[n].first = m_subs[n].second = m_subs[0].second;
m_subs[n].matched = false;
}
}
void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos, bool escape_k = false)
{
BOOST_ASSERT(pos+2 < m_subs.size());
if(pos || escape_k)
{
m_subs[pos+2].first = i;
if(escape_k)
{
m_subs[1].second = i;
m_subs[1].matched = (m_subs[1].first != m_subs[1].second);
}
}
else
set_first(i);
}
void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
void BOOST_REGEX_CALL set_named_subs(boost::shared_ptr<named_sub_type> subs)
{
m_named_subs = subs;
}
private:
//
// Error handler called when an uninitialized match_results is accessed:
//
static void raise_logic_error()
{
std::logic_error e("Attempt to access an uninitialzed boost::match_results<> class.");
boost::throw_exception(e);
}
vector_type m_subs; // subexpressions
BidiIterator m_base; // where the search started from
sub_match<BidiIterator> m_null; // a null match
boost::shared_ptr<named_sub_type> m_named_subs; // Shared copy of named subs in the regex object
int m_last_closed_paren; // Last ) to be seen - used for formatting
bool m_is_singular; // True if our stored iterators are singular
};
template <class BidiIterator, class Allocator>
void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
{
if(m_is_singular)
{
*this = m;
return;
}
const_iterator p1, p2;
p1 = begin();
p2 = m.begin();
//
// Distances are measured from the start of *this* match, unless this isn't
// a valid match in which case we use the start of the whole sequence. Note that
// no subsequent match-candidate can ever be to the left of the first match found.
// This ensures that when we are using bidirectional iterators, that distances
// measured are as short as possible, and therefore as efficient as possible
// to compute. Finally note that we don't use the "matched" data member to test
// whether a sub-expression is a valid match, because partial matches set this
// to false for sub-expression 0.
//
BidiIterator l_end = this->suffix().second;
BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
difference_type len1 = 0;
difference_type len2 = 0;
difference_type base1 = 0;
difference_type base2 = 0;
std::size_t i;
for(i = 0; i < size(); ++i, ++p1, ++p2)
{
//
// Leftmost takes priority over longest; handle special cases
// where distances need not be computed first (an optimisation
// for bidirectional iterators: ensure that we don't accidently
// compute the length of the whole sequence, as this can be really
// expensive).
//
if(p1->first == l_end)
{
if(p2->first != l_end)
{
// p2 must be better than p1, and no need to calculate
// actual distances:
base1 = 1;
base2 = 0;
break;
}
else
{
// *p1 and *p2 are either unmatched or match end-of sequence,
// either way no need to calculate distances:
if((p1->matched == false) && (p2->matched == true))
break;
if((p1->matched == true) && (p2->matched == false))
return;
continue;
}
}
else if(p2->first == l_end)
{
// p1 better than p2, and no need to calculate distances:
return;
}
base1 = ::boost::re_detail::distance(l_base, p1->first);
base2 = ::boost::re_detail::distance(l_base, p2->first);
BOOST_ASSERT(base1 >= 0);
BOOST_ASSERT(base2 >= 0);
if(base1 < base2) return;
if(base2 < base1) break;
len1 = ::boost::re_detail::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
len2 = ::boost::re_detail::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
BOOST_ASSERT(len1 >= 0);
BOOST_ASSERT(len2 >= 0);
if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
break;
if((p1->matched == true) && (p2->matched == false))
return;
}
if(i == size())
return;
if(base2 < base1)
*this = m;
else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
*this = m;
}
template <class BidiIterator, class Allocator>
void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
{
a.swap(b);
}
#ifndef BOOST_NO_STD_LOCALE
template <class charT, class traits, class BidiIterator, class Allocator>
std::basic_ostream<charT, traits>&
operator << (std::basic_ostream<charT, traits>& os,
const match_results<BidiIterator, Allocator>& s)
{
return (os << s.str());
}
#else
template <class BidiIterator, class Allocator>
std::ostream& operator << (std::ostream& os,
const match_results<BidiIterator, Allocator>& s)
{
return (os << s.str());
}
#endif
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
} // namespace boost
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable: 4103)
#endif
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif
|