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