From 2661366324019814c661acfff9e75696473f38b7 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 10:33:38 +0100 Subject: [PATCH] Accept std::string_view in rfl::string_to_enum (#565) --- include/rfl/enums.hpp | 56 ++++++++++++++----- .../rfl/internal/enums/cpp20/from_string.hpp | 5 +- .../rfl/internal/enums/cpp26/from_string.hpp | 4 +- include/rfl/internal/enums/from_string.hpp | 4 +- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/include/rfl/enums.hpp b/include/rfl/enums.hpp index 7f107916a..133791b2f 100644 --- a/include/rfl/enums.hpp +++ b/include/rfl/enums.hpp @@ -1,8 +1,12 @@ #ifndef RFL_ENUMS_HPP_ #define RFL_ENUMS_HPP_ -#include +#include #include +#include +#include +#include +#include #include "Result.hpp" #include "internal/enums/from_string.hpp" @@ -89,8 +93,8 @@ std::string enum_to_string(const EnumType _enum) { // EnumNamesOnly processor with a parser) to accept only the declared // enumerator names instead. template -Result string_to_enum(const std::string& _str) { - const auto make_error_msg = [&](const auto& name) { +Result string_to_enum(const std::string_view _str) { + const auto make_error_msg = [](const std::string_view name) { std::string msg = "Invalid enum value: '"; msg += name; msg += "'. Must be one of ["; @@ -105,7 +109,7 @@ Result string_to_enum(const std::string& _str) { }; const auto cast_numbers_or_names = - [&](const std::string& name) -> Result { + [&](const std::string_view name) -> Result { const auto r = internal::enums::from_string(name); if (r) { return *r; @@ -113,26 +117,50 @@ Result string_to_enum(const std::string& _str) { if constexpr (enum_names_only) { return make_error_msg(name); } else { - try { - const auto val = std::stoi(name); - return static_cast(val); - } catch (std::exception& exp) { + if (name.empty()) { return make_error_msg(name); } + using Underlying = std::underlying_type_t; + using ParseType = std::conditional_t< + (sizeof(Underlying) < sizeof(int)), int, + std::conditional_t< + std::is_signed_v, + std::conditional_t< + (sizeof(Underlying) <= sizeof(int)), int, + std::conditional_t<(sizeof(Underlying) <= sizeof(long)), long, + long long>>, + std::conditional_t< + (sizeof(Underlying) <= sizeof(unsigned int)), unsigned int, + std::conditional_t< + (sizeof(Underlying) <= sizeof(unsigned long)), + unsigned long, unsigned long long>>>>; + ParseType val{}; + const auto* begin = name.data(); + const auto* end = begin + name.size(); + const auto [ptr, ec] = std::from_chars(begin, end, val); + if (ec == std::errc{} && ptr == end) { + return static_cast(val); + } + return make_error_msg(name); } }; if constexpr (internal::enums::is_flag_enum) { using T = std::underlying_type_t; - const auto split = internal::strings::split(_str, "|"); auto res = static_cast(0); - for (const auto& s : split) { - const auto r = cast_numbers_or_names(s); - if (r) { - res |= static_cast(*r); - } else { + std::string_view remaining = _str; + while (true) { + const auto pos = remaining.find('|'); + const auto token = remaining.substr(0, pos); + const auto r = cast_numbers_or_names(token); + if (!r) { return r; } + res |= static_cast(*r); + if (pos == std::string_view::npos) { + break; + } + remaining.remove_prefix(pos + 1); } return static_cast(res); } else { diff --git a/include/rfl/internal/enums/cpp20/from_string.hpp b/include/rfl/internal/enums/cpp20/from_string.hpp index b14315964..1d1cec7de 100644 --- a/include/rfl/internal/enums/cpp20/from_string.hpp +++ b/include/rfl/internal/enums/cpp20/from_string.hpp @@ -3,15 +3,14 @@ #ifndef REFLECTCPP_USE_CPP26_REFLECTION -#include -#include +#include #include "enchantum.hpp" // NOLINT(unused-includes) namespace rfl::internal::enums::cpp20 { template -auto from_string(const std::string& _str) { +auto from_string(const std::string_view _str) { return enchantum::cast(_str); } diff --git a/include/rfl/internal/enums/cpp26/from_string.hpp b/include/rfl/internal/enums/cpp26/from_string.hpp index 919be47c6..1f4698529 100644 --- a/include/rfl/internal/enums/cpp26/from_string.hpp +++ b/include/rfl/internal/enums/cpp26/from_string.hpp @@ -5,13 +5,13 @@ #include #include -#include +#include #include namespace rfl::internal::enums::cpp26 { template -std::optional from_string(const std::string& _str) { +std::optional from_string(const std::string_view _str) { template for (constexpr auto e : std::define_static_array( std::meta::enumerators_of(^^EnumType))) { if (_str == std::meta::identifier_of(e)) { diff --git a/include/rfl/internal/enums/from_string.hpp b/include/rfl/internal/enums/from_string.hpp index 160363283..c966f6d36 100644 --- a/include/rfl/internal/enums/from_string.hpp +++ b/include/rfl/internal/enums/from_string.hpp @@ -1,6 +1,8 @@ #ifndef RFL_INTERNAL_ENUMS_FROM_STRING_HPP_ #define RFL_INTERNAL_ENUMS_FROM_STRING_HPP_ +#include + #ifndef REFLECTCPP_USE_CPP26_REFLECTION #include "cpp20/from_string.hpp" #else @@ -10,7 +12,7 @@ namespace rfl::internal::enums { template -auto from_string(const std::string& _str) { +auto from_string(const std::string_view _str) { #ifndef REFLECTCPP_USE_CPP26_REFLECTION return cpp20::from_string(_str); #else