annotate DEPENDENCIES/generic/include/boost/regex/v4/match_results.hpp @ 133:4acb5d8d80b6 tip

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