Skip to content

Commit

Permalink
add optional interface to result type
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Jan 24, 2024
1 parent 6d9fa80 commit 132cea5
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions include/ctre/return_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string_view>
#include <string>
#include <iterator>
#include <optional>
#ifdef _MSC_VER
#include <memory>
#endif
Expand Down Expand Up @@ -127,6 +128,14 @@ template <size_t Id, typename Name = void> struct captured_content {
std::from_chars(view.data(), view.data() + view.size(), result, std::forward<Ts>(args)...);
return result;
}

template <typename R = int, typename... Ts> constexpr CTRE_FORCE_INLINE auto to_optional_number(Ts && ... args) const noexcept -> std::optional<R> {
if (!static_cast<bool>(*this)) {
return std::nullopt;
}

return to_number<R>(std::forward<Ts>(args)...);
}
#endif

template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
Expand All @@ -138,6 +147,17 @@ template <size_t Id, typename Name = void> struct captured_content {
return std::basic_string_view<char_type>(data_unsafe(), static_cast<size_t>(unit_size()));
}

constexpr CTRE_FORCE_INLINE auto view() const noexcept {
return to_view();
}

template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_optional_view() const noexcept -> std::optional<std::basic_string_view<char_type>> {
if (!static_cast<bool>(*this)) {
return std::nullopt;
}
return to_view();
}

constexpr CTRE_FORCE_INLINE std::basic_string<char_type> to_string() const noexcept {
#if __cpp_char8_t >= 201811
if constexpr (std::is_same_v<Iterator, utf8_iterator>) {
Expand All @@ -150,11 +170,14 @@ template <size_t Id, typename Name = void> struct captured_content {
#endif
}

constexpr CTRE_FORCE_INLINE auto view() const noexcept {
return to_view();
constexpr CTRE_FORCE_INLINE auto str() const noexcept {
return to_string();
}

constexpr CTRE_FORCE_INLINE auto str() const noexcept {
template <typename It = Iterator> constexpr CTRE_FORCE_INLINE auto to_optional_string() const noexcept -> std::optional<std::basic_string<char_type>> {
if (!static_cast<bool>(*this)) {
return std::nullopt;
}
return to_string();
}

Expand All @@ -166,6 +189,14 @@ template <size_t Id, typename Name = void> struct captured_content {
return to_string();
}

constexpr CTRE_FORCE_INLINE operator std::optional<std::basic_string_view<char_type>>() const noexcept {
return to_optional_view();
}

constexpr CTRE_FORCE_INLINE explicit operator std::optional<std::basic_string<char_type>>() const noexcept {
return to_optional_string();
}

constexpr CTRE_FORCE_INLINE static size_t get_id() noexcept {
return Id;
}
Expand Down Expand Up @@ -378,6 +409,7 @@ template <typename Iterator, typename... Captures> class regex_results {
return bool(_captures.template select<0>());
}

// implicit conversions
constexpr CTRE_FORCE_INLINE operator std::basic_string_view<char_type>() const noexcept {
return to_view();
}
Expand All @@ -386,28 +418,55 @@ template <typename Iterator, typename... Captures> class regex_results {
return to_string();
}

constexpr CTRE_FORCE_INLINE operator std::optional<std::basic_string_view<char_type>>() const noexcept {
return to_optional_view();
}

constexpr CTRE_FORCE_INLINE explicit operator std::optional<std::basic_string<char_type>>() const noexcept {
return to_optional_string();
}

// conversion to numbers
#if __has_include(<charconv>)
template <typename R = int, typename... Ts> constexpr CTRE_FORCE_INLINE auto to_number(Ts && ... args) const noexcept -> R {
return _captures.template select<0>().template to_number<R>(std::forward<Ts>(args)...);
}

template <typename R = int, typename... Ts> constexpr CTRE_FORCE_INLINE auto to_optional_number(Ts && ... args) const noexcept -> std::optional<R> {
return _captures.template select<0>().template to_optional_number<R>(std::forward<Ts>(args)...);
}
#endif

// conversion to basic_string_view
constexpr CTRE_FORCE_INLINE auto to_view() const noexcept {
return _captures.template select<0>().to_view();
}

constexpr CTRE_FORCE_INLINE auto view() const noexcept {
return _captures.template select<0>().view(); // this should be deprecated (??)
}

constexpr CTRE_FORCE_INLINE auto to_optional_view() const noexcept {
return _captures.template select<0>().to_optional_view();
}

// conversion to basic_string
constexpr CTRE_FORCE_INLINE auto to_string() const noexcept {
return _captures.template select<0>().to_string();
}

constexpr CTRE_FORCE_INLINE auto view() const noexcept {
return _captures.template select<0>().view();
constexpr CTRE_FORCE_INLINE auto str() const noexcept {
return _captures.template select<0>().to_string(); // this should be deprecated (??)
}

constexpr CTRE_FORCE_INLINE auto str() const noexcept {
return _captures.template select<0>().to_string();
constexpr CTRE_FORCE_INLINE auto to_optional_string() const noexcept {
return _captures.template select<0>().to_optional_string();
}





constexpr CTRE_FORCE_INLINE size_t size() const noexcept {
return _captures.template select<0>().size();
}
Expand Down

0 comments on commit 132cea5

Please sign in to comment.