os/ossrv/ossrv_pub/boost_apis/boost/xpressive/sub_match.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
///////////////////////////////////////////////////////////////////////////////
sl@0
     2
/// \file sub_match.hpp
sl@0
     3
/// Contains the definition of the class template sub_match\<\>
sl@0
     4
/// and associated helper functions
sl@0
     5
//
sl@0
     6
//  Copyright 2004 Eric Niebler. Distributed under the Boost
sl@0
     7
//  Software License, Version 1.0. (See accompanying file
sl@0
     8
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     9
sl@0
    10
#ifndef BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
sl@0
    11
#define BOOST_XPRESSIVE_SUB_MATCH_HPP_EAN_10_04_2005
sl@0
    12
sl@0
    13
// MS compatible compilers support #pragma once
sl@0
    14
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
sl@0
    15
# pragma once
sl@0
    16
#endif
sl@0
    17
sl@0
    18
#include <iosfwd>
sl@0
    19
#include <string>
sl@0
    20
#include <utility>
sl@0
    21
#include <iterator>
sl@0
    22
#include <algorithm>
sl@0
    23
#include <boost/iterator/iterator_traits.hpp>
sl@0
    24
sl@0
    25
//{{AFX_DOC_COMMENT
sl@0
    26
///////////////////////////////////////////////////////////////////////////////
sl@0
    27
// This is a hack to get Doxygen to show the inheritance relation between
sl@0
    28
// sub_match<T> and std::pair<T,T>.
sl@0
    29
#ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED
sl@0
    30
/// INTERNAL ONLY
sl@0
    31
namespace std
sl@0
    32
{
sl@0
    33
    /// INTERNAL ONLY
sl@0
    34
    template<typename, typename> struct pair {};
sl@0
    35
}
sl@0
    36
#endif
sl@0
    37
//}}AFX_DOC_COMMENT
sl@0
    38
sl@0
    39
