// Copyright (C) 2022 T. Zachary Laine // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PARSER_SUBRANGE_HPP #define BOOST_PARSER_SUBRANGE_HPP #include #include #include namespace boost::parser { /** A simple view type used throughout the rest of the library in C++17 builds; similar to `std::ranges::subrange`. */ #if BOOST_PARSER_USE_CONCEPTS template S = I> #else template #endif struct subrange : detail::stl_interfaces::view_interface> { constexpr subrange() = default; constexpr subrange(I first, S last) : first_(first), last_(last) {} template constexpr explicit subrange(R const & r) : first_(detail::text::detail::begin(r)), last_(detail::text::detail::end(r)) {} constexpr I begin() const { return first_; } constexpr S end() const { return last_; } [[nodiscard]] constexpr subrange next(std::ptrdiff_t n = 1) const { return subrange{detail::text::detail::next(first_), last_}; } [[nodiscard]] constexpr subrange prev(std::ptrdiff_t n = 1) const { return subrange{detail::text::detail::prev(first_), last_}; } constexpr subrange & advance(std::ptrdiff_t n) { std::advance(first_, n); return *this; } template< typename I2, typename S2, typename Enable = std::enable_if_t< std::is_convertible::value && std::is_convertible::value>> constexpr operator subrange() const { return {first_, last_}; } private: I first_; [[no_unique_address]] S last_; }; #if defined(__cpp_deduction_guides) #if BOOST_PARSER_USE_CONCEPTS template S> #else template #endif subrange(I, S) -> subrange; #if BOOST_PARSER_USE_CONCEPTS template #else template #endif subrange(R &&) -> subrange< detail::text::detail::iterator_t, detail::text::detail::sentinel_t>; #endif /** Makes a `subrange` from an `I` and an `S`. */ #if BOOST_PARSER_USE_CONCEPTS template S = I> #else template #endif constexpr subrange make_subrange(I first, S last) noexcept { return subrange(first, last); } } #if BOOST_PARSER_USE_CONCEPTS namespace std::ranges { template S> inline constexpr bool enable_borrowed_range> = true; } #endif #endif