600
|
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 match_results.cpp
|
|
15 |
* VERSION see <boost/version.hpp>
|
|
16 |
* DESCRIPTION: Declares template class match_results.
|
|
17 |
*/
|
|
18 |
|
|
19 |
#ifndef BOOST_REGEX_V4_MATCH_RESULTS_HPP
|
|
20 |
#define BOOST_REGEX_V4_MATCH_RESULTS_HPP
|
|
21 |
|
|
22 |
#ifdef BOOST_MSVC
|
|
23 |
#pragma warning(push)
|
|
24 |
#pragma warning(disable: 4103)
|
|
25 |
#endif
|
|
26 |
#ifdef BOOST_HAS_ABI_HEADERS
|
|
27 |
# include BOOST_ABI_PREFIX
|
|
28 |
#endif
|
|
29 |
#ifdef BOOST_MSVC
|
|
30 |
#pragma warning(pop)
|
|
31 |
#endif
|
|
32 |
|
|
33 |
namespace boost{
|
|
34 |
#ifdef BOOST_MSVC
|
|
35 |
#pragma warning(push)
|
|
36 |
#pragma warning(disable : 4251 4231 4660)
|
|
37 |
#endif
|
|
38 |
|
|
39 |
template <class BidiIterator, class Allocator>
|
|
40 |
class match_results
|
|
41 |
{
|
|
42 |
private:
|
|
43 |
#ifndef BOOST_NO_STD_ALLOCATOR
|
|
44 |
typedef std::vector<sub_match<BidiIterator>, Allocator> vector_type;
|
|
45 |
#else
|
|
46 |
typedef std::vector<sub_match<BidiIterator> > vector_type;
|
|
47 |
#endif
|
|
48 |
public:
|
|
49 |
typedef sub_match<BidiIterator> value_type;
|
|
50 |
#if !defined(BOOST_NO_STD_ALLOCATOR) && !(defined(BOOST_MSVC) && defined(_STLPORT_VERSION))
|
|
51 |
typedef typename Allocator::const_reference const_reference;
|
|
52 |
#else
|
|
53 |
typedef const value_type& const_reference;
|
|
54 |
#endif
|
|
55 |
typedef const_reference reference;
|
|
56 |
typedef typename vector_type::const_iterator const_iterator;
|
|
57 |
typedef const_iterator iterator;
|
|
58 |
typedef typename re_detail::regex_iterator_traits<
|
|
59 |
BidiIterator>::difference_type difference_type;
|
|
60 |
typedef typename Allocator::size_type size_type;
|
|
61 |
typedef Allocator allocator_type;
|
|
62 |
typedef typename re_detail::regex_iterator_traits<
|
|
63 |
BidiIterator>::value_type char_type;
|
|
64 |
typedef std::basic_string<char_type> string_type;
|
|
65 |
|
|
66 |
// construct/copy/destroy:
|
|
67 |
explicit match_results(const Allocator& a = Allocator())
|
|
68 |
#ifndef BOOST_NO_STD_ALLOCATOR
|
|
69 |
: m_subs(a), m_base() {}
|
|
70 |
#else
|
|
71 |
: m_subs(), m_base() { (void)a; }
|
|
72 |
#endif
|
|
73 |
match_results(const match_results& m)
|
|
74 |
: m_subs(m.m_subs), m_base(m.m_base) {}
|
|
75 |
match_results& operator=(const match_results& m)
|
|
76 |
{
|
|
77 |
m_subs = m.m_subs;
|
|
78 |
m_base = m.m_base;
|
|
79 |
return *this;
|
|
80 |
}
|
|
81 |
~match_results(){}
|
|
82 |
|
|
83 |
// size:
|
|
84 |
size_type size() const
|
|
85 |
{ return empty() ? 0 : m_subs.size() - 2; }
|
|
86 |
size_type max_size() const
|
|
87 |
{ return m_subs.max_size(); }
|
|
88 |
bool empty() const
|
|
89 |
{ return m_subs.size() < 2; }
|
|
90 |
// element access:
|
|
91 |
difference_type length(int sub = 0) const
|
|
92 |
{
|
|
93 |
sub += 2;
|
|
94 |
if((sub < (int)m_subs.size()) && (sub > 0))
|
|
95 |
return m_subs[sub].length();
|
|
96 |
return 0;
|
|
97 |
}
|
|
98 |
difference_type position(size_type sub = 0) const
|
|
99 |
{
|
|
100 |
sub += 2;
|
|
101 |
if(sub < m_subs.size())
|
|
102 |
{
|
|
103 |
const sub_match<BidiIterator>& s = m_subs[sub];
|
|
104 |
if(s.matched || (sub == 2))
|
|
105 |
{
|
|
106 |
return ::boost::re_detail::distance((BidiIterator)(m_base), (BidiIterator)(s.first));
|
|
107 |
}
|
|
108 |
}
|
|
109 |
return ~static_cast<difference_type>(0);
|
|
110 |
}
|
|
111 |
string_type str(int sub = 0) const
|
|
112 |
{
|
|
113 |
sub += 2;
|
|
114 |
string_type result;
|
|
115 |
if(sub < (int)m_subs.size() && (sub > 0))
|
|
116 |
{
|
|
117 |
const sub_match<BidiIterator>& s = m_subs[sub];
|
|
118 |
if(s.matched)
|
|
119 |
{
|
|
120 |
result = s.str();
|
|
121 |
}
|
|
122 |
}
|
|
123 |
return result;
|
|
124 |
}
|
|
125 |
const_reference operator[](int sub) const
|
|
126 |
{
|
|
127 |
sub += 2;
|
|
128 |
if(sub < (int)m_subs.size() && (sub >= 0))
|
|
129 |
{
|
|
130 |
return m_subs[sub];
|
|
131 |
}
|
|
132 |
return m_null;
|
|
133 |
}
|
|
134 |
|
|
135 |
const_reference prefix() const
|
|
136 |
{
|
|
137 |
return (*this)[-1];
|
|
138 |
}
|
|
139 |
|
|
140 |
const_reference suffix() const
|
|
141 |
{
|
|
142 |
return (*this)[-2];
|
|
143 |
}
|
|
144 |
const_iterator begin() const
|
|
145 |
{
|
|
146 |
return (m_subs.size() > 2) ? (m_subs.begin() + 2) : m_subs.end();
|
|
147 |
}
|
|
148 |
const_iterator end() const
|
|
149 |
{
|
|
150 |
return m_subs.end();
|
|
151 |
}
|
|
152 |
// format:
|
|
153 |
template <class OutputIterator>
|
|
154 |
OutputIterator format(OutputIterator out,
|
|
155 |
const string_type& fmt,
|
|
156 |
match_flag_type flags = format_default) const
|
|
157 |
{
|
|
158 |
re_detail::trivial_format_traits<char_type> traits;
|
|
159 |
return re_detail::regex_format_imp(out, *this, fmt.data(), fmt.data() + fmt.size(), flags, traits);
|
|
160 |
}
|
|
161 |
string_type format(const string_type& fmt,
|
|
162 |
match_flag_type flags = format_default) const
|
|
163 |
{
|
|
164 |
string_type result;
|
|
165 |
re_detail::string_out_iterator<string_type> i(result);
|
|
166 |
re_detail::trivial_format_traits<char_type> traits;
|
|
167 |
re_detail::regex_format_imp(i, *this, fmt.data(), fmt.data() + fmt.size(), flags, traits);
|
|
168 |
return result;
|
|
169 |
}
|
|
170 |
// format with locale:
|
|
171 |
template <class OutputIterator, class RegexT>
|
|
172 |
OutputIterator format(OutputIterator out,
|
|
173 |
const string_type& fmt,
|
|
174 |
match_flag_type flags,
|
|
175 |
const RegexT& re) const
|
|
176 |
{
|
|
177 |
return ::boost::re_detail::regex_format_imp(out, *this, fmt.data(), fmt.data() + fmt.size(), flags, re.get_traits());
|
|
178 |
}
|
|
179 |
template <class RegexT>
|
|
180 |
string_type format(const string_type& fmt,
|
|
181 |
match_flag_type flags,
|
|
182 |
const RegexT& re) const
|
|
183 |
{
|
|
184 |
string_type result;
|
|
185 |
re_detail::string_out_iterator<string_type> i(result);
|
|
186 |
::boost::re_detail::regex_format_imp(i, *this, fmt.data(), fmt.data() + fmt.size(), flags, re.get_traits());
|
|
187 |
return result;
|
|
188 |
}
|
|
189 |
|
|
190 |
allocator_type get_allocator() const
|
|
191 |
{
|
|
192 |
#ifndef BOOST_NO_STD_ALLOCATOR
|
|
193 |
return m_subs.get_allocator();
|
|
194 |
#else
|
|
195 |
return allocator_type();
|
|
196 |
#endif
|
|
197 |
}
|
|
198 |
void swap(match_results& that)
|
|
199 |
{
|
|
200 |
std::swap(m_subs, that.m_subs);
|
|
201 |
std::swap(m_base, that.m_base);
|
|
202 |
}
|
|
203 |
bool operator==(const match_results& that)const
|
|
204 |
{
|
|
205 |
return (m_subs == that.m_subs) && (m_base == that.m_base);
|
|
206 |
}
|
|
207 |
bool operator!=(const match_results& that)const
|
|
208 |
{ return !(*this == that); }
|
|
209 |
|
|
210 |
#ifdef BOOST_REGEX_MATCH_EXTRA
|
|
211 |
typedef typename sub_match<BidiIterator>::capture_sequence_type capture_sequence_type;
|
|
212 |
|
|
213 |
const capture_sequence_type& captures(int i)const
|
|
214 |
{
|
|
215 |
return (*this)[i].captures();
|
|
216 |
}
|
|
217 |
#endif
|
|
218 |
|
|
219 |
//
|
|
220 |
// private access functions:
|
|
221 |
void BOOST_REGEX_CALL set_second(BidiIterator i)
|
|
222 |
{
|
|
223 |
BOOST_ASSERT(m_subs.size() > 2);
|
|
224 |
m_subs[2].second = i;
|
|
225 |
m_subs[2].matched = true;
|
|
226 |
m_subs[0].first = i;
|
|
227 |
m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
|
|
228 |
m_null.first = i;
|
|
229 |
m_null.second = i;
|
|
230 |
m_null.matched = false;
|
|
231 |
}
|
|
232 |
|
|
233 |
void BOOST_REGEX_CALL set_second(BidiIterator i, size_type pos, bool m = true)
|
|
234 |
{
|
|
235 |
pos += 2;
|
|
236 |
BOOST_ASSERT(m_subs.size() > pos);
|
|
237 |
m_subs[pos].second = i;
|
|
238 |
m_subs[pos].matched = m;
|
|
239 |
if(pos == 2)
|
|
240 |
{
|
|
241 |
m_subs[0].first = i;
|
|
242 |
m_subs[0].matched = (m_subs[0].first != m_subs[0].second);
|
|
243 |
m_null.first = i;
|
|
244 |
m_null.second = i;
|
|
245 |
m_null.matched = false;
|
|
246 |
}
|
|
247 |
}
|
|
248 |
void BOOST_REGEX_CALL set_size(size_type n, BidiIterator i, BidiIterator j)
|
|
249 |
{
|
|
250 |
value_type v(j);
|
|
251 |
size_type len = m_subs.size();
|
|
252 |
if(len > n + 2)
|
|
253 |
{
|
|
254 |
m_subs.erase(m_subs.begin()+n+2, m_subs.end());
|
|
255 |
std::fill(m_subs.begin(), m_subs.end(), v);
|
|
256 |
}
|
|
257 |
else
|
|
258 |
{
|
|
259 |
std::fill(m_subs.begin(), m_subs.end(), v);
|
|
260 |
if(n+2 != len)
|
|
261 |
m_subs.insert(m_subs.end(), n+2-len, v);
|
|
262 |
}
|
|
263 |
m_subs[1].first = i;
|
|
264 |
}
|
|
265 |
void BOOST_REGEX_CALL set_base(BidiIterator pos)
|
|
266 |
{
|
|
267 |
m_base = pos;
|
|
268 |
}
|
|
269 |
BidiIterator base()const
|
|
270 |
{
|
|
271 |
return m_base;
|
|
272 |
}
|
|
273 |
void BOOST_REGEX_CALL set_first(BidiIterator i)
|
|
274 |
{
|
|
275 |
// set up prefix:
|
|
276 |
m_subs[1].second = i;
|
|
277 |
m_subs[1].matched = (m_subs[1].first != i);
|
|
278 |
// set up $0:
|
|
279 |
m_subs[2].first = i;
|
|
280 |
// zero out everything else:
|
|
281 |
for(size_type n = 3; n < m_subs.size(); ++n)
|
|
282 |
{
|
|
283 |
m_subs[n].first = m_subs[n].second = m_subs[0].second;
|
|
284 |
m_subs[n].matched = false;
|
|
285 |
}
|
|
286 |
}
|
|
287 |
void BOOST_REGEX_CALL set_first(BidiIterator i, size_type pos)
|
|
288 |
{
|
|
289 |
BOOST_ASSERT(pos+2 < m_subs.size());
|
|
290 |
if(pos)
|
|
291 |
m_subs[pos+2].first = i;
|
|
292 |
else
|
|
293 |
set_first(i);
|
|
294 |
}
|
|
295 |
void BOOST_REGEX_CALL maybe_assign(const match_results<BidiIterator, Allocator>& m);
|
|
296 |
|
|
297 |
|
|
298 |
private:
|
|
299 |
vector_type m_subs; // subexpressions
|
|
300 |
BidiIterator m_base; // where the search started from
|
|
301 |
sub_match<BidiIterator> m_null; // a null match
|
|
302 |
};
|
|
303 |
|
|
304 |
template <class BidiIterator, class Allocator>
|
|
305 |
void BOOST_REGEX_CALL match_results<BidiIterator, Allocator>::maybe_assign(const match_results<BidiIterator, Allocator>& m)
|
|
306 |
{
|
|
307 |
const_iterator p1, p2;
|
|
308 |
p1 = begin();
|
|
309 |
p2 = m.begin();
|
|
310 |
//
|
|
311 |
// Distances are measured from the start of *this* match, unless this isn't
|
|
312 |
// a valid match in which case we use the start of the whole sequence. Note that
|
|
313 |
// no subsequent match-candidate can ever be to the left of the first match found.
|
|
314 |
// This ensures that when we are using bidirectional iterators, that distances
|
|
315 |
// measured are as short as possible, and therefore as efficient as possible
|
|
316 |
// to compute. Finally note that we don't use the "matched" data member to test
|
|
317 |
// whether a sub-expression is a valid match, because partial matches set this
|
|
318 |
// to false for sub-expression 0.
|
|
319 |
//
|
|
320 |
BidiIterator l_end = this->suffix().second;
|
|
321 |
BidiIterator l_base = (p1->first == l_end) ? this->prefix().first : (*this)[0].first;
|
|
322 |
difference_type len1 = 0;
|
|
323 |
difference_type len2 = 0;
|
|
324 |
difference_type base1 = 0;
|
|
325 |
difference_type base2 = 0;
|
|
326 |
std::size_t i;
|
|
327 |
for(i = 0; i < size(); ++i, ++p1, ++p2)
|
|
328 |
{
|
|
329 |
//
|
|
330 |
// Leftmost takes priority over longest; handle special cases
|
|
331 |
// where distances need not be computed first (an optimisation
|
|
332 |
// for bidirectional iterators: ensure that we don't accidently
|
|
333 |
// compute the length of the whole sequence, as this can be really
|
|
334 |
// expensive).
|
|
335 |
//
|
|
336 |
if(p1->first == l_end)
|
|
337 |
{
|
|
338 |
if(p2->first != l_end)
|
|
339 |
{
|
|
340 |
// p2 must be better than p1, and no need to calculate
|
|
341 |
// actual distances:
|
|
342 |
base1 = 1;
|
|
343 |
base2 = 0;
|
|
344 |
break;
|
|
345 |
}
|
|
346 |
else
|
|
347 |
{
|
|
348 |
// *p1 and *p2 are either unmatched or match end-of sequence,
|
|
349 |
// either way no need to calculate distances:
|
|
350 |
if((p1->matched == false) && (p2->matched == true))
|
|
351 |
break;
|
|
352 |
if((p1->matched == true) && (p2->matched == false))
|
|
353 |
return;
|
|
354 |
continue;
|
|
355 |
}
|
|
356 |
}
|
|
357 |
else if(p2->first == l_end)
|
|
358 |
{
|
|
359 |
// p1 better than p2, and no need to calculate distances:
|
|
360 |
return;
|
|
361 |
}
|
|
362 |
base1 = ::boost::re_detail::distance(l_base, p1->first);
|
|
363 |
base2 = ::boost::re_detail::distance(l_base, p2->first);
|
|
364 |
BOOST_ASSERT(base1 >= 0);
|
|
365 |
BOOST_ASSERT(base2 >= 0);
|
|
366 |
if(base1 < base2) return;
|
|
367 |
if(base2 < base1) break;
|
|
368 |
|
|
369 |
len1 = ::boost::re_detail::distance((BidiIterator)p1->first, (BidiIterator)p1->second);
|
|
370 |
len2 = ::boost::re_detail::distance((BidiIterator)p2->first, (BidiIterator)p2->second);
|
|
371 |
BOOST_ASSERT(len1 >= 0);
|
|
372 |
BOOST_ASSERT(len2 >= 0);
|
|
373 |
if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
|
|
374 |
break;
|
|
375 |
if((p1->matched == true) && (p2->matched == false))
|
|
376 |
return;
|
|
377 |
}
|
|
378 |
if(i == size())
|
|
379 |
return;
|
|
380 |
if(base2 < base1)
|
|
381 |
*this = m;
|
|
382 |
else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
|
|
383 |
*this = m;
|
|
384 |
}
|
|
385 |
|
|
386 |
template <class BidiIterator, class Allocator>
|
|
387 |
void swap(match_results<BidiIterator, Allocator>& a, match_results<BidiIterator, Allocator>& b)
|
|
388 |
{
|
|
389 |
a.swap(b);
|
|
390 |
}
|
|
391 |
|
|
392 |
#ifndef BOOST_NO_STD_LOCALE
|
|
393 |
template <class charT, class traits, class BidiIterator, class Allocator>
|
|
394 |
std::basic_ostream<charT, traits>&
|
|
395 |
operator << (std::basic_ostream<charT, traits>& os,
|
|
396 |
const match_results<BidiIterator, Allocator>& s)
|
|
397 |
{
|
|
398 |
return (os << s.str());
|
|
399 |
}
|
|
400 |
#else
|
|
401 |
template <class BidiIterator, class Allocator>
|
|
402 |
std::ostream& operator << (std::ostream& os,
|
|
403 |
const match_results<BidiIterator, Allocator>& s)
|
|
404 |
{
|
|
405 |
return (os << s.str());
|
|
406 |
}
|
|
407 |
#endif
|
|
408 |
|
|
409 |
#ifdef BOOST_MSVC
|
|
410 |
#pragma warning(pop)
|
|
411 |
#endif
|
|
412 |
} // namespace boost
|
|
413 |
|
|
414 |
#ifdef BOOST_MSVC
|
|
415 |
#pragma warning(push)
|
|
416 |
#pragma warning(disable: 4103)
|
|
417 |
#endif
|
|
418 |
#ifdef BOOST_HAS_ABI_HEADERS
|
|
419 |
# include BOOST_ABI_SUFFIX
|
|
420 |
#endif
|
|
421 |
#ifdef BOOST_MSVC
|
|
422 |
#pragma warning(pop)
|
|
423 |
#endif
|
|
424 |
|
|
425 |
#endif
|
|
426 |
|
|
427 |
|