namespace boost { namespace xpressive
sl@0
    40
{
sl@0
    41
sl@0
    42
///////////////////////////////////////////////////////////////////////////////
sl@0
    43
// sub_match
sl@0
    44
//
sl@0
    45
/// \brief Class template sub_match denotes the sequence of characters matched by a particular marked sub-expression.
sl@0
    46
///
sl@0
    47
/// When the marked sub-expression denoted by an object of type sub_match\<\> participated in a
sl@0
    48
/// regular expression match then member matched evaluates to true, and members first and second
sl@0
    49
/// denote the range of characters [first,second) which formed that match. Otherwise matched is false,
sl@0
    50
/// and members first and second contained undefined values.
sl@0
    51
///
sl@0
    52
/// If an object of type sub_match\<\> represents sub-expression 0 - that is to say the whole match -
sl@0
    53
/// then member matched is always true, unless a partial match was obtained as a result of the flag
sl@0
    54
/// match_partial being passed to a regular expression algorithm, in which case member matched is
sl@0
    55
/// false, and members first and second represent the character range that formed the partial match.
sl@0
    56
template<typename BidiIter>
sl@0
    57
struct sub_match
sl@0
    58
  : std::pair<BidiIter, BidiIter>
sl@0
    59
{
sl@0
    60
private:
sl@0
    61
    struct dummy { int i_; };
sl@0
    62
    typedef int dummy::*bool_type;
sl@0
    63
sl@0
    64
public:
sl@0
    65
    typedef typename iterator_value<BidiIter>::type value_type;
sl@0
    66
    typedef typename iterator_difference<BidiIter>::type difference_type;
sl@0
    67
    typedef std::basic_string<value_type> string_type;
sl@0
    68
    typedef BidiIter iterator;
sl@0
    69
sl@0
    70
    explicit sub_match(BidiIter first = BidiIter(), BidiIter second = BidiIter(), bool matched_ = false)
sl@0
    71
      : std::pair<BidiIter, BidiIter>(first, second)
sl@0
    72
      , matched(matched_)
sl@0
    73
    {
sl@0
    74
    }
sl@0
    75
sl@0
    76
    string_type str() const
sl@0
    77
    {
sl@0
    78
        return this->matched ? string_type(this->first, this->second) : string_type();
sl@0
    79
    }
sl@0
    80
sl@0
    81
    operator string_type() const
sl@0
    82
    {
sl@0
    83
        return this->matched ? string_type(this->first, this->second) : string_type();
sl@0
    84
    }
sl@0
    85
sl@0
    86
    difference_type length() const
sl@0
    87
    {
sl@0
    88
        return this->matched ? std::distance(this->first, this->second) : 0;
sl@0
    89
    }
sl@0
    90
sl@0
    91
    operator bool_type() const
sl@0
    92
    {
sl@0
    93
        return this->matched ? &dummy::i_ : 0;
sl@0
    94
    }
sl@0
    95
sl@0
    96
    bool operator !() const
sl@0
    97
    {
sl@0
    98
        return !this->matched;
sl@0
    99
    }
sl@0
   100
sl@0
   101
    /// \brief Performs a lexicographic string comparison
sl@0
   102
    /// \param str the string against which to compare
sl@0
   103
    /// \return the results of (*this).str().compare(str)
sl@0
   104
    int compare(string_type const &str) const
sl@0
   105
    {
sl@0
   106
        return this->str().compare(str);
sl@0
   107
    }
sl@0
   108
sl@0
   109
    /// \overload
sl@0
   110
    int compare(sub_match const &sub) const
sl@0
   111
    {
sl@0
   112
        return this->str().compare(sub.str());
sl@0
   113
    }
sl@0
   114
sl@0
   115
    /// \overload
sl@0
   116
    int compare(value_type const *ptr) const
sl@0
   117
    {
sl@0
   118
        return this->str().compare(ptr);
sl@0
   119
    }
sl@0
   120
sl@0
   121
    /// \brief true if this sub-match participated in the full match.
sl@0
   122
    bool matched;
sl@0
   123
};
sl@0
   124
sl@0
   125
///////////////////////////////////////////////////////////////////////////////
sl@0
   126
/// \brief insertion operator for sending sub-matches to ostreams
sl@0
   127
/// \param sout output stream.
sl@0
   128
/// \param sub sub_match object to be written to the stream.
sl@0
   129
/// \return sout \<\< sub.str()
sl@0
   130
template<typename BidiIter, typename Char, typename Traits>
sl@0
   131
inline std::basic_ostream<Char, Traits> &operator <<
sl@0
   132
(
sl@0
   133
    std::basic_ostream<Char, Traits> &sout
sl@0
   134
  , sub_match<BidiIter> const &sub
sl@0
   135
)
sl@0
   136
{
sl@0
   137
    typedef typename iterator_value<BidiIter>::type char_type;
sl@0
   138
    if(sub.matched)
sl@0
   139
    {
sl@0
   140
        std::ostream_iterator<char_type, Char, Traits> iout(sout);
sl@0
   141
        std::copy(sub.first, sub.second, iout);
sl@0
   142
    }
sl@0
   143
    return sout;
sl@0
   144
}
sl@0
   145
sl@0
   146
sl@0
   147
// BUGBUG make these more efficient
sl@0
   148
sl@0
   149
template<typename BidiIter>
sl@0
   150
bool operator == (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   151
{
sl@0
   152
    return lhs.compare(rhs) == 0;
sl@0
   153
}
sl@0
   154
sl@0
   155
template<typename BidiIter>
sl@0
   156
bool operator != (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   157
{
sl@0
   158
    return lhs.compare(rhs) != 0;
sl@0
   159
}
sl@0
   160
sl@0
   161
template<typename BidiIter>
sl@0
   162
bool operator < (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   163
{
sl@0
   164
    return lhs.compare(rhs) < 0;
sl@0
   165
}
sl@0
   166
sl@0
   167
template<typename BidiIter>
sl@0
   168
bool operator <= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   169
{
sl@0
   170
    return lhs.compare(rhs) <= 0;
sl@0
   171
}
sl@0
   172
sl@0
   173
template<typename BidiIter>
sl@0
   174
bool operator >= (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   175
{
sl@0
   176
    return lhs.compare(rhs)>= 0;
sl@0
   177
}
sl@0
   178
sl@0
   179
template<typename BidiIter>
sl@0
   180
bool operator> (sub_match<BidiIter> const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   181
{
sl@0
   182
    return lhs.compare(rhs)> 0;
sl@0
   183
}
sl@0
   184
sl@0
   185
template<typename BidiIter>
sl@0
   186
bool operator == (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   187
{
sl@0
   188
    return lhs == rhs.str();
sl@0
   189
}
sl@0
   190
sl@0
   191
template<typename BidiIter>
sl@0
   192
bool operator != (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   193
{
sl@0
   194
    return lhs != rhs.str();
sl@0
   195
}
sl@0
   196
sl@0
   197
template<typename BidiIter>
sl@0
   198
bool operator < (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   199
{
sl@0
   200
    return lhs < rhs.str();
sl@0
   201
}
sl@0
   202
sl@0
   203
template<typename BidiIter>
sl@0
   204
bool operator> (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   205
{
sl@0
   206
    return lhs> rhs.str();
sl@0
   207
}
sl@0
   208
sl@0
   209
template<typename BidiIter>
sl@0
   210
bool operator >= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   211
{
sl@0
   212
    return lhs >= rhs.str();
sl@0
   213
}
sl@0
   214
sl@0
   215
template<typename BidiIter>
sl@0
   216
bool operator <= (typename iterator_value<BidiIter>::type const *lhs, sub_match<BidiIter> const &rhs)
sl@0
   217
{
sl@0
   218
    return lhs <= rhs.str();
sl@0
   219
}
sl@0
   220
sl@0
   221
template<typename BidiIter>
sl@0
   222
bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   223
{
sl@0
   224
    return lhs.str() == rhs;
sl@0
   225
}
sl@0
   226
sl@0
   227
template<typename BidiIter>
sl@0
   228
bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   229
{
sl@0
   230
    return lhs.str() != rhs;
sl@0
   231
}
sl@0
   232
sl@0
   233
template<typename BidiIter>
sl@0
   234
bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   235
{
sl@0
   236
    return lhs.str() < rhs;
sl@0
   237
}
sl@0
   238
sl@0
   239
template<typename BidiIter>
sl@0
   240
bool operator> (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   241
{
sl@0
   242
    return lhs.str()> rhs;
sl@0
   243
}
sl@0
   244
sl@0
   245
template<typename BidiIter>
sl@0
   246
bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   247
{
sl@0
   248
    return lhs.str()>= rhs;
sl@0
   249
}
sl@0
   250
sl@0
   251
template<typename BidiIter>
sl@0
   252
bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const *rhs)
sl@0
   253
{
sl@0
   254
    return lhs.str() <= rhs;
sl@0
   255
}
sl@0
   256
sl@0
   257
template<typename BidiIter>
sl@0
   258
bool operator == (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   259
{
sl@0
   260
    return lhs == rhs.str();
sl@0
   261
}
sl@0
   262
sl@0
   263
template<typename BidiIter>
sl@0
   264
bool operator != (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   265
{
sl@0
   266
    return lhs != rhs.str();
sl@0
   267
}
sl@0
   268
sl@0
   269
template<typename BidiIter>
sl@0
   270
bool operator < (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   271
{
sl@0
   272
    return lhs < rhs.str();
sl@0
   273
}
sl@0
   274
sl@0
   275
template<typename BidiIter>
sl@0
   276
bool operator> (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   277
{
sl@0
   278
    return lhs> rhs.str();
sl@0
   279
}
sl@0
   280
sl@0
   281
template<typename BidiIter>
sl@0
   282
bool operator >= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   283
{
sl@0
   284
    return lhs >= rhs.str();
sl@0
   285
}
sl@0
   286
sl@0
   287
template<typename BidiIter>
sl@0
   288
bool operator <= (typename iterator_value<BidiIter>::type const &lhs, sub_match<BidiIter> const &rhs)
sl@0
   289
{
sl@0
   290
    return lhs <= rhs.str();
sl@0
   291
}
sl@0
   292
sl@0
   293
template<typename BidiIter>
sl@0
   294
bool operator == (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   295
{
sl@0
   296
    return lhs.str() == rhs;
sl@0
   297
}
sl@0
   298
sl@0
   299
template<typename BidiIter>
sl@0
   300
bool operator != (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   301
{
sl@0
   302
    return lhs.str() != rhs;
sl@0
   303
}
sl@0
   304
sl@0
   305
template<typename BidiIter>
sl@0
   306
bool operator < (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   307
{
sl@0
   308
    return lhs.str() < rhs;
sl@0
   309
}
sl@0
   310
sl@0
   311
template<typename BidiIter>
sl@0
   312
bool operator> (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   313
{
sl@0
   314
    return lhs.str()> rhs;
sl@0
   315
}
sl@0
   316
sl@0
   317
template<typename BidiIter>
sl@0
   318
bool operator >= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   319
{
sl@0
   320
    return lhs.str()>= rhs;
sl@0
   321
}
sl@0
   322
sl@0
   323
template<typename BidiIter>
sl@0
   324
bool operator <= (sub_match<BidiIter> const &lhs, typename iterator_value<BidiIter>::type const &rhs)
sl@0
   325
{
sl@0
   326
    return lhs.str() <= rhs;
sl@0
   327
}
sl@0
   328
sl@0
   329
}} // namespace boost::xpressive
sl@0
   330
sl@0
   331
#endif