annotate DEPENDENCIES/generic/include/boost/xpressive/sub_match.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 /// \file sub_match.hpp
Chris@16 3 /// Contains the definition of the class template sub_match\<\>
Chris@16 4 /// and associated helper functions
Chris@16 5 //
Chris@16 6 // Copyright 2008 Eric Niebler. Distributed under the Boost
Chris@16 7 // 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 #ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
Chris@16 11 #define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
Chris@16 12
Chris@16 13 // MS compatible compilers support #pragma once
Chris@16 14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
Chris@16 15 # pragma once
Chris@16 16 #endif
Chris@16 17
Chris@16 18 #include <iosfwd>
Chris@16 19 #include <string>
Chris@16 20 #include <utility>
Chris@16 21 #include <iterator>
Chris@16 22 #include <algorithm>
Chris@16 23 #include <boost/mpl/assert.hpp>
Chris@16 24 #include <boost/type_traits/is_same.hpp>
Chris@16 25 #include <boost/iterator/iterator_traits.hpp>
Chris@16 26 #include <boost/xpressive/detail/detail_fwd.hpp>
Chris@16 27
Chris@16 28 //{{AFX_DOC_COMMENT
Chris@16 29 ///////////////////////////////////////////////////////////////////////////////
Chris@16 30 // This is a hack to get Doxygen to show the inheritance relation between
Chris@16 31 // sub_match<T> and std::pair<T,T>.
Chris@16 32 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
Chris@16 33 /// INTERNAL ONLY
Chris@16 34 namespace std
Chris@16 35 {
Chris@16 36 /// INTERNAL ONLY
Chris@16 37 template<typename, typename> struct pair {};
Chris@16 38 }
Chris@16 39 #endif
Chris@16 40 //}}AFX_DOC_COMMENT
Chris@16 41
Chris@16 42 namespace boost { namespace xpressive
Chris@16 43 {
Chris@16 44
Chris@16 45 ///////////////////////////////////////////////////////////////////////////////
Chris@16 46 // sub_match
Chris@16 47 //
Chris@16 48 /// \brief Class template \c sub_match denotes the sequence of characters matched by a particular
Chris@16 49 /// marked sub-expression.
Chris@16 50 ///
Chris@16 51 /// When the marked sub-expression denoted by an object of type \c sub_match\<\> participated in a
Chris@16 52 /// regular expression match then member \c matched evaluates to \c true, and members \c first and \c second
Chris@16 53 /// denote the range of characters <tt>[first,second)</tt> which formed that match. Otherwise \c matched is \c false,
Chris@16 54 /// and members \c first and \c second contained undefined values.
Chris@16 55 ///
Chris@16 56 /// If an object of type \c sub_match\<\> represents sub-expression 0 - that is to say the whole match -
Chris@16 57 /// then member \c matched is always \c true, unless a partial match was obtained as a result of the flag
Chris@16 58 /// \c match_partial being passed to a regular expression algorithm, in which case member \c matched is
Chris@16 59 /// \c false, and members \c first and \c second represent the character range that formed the partial match.
Chris@16 60 template<typename BidiIter>
Chris@16 61 struct sub_match
Chris@16 62 : std::pair<BidiIter, BidiIter>
Chris@16 63 {
Chris@16 64 private:
Chris@16 65 /// INTERNAL ONLY
Chris@16 66 ///
Chris@16 67 struct dummy { int i_; };
Chris@16 68 typedef int dummy::*bool_type;
Chris@16 69
Chris@16 70 public:
Chris@16 71 typedef typename iterator_value<BidiIter>::type value_type;
Chris@16 72 typedef typename iterator_difference<BidiIter>::type difference_type;
Chris@16 73 typedef typename detail::string_type<value_type>::type string_type;
Chris@16 74 typedef BidiIter iterator;
Chris@16 75
Chris@16 76 sub_match()
Chris@16 77 : std::pair<BidiIter, BidiIter>()
Chris@16 78 , matched(false)
Chris@16 79 {
Chris@16 80 }
Chris@16 81
Chris@16 82 sub_match(BidiIter first, BidiIter second, bool matched_ = false)
Chris@16 83 : std::pair<BidiIter, BidiIter>(first, second)
Chris@16 84 , matched(matched_)
Chris@16 85 {
Chris@16 86 }
Chris@16 87
Chris@16 88 string_type str() const
Chris@16 89 {
Chris@16 90 return this->matched ? string_type(this->first, this->second) : string_type();
Chris@16 91 }
Chris@16 92
Chris@16 93 operator string_type() const
Chris@16 94 {
Chris@16 95 return this->matched ? string_type(this->first, this->second) : string_type();
Chris@16 96 }
Chris@16 97
Chris@16 98 difference_type length() const
Chris@16 99 {
Chris@16 100 return this->matched ? std::distance(this->first, this->second) : 0;
Chris@16 101 }
Chris@16 102
Chris@16 103 operator bool_type() const
Chris@16 104 {
Chris@16 105 return this->matched ? &dummy::i_ : 0;
Chris@16 106 }
Chris@16 107
Chris@16 108 bool operator !() const
Chris@16 109 {
Chris@16 110 return !this->matched;
Chris@16 111 }
Chris@16 112
Chris@16 113 /// \brief Performs a lexicographic string comparison
Chris@16 114 /// \param str the string against which to compare
Chris@16 115 /// \return the results of <tt>(*this).str().compare(str)</tt>
Chris@16 116 int compare(string_type const &str) const
Chris@16 117 {
Chris@16 118 return this->str().compare(str);
Chris@16 119 }
Chris@16 120
Chris@16 121 /// \overload
Chris@16 122 ///
Chris@16 123 int compare(sub_match const &sub) const
Chris@16 124 {
Chris@16 125 return this->str().compare(sub.str());
Chris@16 126 }
Chris@16 127
Chris@16 128 /// \overload
Chris@16 129 ///
Chris@16 130 int compare(value_type const *ptr) const
Chris@16 131 {
Chris@16 132 return this->str().compare(ptr);
Chris@16 133 }
Chris@16 134
Chris@16 135 /// \brief true if this sub-match participated in the full match.
Chris@16 136 bool matched;
Chris@16 137 };
Chris@16 138
Chris@16 139 ///////////////////////////////////////////////////////////////////////////////
Chris@16 140 /// \brief \c range_begin() to make \c sub_match\<\> a valid range
Chris@16 141 /// \param sub the \c sub_match\<\> object denoting the range
Chris@16 142 /// \return \c sub.first
Chris@16 143 /// \pre \c sub.first is not singular
Chris@16 144 template<typename BidiIter>
Chris@16 145 inline BidiIter range_begin(sub_match<BidiIter> &sub)
Chris@16 146 {
Chris@16 147 return sub.first;
Chris@16 148 }
Chris@16 149
Chris@16 150 /// \overload
Chris@16 151 ///
Chris@16 152 template<typename BidiIter>
Chris@16 153 inline BidiIter range_begin(sub_match<BidiIter> const &sub)
Chris@16 154 {
Chris@16 155 return sub.first;
Chris@16 156 }
Chris@16 157
Chris@16 158 ///////////////////////////////////////////////////////////////////////////////
Chris@16 159 /// \brief \c range_end() to make \c sub_match\<\> a valid range
Chris@16 160 /// \param sub the \c sub_match\<\> object denoting the range
Chris@16 161 /// \return \c sub.second
Chris@16 162 /// \pre \c sub.second is not singular
Chris@16 163 template<typename BidiIter>
Chris@16 164 inline BidiIter range_end(sub_match<BidiIter> &sub)
Chris@16 165 {
Chris@16 166 return sub.second;
Chris@16 167 }
Chris@16 168
Chris@16 169 /// \overload
Chris@16 170 ///
Chris@16 171 template<typename BidiIter>
Chris@16 172 inline BidiIter range_end(sub_match<BidiIter> const &sub)
Chris@16 173 {
Chris@16 174 return sub.second;
Chris@16 175 }
Chris@16 176
Chris@16 177 ///////////////////////////////////////////////////////////////////////////////
Chris@16 178 /// \brief insertion operator for sending sub-matches to ostreams
Chris@16 179 /// \param sout output stream.
Chris@16 180 /// \param sub sub_match object to be written to the stream.
Chris@16 181 /// \return sout \<\< sub.str()
Chris@16 182 template<typename BidiIter, typename Char, typename Traits>
Chris@16 183 inline std::basic_ostream<Char, Traits> &operator <<
Chris@16 184 (
Chris@16 185 std::basic_ostream<Char, Traits> &sout
Chris@16 186 , sub_match<BidiIter> const &sub
Chris@16 187 )
Chris@16 188 {
Chris@16 189 typedef typename iterator_value<BidiIter>::type char_type;
Chris@16 190 BOOST_MPL_ASSERT_MSG(
Chris@16 191 (boost::is_same<Char, char_type>::value)
Chris@16 192 , CHARACTER_TYPES_OF_STREAM_AND_SUB_MATCH_MUST_MATCH
Chris@16 193 , (Char, char_type)
Chris@16 194 );
Chris@16 195 if(sub.matched)
Chris@16 196 {
Chris@16 197 std::ostream_iterator<char_type, Char, Traits> iout(sout);
Chris@16 198 std::copy(sub.first, sub.second, iout);
Chris@16 199 }
Chris@16 200 return sout;
Chris@16 201 }
Chris@16 202
Chris@16 203
Chris@16 204 // BUGBUG make these more efficient
Chris@16 205
Chris@16 206 template<typename BidiIter>
Chris@16 207 bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 208 {
Chris@16 209 return lhs.compare(rhs) == 0;
Chris@16 210 }
Chris@16 211
Chris@16 212 template<typename BidiIter>
Chris@16 213 bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 214 {
Chris@16 215 return lhs.compare(rhs) != 0;
Chris@16 216 }
Chris@16 217
Chris@16 218 template<typename BidiIter>
Chris@16 219 bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 220 {
Chris@16 221 return lhs.compare(rhs) < 0;
Chris@16 222 }
Chris@16 223
Chris@16 224 template<typename BidiIter>
Chris@16 225 bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 226 {
Chris@16 227 return lhs.compare(rhs) <= 0;
Chris@16 228 }
Chris@16 229
Chris@16 230 template<typename BidiIter>
Chris@16 231 bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 232 {
Chris@16 233 return lhs.compare(rhs) >= 0;
Chris@16 234 }
Chris@16 235
Chris@16 236 template<typename BidiIter>
Chris@16 237 bool operator > (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 238 {
Chris@16 239 return lhs.compare(rhs) > 0;
Chris@16 240 }
Chris@16 241
Chris@16 242 template<typename BidiIter>
Chris@16 243 bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 244 {
Chris@16 245 return lhs == rhs.str();
Chris@16 246 }
Chris@16 247
Chris@16 248 template<typename BidiIter>
Chris@16 249 bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 250 {
Chris@16 251 return lhs != rhs.str();
Chris@16 252 }
Chris@16 253
Chris@16 254 template<typename BidiIter>
Chris@16 255 bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 256 {
Chris@16 257 return lhs < rhs.str();
Chris@16 258 }
Chris@16 259
Chris@16 260 template<typename BidiIter>
Chris@16 261 bool operator > (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 262 {
Chris@16 263 return lhs> rhs.str();
Chris@16 264 }
Chris@16 265
Chris@16 266 template<typename BidiIter>
Chris@16 267 bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 268 {
Chris@16 269 return lhs >= rhs.str();
Chris@16 270 }
Chris@16 271
Chris@16 272 template<typename BidiIter>
Chris@16 273 bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 274 {
Chris@16 275 return lhs <= rhs.str();
Chris@16 276 }
Chris@16 277
Chris@16 278 template<typename BidiIter>
Chris@16 279 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 280 {
Chris@16 281 return lhs.str() == rhs;
Chris@16 282 }
Chris@16 283
Chris@16 284 template<typename BidiIter>
Chris@16 285 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 286 {
Chris@16 287 return lhs.str() != rhs;
Chris@16 288 }
Chris@16 289
Chris@16 290 template<typename BidiIter>
Chris@16 291 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 292 {
Chris@16 293 return lhs.str() < rhs;
Chris@16 294 }
Chris@16 295
Chris@16 296 template<typename BidiIter>
Chris@16 297 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 298 {
Chris@16 299 return lhs.str() > rhs;
Chris@16 300 }
Chris@16 301
Chris@16 302 template<typename BidiIter>
Chris@16 303 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 304 {
Chris@16 305 return lhs.str() >= rhs;
Chris@16 306 }
Chris@16 307
Chris@16 308 template<typename BidiIter>
Chris@16 309 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 310 {
Chris@16 311 return lhs.str() <= rhs;
Chris@16 312 }
Chris@16 313
Chris@16 314 template<typename BidiIter>
Chris@16 315 bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 316 {
Chris@16 317 return lhs == rhs.str();
Chris@16 318 }
Chris@16 319
Chris@16 320 template<typename BidiIter>
Chris@16 321 bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 322 {
Chris@16 323 return lhs != rhs.str();
Chris@16 324 }
Chris@16 325
Chris@16 326 template<typename BidiIter>
Chris@16 327 bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 328 {
Chris@16 329 return lhs < rhs.str();
Chris@16 330 }
Chris@16 331
Chris@16 332 template<typename BidiIter>
Chris@16 333 bool operator > (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 334 {
Chris@16 335 return lhs> rhs.str();
Chris@16 336 }
Chris@16 337
Chris@16 338 template<typename BidiIter>
Chris@16 339 bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 340 {
Chris@16 341 return lhs >= rhs.str();
Chris@16 342 }
Chris@16 343
Chris@16 344 template<typename BidiIter>
Chris@16 345 bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 346 {
Chris@16 347 return lhs <= rhs.str();
Chris@16 348 }
Chris@16 349
Chris@16 350 template<typename BidiIter>
Chris@16 351 bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 352 {
Chris@16 353 return lhs.str() == rhs;
Chris@16 354 }
Chris@16 355
Chris@16 356 template<typename BidiIter>
Chris@16 357 bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 358 {
Chris@16 359 return lhs.str() != rhs;
Chris@16 360 }
Chris@16 361
Chris@16 362 template<typename BidiIter>
Chris@16 363 bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 364 {
Chris@16 365 return lhs.str() < rhs;
Chris@16 366 }
Chris@16 367
Chris@16 368 template<typename BidiIter>
Chris@16 369 bool operator > (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 370 {
Chris@16 371 return lhs.str() > rhs;
Chris@16 372 }
Chris@16 373
Chris@16 374 template<typename BidiIter>
Chris@16 375 bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 376 {
Chris@16 377 return lhs.str() >= rhs;
Chris@16 378 }
Chris@16 379
Chris@16 380 template<typename BidiIter>
Chris@16 381 bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 382 {
Chris@16 383 return lhs.str() <= rhs;
Chris@16 384 }
Chris@16 385
Chris@16 386 // Operator+ convenience function
Chris@16 387 template<typename BidiIter>
Chris@16 388 typename sub_match<BidiIter>::string_type
Chris@16 389 operator + (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 390 {
Chris@16 391 return lhs.str() + rhs.str();
Chris@16 392 }
Chris@16 393
Chris@16 394 template<typename BidiIter>
Chris@16 395 typename sub_match<BidiIter>::string_type
Chris@16 396 operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
Chris@16 397 {
Chris@16 398 return lhs.str() + rhs;
Chris@16 399 }
Chris@16 400
Chris@16 401 template<typename BidiIter>
Chris@16 402 typename sub_match<BidiIter>::string_type
Chris@16 403 operator + (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 404 {
Chris@16 405 return lhs + rhs.str();
Chris@16 406 }
Chris@16 407
Chris@16 408 template<typename BidiIter>
Chris@16 409 typename sub_match<BidiIter>::string_type
Chris@16 410 operator + (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
Chris@16 411 {
Chris@16 412 return lhs.str() + rhs;
Chris@16 413 }
Chris@16 414
Chris@16 415 template<typename BidiIter>
Chris@16 416 typename sub_match<BidiIter>::string_type
Chris@16 417 operator + (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
Chris@16 418 {
Chris@16 419 return lhs + rhs.str();
Chris@16 420 }
Chris@16 421
Chris@16 422 template<typename BidiIter>
Chris@16 423 typename sub_match<BidiIter>::string_type
Chris@16 424 operator + (sub_match<BidiIter> const &lhs, typename sub_match<BidiIter>::string_type const &rhs)
Chris@16 425 {
Chris@16 426 return lhs.str() + rhs;
Chris@16 427 }
Chris@16 428
Chris@16 429 template<typename BidiIter>
Chris@16 430 typename sub_match<BidiIter>::string_type
Chris@16 431 operator + (typename sub_match<BidiIter>::string_type const &lhs, sub_match<BidiIter> const &rhs)
Chris@16 432 {
Chris@16 433 return lhs + rhs.str();
Chris@16 434 }
Chris@16 435
Chris@16 436 }} // namespace boost::xpressive
Chris@16 437
Chris@16 438 // Hook the Boost.Range customization points to make sub_match a valid range.
Chris@16 439 namespace boost
Chris@16 440 {
Chris@16 441 /// INTERNAL ONLY
Chris@16 442 ///
Chris@16 443 template<typename Range>
Chris@16 444 struct range_mutable_iterator;
Chris@16 445
Chris@16 446 /// INTERNAL ONLY
Chris@16 447 ///
Chris@16 448 template<typename BidiIter>
Chris@16 449 struct range_mutable_iterator<xpressive::sub_match<BidiIter> >
Chris@16 450 {
Chris@16 451 typedef BidiIter type;
Chris@16 452 };
Chris@16 453
Chris@16 454 /// INTERNAL ONLY
Chris@16 455 ///
Chris@16 456 template<typename Range>
Chris@16 457 struct range_const_iterator;
Chris@16 458
Chris@16 459 /// INTERNAL ONLY
Chris@16 460 ///
Chris@16 461 template<typename BidiIter>
Chris@16 462 struct range_const_iterator<xpressive::sub_match<BidiIter> >
Chris@16 463 {
Chris@16 464 typedef BidiIter type;
Chris@16 465 };
Chris@16 466 }
Chris@16 467
Chris@16 468 #endif