Skip to content

Commit

Permalink
add concept for ctre::capture_group
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Dec 3, 2023
1 parent 108244d commit 6d9fa80
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
24 changes: 21 additions & 3 deletions include/ctre/return_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#if __has_include(<charconv>)
#include <charconv>
#endif
#if __cpp_concepts >= 202002L
#include <concepts>
#endif

namespace ctre {

Expand Down Expand Up @@ -126,8 +129,6 @@ template <size_t Id, typename Name = void> struct captured_content {
}
#endif

template <typename T> struct identify;

template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
// random access, because C++ (waving hands around)
constexpr bool must_be_nonreverse_contiguous_iterator = is_random_accessible<typename std::iterator_traits<std::remove_const_t<It>>::iterator_category> && !is_reverse_iterator<It>;
Expand Down Expand Up @@ -187,6 +188,23 @@ template <size_t Id, typename Name = void> struct captured_content {
};
};

#if __cpp_concepts >= 202002L
template <typename T> concept capture_group = requires(const T & cap) {
{ T::get_id() } -> std::same_as<size_t>;
{ cap.view() };
{ cap.str() };
{ cap.to_string() };
{ cap.to_view() };
{ cap.unit_size() } -> std::same_as<size_t>;
{ cap.size() } -> std::same_as<size_t>;
{ static_cast<bool>(cap) };
{ cap.data() };
{ cap.data_unsafe() };
{ cap.begin() };
{ cap.end() };
};
#endif

struct capture_not_exists_tag { };

static constexpr inline auto capture_not_exists = capture_not_exists_tag{};
Expand Down Expand Up @@ -453,7 +471,7 @@ template <typename Iterator, typename... Captures> struct is_regex_results_t<reg
template <typename T> constexpr bool is_regex_results_v = is_regex_results_t<T>();

#if __cpp_concepts >= 202002L
template <typename T> concept regex_captures = is_regex_results_v<T>;
template <typename T> concept capture_groups = is_regex_results_v<T>;
#endif

template <typename ResultIterator, typename Pattern> using return_type = decltype(regex_results(std::declval<ResultIterator>(), find_captures(Pattern{})));
Expand Down
24 changes: 21 additions & 3 deletions single-header/ctre-unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3303,6 +3303,9 @@ struct utf8_range {
#if __has_include(<charconv>)
#include <charconv>
#endif
#if __cpp_concepts >= 202002L
#include <concepts>
#endif

namespace ctre {

Expand Down Expand Up @@ -3414,8 +3417,6 @@ template <size_t Id, typename Name = void> struct captured_content {
}
#endif

template <typename T> struct identify;

template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
// random access, because C++ (waving hands around)
constexpr bool must_be_nonreverse_contiguous_iterator = is_random_accessible<typename std::iterator_traits<std::remove_const_t<It>>::iterator_category> && !is_reverse_iterator<It>;
Expand Down Expand Up @@ -3475,6 +3476,23 @@ template <size_t Id, typename Name = void> struct captured_content {
};
};

#if __cpp_concepts >= 202002L
template <typename T> concept capture_group = requires(const T & cap) {
{ T::get_id() } -> std::same_as<size_t>;
{ cap.view() };
{ cap.str() };
{ cap.to_string() };
{ cap.to_view() };
{ cap.unit_size() } -> std::same_as<size_t>;
{ cap.size() } -> std::same_as<size_t>;
{ static_cast<bool>(cap) };
{ cap.data() };
{ cap.data_unsafe() };
{ cap.begin() };
{ cap.end() };
};
#endif

struct capture_not_exists_tag { };

static constexpr inline auto capture_not_exists = capture_not_exists_tag{};
Expand Down Expand Up @@ -3741,7 +3759,7 @@ template <typename Iterator, typename... Captures> struct is_regex_results_t<reg
template <typename T> constexpr bool is_regex_results_v = is_regex_results_t<T>();

#if __cpp_concepts >= 202002L
template <typename T> concept regex_captures = is_regex_results_v<T>;
template <typename T> concept capture_groups = is_regex_results_v<T>;
#endif

template <typename ResultIterator, typename Pattern> using return_type = decltype(regex_results(std::declval<ResultIterator>(), find_captures(Pattern{})));
Expand Down
24 changes: 21 additions & 3 deletions single-header/ctre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,9 @@ struct utf8_range {
#if __has_include(<charconv>)
#include <charconv>
#endif
#if __cpp_concepts >= 202002L
#include <concepts>
#endif

namespace ctre {

Expand Down Expand Up @@ -3411,8 +3414,6 @@ template <size_t Id, typename Name = void> struct captured_content {
}
#endif

template <typename T> struct identify;

template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
// random access, because C++ (waving hands around)
constexpr bool must_be_nonreverse_contiguous_iterator = is_random_accessible<typename std::iterator_traits<std::remove_const_t<It>>::iterator_category> && !is_reverse_iterator<It>;
Expand Down Expand Up @@ -3472,6 +3473,23 @@ template <size_t Id, typename Name = void> struct captured_content {
};
};

#if __cpp_concepts >= 202002L
template <typename T> concept capture_group = requires(const T & cap) {
{ T::get_id() } -> std::same_as<size_t>;
{ cap.view() };
{ cap.str() };
{ cap.to_string() };
{ cap.to_view() };
{ cap.unit_size() } -> std::same_as<size_t>;
{ cap.size() } -> std::same_as<size_t>;
{ static_cast<bool>(cap) };
{ cap.data() };
{ cap.data_unsafe() };
{ cap.begin() };
{ cap.end() };
};
#endif

struct capture_not_exists_tag { };

static constexpr inline auto capture_not_exists = capture_not_exists_tag{};
Expand Down Expand Up @@ -3738,7 +3756,7 @@ template <typename Iterator, typename... Captures> struct is_regex_results_t<reg
template <typename T> constexpr bool is_regex_results_v = is_regex_results_t<T>();

#if __cpp_concepts >= 202002L
template <typename T> concept regex_captures = is_regex_results_v<T>;
template <typename T> concept capture_groups = is_regex_results_v<T>;
#endif

template <typename ResultIterator, typename Pattern> using return_type = decltype(regex_results(std::declval<ResultIterator>(), find_captures(Pattern{})));
Expand Down
4 changes: 2 additions & 2 deletions tests/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ int main() {
#endif

#if CTRE_CNTTP_COMPILER_CHECK
for (auto match: ctre::range<"(?<first>[0-9])[0-9]++">(input)) {
for (auto match: ctre::search_all<"(?<first>[0-9])[0-9]++">(input)) {
#else
using namespace ctre::literals;

for (auto match: ctre::range<pattern>(input)) {
for (auto match: ctre::search_all<pattern>(input)) {
#endif

#if CTRE_CNTTP_COMPILER_CHECK
Expand Down
2 changes: 1 addition & 1 deletion tests/results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main() {
using namespace std::string_view_literals;
auto input = "123,456,768"sv;

for (auto match: ctre::range<pattern>(input)) {
for (auto match: ctre::search_all<pattern>(input)) {

if (match == "456") std::cout << "bingo: ";
if (match != "768") std::cout << "bad: ";
Expand Down

0 comments on commit 6d9fa80

Please sign in to comment.