annotate DEPENDENCIES/generic/include/boost/regex/v4/match_results.hpp @ 16:2665513ce2d3

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