Skip to content

Commit

Permalink
generate single-header
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Jan 24, 2024
1 parent 0eb07ba commit 9725886
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 14 deletions.
73 changes: 66 additions & 7 deletions single-header/ctre-unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,7 @@ struct utf8_range {
#include <string_view>
#include <string>
#include <iterator>
#include <optional>
#ifdef _MSC_VER
#include <memory>
#endif
Expand Down Expand Up @@ -3415,6 +3416,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 @@ -3426,6 +3435,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 @@ -3438,11 +3458,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 @@ -3454,6 +3477,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 @@ -3666,6 +3697,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 @@ -3674,28 +3706,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
73 changes: 66 additions & 7 deletions single-header/ctre.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3293,6 +3293,7 @@ struct utf8_range {
#include <string_view>
#include <string>
#include <iterator>
#include <optional>
#ifdef _MSC_VER
#include <memory>
#endif
Expand Down Expand Up @@ -3412,6 +3413,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 @@ -3423,6 +3432,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 @@ -3435,11 +3455,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 @@ -3451,6 +3474,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 @@ -3663,6 +3694,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 @@ -3671,28 +3703,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 9725886

Please sign in to comment.