| 1 |
/// \file |
| 2 |
// Range v3 library |
| 3 |
// |
| 4 |
// Copyright Eric Niebler 2013-present |
| 5 |
// |
| 6 |
// Use, modification and distribution is subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying |
| 8 |
// file LICENSE_1_0.txt or copy at |
| 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 10 |
// |
| 11 |
// Project home: https://github.com/ericniebler/range-v3 |
| 12 |
// |
| 13 |
|
| 14 |
#ifndef RANGES_V3_VIEW_TOKENIZE_HPP |
| 15 |
#define RANGES_V3_VIEW_TOKENIZE_HPP |
| 16 |
|
| 17 |
#include <initializer_list> |
| 18 |
#include <regex> |
| 19 |
#include <type_traits> |
| 20 |
#include <utility> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include <range/v3/range_fwd.hpp> |
| 24 |
|
| 25 |
#include <range/v3/functional/bind_back.hpp> |
| 26 |
#include <range/v3/range/access.hpp> |
| 27 |
#include <range/v3/range/concepts.hpp> |
| 28 |
#include <range/v3/utility/static_const.hpp> |
| 29 |
#include <range/v3/view/all.hpp> |
| 30 |
#include <range/v3/view/interface.hpp> |
| 31 |
#include <range/v3/view/view.hpp> |
| 32 |
|
| 33 |
#include <range/v3/detail/prologue.hpp> |
| 34 |
|
| 35 |
namespace ranges |
| 36 |
{ |
| 37 |
/// \addtogroup group-views |
| 38 |
/// @{ |
| 39 |
template<typename Rng, typename Regex, typename SubMatchRange> |
| 40 |
struct tokenize_view |
| 41 |
: view_interface<tokenize_view<Rng, Regex, SubMatchRange>, |
| 42 |
is_finite<Rng>::value ? finite : range_cardinality<Rng>::value> |
| 43 |
{ |
| 44 |
private: |
| 45 |
CPP_assert(bidirectional_range<Rng> && view_<Rng> && common_range<Rng>); |
| 46 |
CPP_assert(semiregular<Regex>); |
| 47 |
CPP_assert(semiregular<SubMatchRange>); |
| 48 |
|
| 49 |
Rng rng_; |
| 50 |
Regex rex_; |
| 51 |
SubMatchRange subs_; |
| 52 |
std::regex_constants::match_flag_type flags_; |
| 53 |
template<bool Const> |
| 54 |
using iterator_t = |
| 55 |
std::regex_token_iterator<iterator_t<meta::const_if_c<Const, Rng>>>; |
| 56 |
|
| 57 |
public: |
| 58 |
tokenize_view() = default; |
| 59 |
tokenize_view(Rng rng, Regex rex, SubMatchRange subs, |
| 60 |
std::regex_constants::match_flag_type flags) |
| 61 |
: rng_(std::move(rng)) |
| 62 |
, rex_(std::move(rex)) |
| 63 |
, subs_(std::move(subs)) |
| 64 |
, flags_(flags) |
| 65 |
{} |
| 66 |
iterator_t<simple_view<Rng>()> begin() |
| 67 |
{ |
| 68 |
meta::const_if_c<simple_view<Rng>(), Rng> & rng = rng_; |
| 69 |
return {ranges::begin(rng), ranges::end(rng), rex_, subs_, flags_}; |
| 70 |
} |
| 71 |
template(bool Const = true)( |
| 72 |
requires range<Rng const>) |
| 73 |
iterator_t<Const> begin() const |
| 74 |
{ |
| 75 |
return {ranges::begin(rng_), ranges::end(rng_), rex_, subs_, flags_}; |
| 76 |
} |
| 77 |
iterator_t<simple_view<Rng>()> end() |
| 78 |
{ |
| 79 |
return {}; |
| 80 |
} |
| 81 |
template(bool Const = true)( |
| 82 |
requires range<Rng const>) |
| 83 |
iterator_t<Const> end() const |
| 84 |
{ |
| 85 |
return {}; |
| 86 |
} |
| 87 |
Rng base() const |
| 88 |
{ |
| 89 |
return rng_; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
#if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17 |
| 94 |
template(typename Rng, typename Regex, typename SubMatchRange)( |
| 95 |
requires copy_constructible<Regex> AND copy_constructible<SubMatchRange>) |
| 96 |
tokenize_view(Rng &&, Regex, SubMatchRange) |
| 97 |
->tokenize_view<views::all_t<Rng>, Regex, SubMatchRange>; |
| 98 |
#endif |
| 99 |
|
| 100 |
namespace views |
| 101 |
{ |
| 102 |
struct tokenize_base_fn |
| 103 |
{ |
| 104 |
template(typename Rng, typename Regex)( |
| 105 |
requires bidirectional_range<Rng> AND common_range<Rng> AND |
| 106 |
same_as< // |
| 107 |
range_value_t<Rng>, // |
| 108 |
typename detail::decay_t<Regex>::value_type>) |
| 109 |
tokenize_view<all_t<Rng>, detail::decay_t<Regex>, int> // |
| 110 |
operator()(Rng && rng, |
| 111 |
Regex && rex, |
| 112 |
int sub = 0, |
| 113 |
std::regex_constants::match_flag_type flags = |
| 114 |
std::regex_constants::match_default) const // |
| 115 |
{ |
| 116 |
return {all(static_cast<Rng &&>(rng)), |
| 117 |
static_cast<Regex &&>(rex), |
| 118 |
sub, |
| 119 |
flags}; |
| 120 |
} |
| 121 |
|
| 122 |
template(typename Rng, typename Regex)( |
| 123 |
requires bidirectional_range<Rng> AND common_range<Rng> AND |
| 124 |
same_as<range_value_t<Rng>, |
| 125 |
typename detail::decay_t<Regex>::value_type>) |
| 126 |
tokenize_view<all_t<Rng>, detail::decay_t<Regex>, std::vector<int>> // |
| 127 |
operator()(Rng && rng, |
| 128 |
Regex && rex, |
| 129 |
std::vector<int> subs, |
| 130 |
std::regex_constants::match_flag_type flags = |
| 131 |
std::regex_constants::match_default) const // |
| 132 |
{ |
| 133 |
return {all(static_cast<Rng &&>(rng)), |
| 134 |
static_cast<Regex &&>(rex), |
| 135 |
std::move(subs), |
| 136 |
flags}; |
| 137 |
} |
| 138 |
|
| 139 |
template(typename Rng, typename Regex)( |
| 140 |
requires bidirectional_range<Rng> AND common_range<Rng> AND |
| 141 |
same_as<range_value_t<Rng>, |
| 142 |
typename detail::decay_t<Regex>::value_type>) |
| 143 |
tokenize_view<all_t<Rng>, |
| 144 |
detail::decay_t<Regex>, |
| 145 |
std::initializer_list<int>> // |
| 146 |
operator()(Rng && rng, |
| 147 |
Regex && rex, |
| 148 |
std::initializer_list<int> subs, |
| 149 |
std::regex_constants::match_flag_type flags = |
| 150 |
std::regex_constants::match_default) const // |
| 151 |
{ |
| 152 |
return {all(static_cast<Rng &&>(rng)), |
| 153 |
static_cast<Regex &&>(rex), |
| 154 |
std::move(subs), |
| 155 |
flags}; |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
struct tokenize_fn : tokenize_base_fn |
| 160 |
{ |
| 161 |
using tokenize_base_fn::operator(); |
| 162 |
|
| 163 |
template<typename Regex> |
| 164 |
constexpr auto operator()(Regex && rex, |
| 165 |
int sub = 0, |
| 166 |
std::regex_constants::match_flag_type flags = |
| 167 |
std::regex_constants::match_default) const |
| 168 |
{ |
| 169 |
return make_view_closure(bind_back( |
| 170 |
tokenize_base_fn{}, static_cast<Regex &&>(rex), sub, flags)); |
| 171 |
} |
| 172 |
|
| 173 |
template<typename Regex> |
| 174 |
auto operator()(Regex && rex, |
| 175 |
std::vector<int> subs, |
| 176 |
std::regex_constants::match_flag_type flags = |
| 177 |
std::regex_constants::match_default) const |
| 178 |
{ |
| 179 |
return bind_back(tokenize_base_fn{}, |
| 180 |
static_cast<Regex &&>(rex), |
| 181 |
std::move(subs), |
| 182 |
flags); |
| 183 |
} |
| 184 |
|
| 185 |
template<typename Regex> |
| 186 |
constexpr auto operator()(Regex && rex, |
| 187 |
std::initializer_list<int> subs, |
| 188 |
std::regex_constants::match_flag_type flags = |
| 189 |
std::regex_constants::match_default) const |
| 190 |
{ |
| 191 |
return make_view_closure(bind_back( |
| 192 |
tokenize_base_fn{}, static_cast<Regex &&>(rex), subs, flags)); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
/// \relates tokenize_fn |
| 197 |
/// \ingroup group-views |
| 198 |
RANGES_INLINE_VARIABLE(tokenize_fn, tokenize) |
| 199 |
} // namespace views |
| 200 |
/// @} |
| 201 |
} // namespace ranges |
| 202 |
|
| 203 |
#include <range/v3/detail/epilogue.hpp> |
| 204 |
#include <range/v3/detail/satisfy_boost_range.hpp> |
| 205 |
RANGES_SATISFY_BOOST_RANGE(::ranges::tokenize_view) |
| 206 |
|
| 207 |
#endif |