diff --git a/library/include/larra/stream_error.hpp b/library/include/larra/stream_error.hpp index ca908aa..648b2a4 100644 --- a/library/include/larra/stream_error.hpp +++ b/library/include/larra/stream_error.hpp @@ -2,43 +2,10 @@ #include #include -#include -#include #include namespace larra::xmpp { -namespace impl { -// std::isupper not declared as constexpr -constexpr auto IsUpper(char ch) -> bool { - return ch >= 'A' && ch <= 'Z'; -} - -constexpr auto ToLower(char ch) -> char { - return (ch >= 'A' && ch <= 'Z') ? static_cast(ch + ('a' - 'A')) : ch; -} - -template -constexpr auto ToKebabCaseName() -> std::string_view { - static constexpr auto rawStr = nameof::nameof_short_type(); - - constexpr auto str = [] { - return rawStr // - | std::views::transform([](auto ch) { - return impl::IsUpper(ch) ? std::array{'-', impl::ToLower(ch)} : std::array{ch, '\0'}; - }) // - | std::views::join // - | std::views::filter([](char ch) { - return ch != '\0'; - }) // - | std::views::drop(1); - }; - static constexpr auto arr = str() | std::ranges::to>>(); - return {arr.data(), arr.size()}; -} - -} // namespace impl - namespace error::stream { struct BaseError : std::exception {}; @@ -48,7 +15,7 @@ struct BaseError : std::exception {}; template struct ErrorImpl : BaseError { static constexpr auto kDefaultName = "stream:error"; - static inline const auto kKebabCaseName = static_cast(impl::ToKebabCaseName()); + static inline const auto kKebabCaseName = static_cast(utils::ToKebabCaseName()); static constexpr auto kErrorMessage = [] -> std::string_view { static constexpr auto name = nameof::nameof_short_type(); diff --git a/library/include/larra/utils.hpp b/library/include/larra/utils.hpp index 7534947..4628754 100644 --- a/library/include/larra/utils.hpp +++ b/library/include/larra/utils.hpp @@ -1,5 +1,7 @@ #pragma once #include +#include +#include #include namespace larra::xmpp::utils { @@ -309,4 +311,31 @@ auto AccumulateFieldLength(const T& obj) -> std::size_t { return totalLength; } +template +constexpr auto ToKebabCaseName() -> std::string_view { + static constexpr auto rawStr = nameof::nameof_short_type(); + + // std::isupper and std::tolower are not declared as constexpr + static constexpr auto isUpper = [](char ch) { + return ch >= 'A' && ch <= 'Z'; + }; + static constexpr auto toLower = [](char ch) { + return (ch >= 'A' && ch <= 'Z') ? static_cast(ch + ('a' - 'A')) : ch; + }; + + constexpr auto str = [] { + return rawStr // + | std::views::transform([](auto ch) { + return isUpper(ch) ? std::array{'-', toLower(ch)} : std::array{ch, '\0'}; + }) // + | std::views::join // + | std::views::filter([](char ch) { + return ch != '\0'; + }) // + | std::views::drop(1); + }; + static constexpr auto arr = str() | std::ranges::to>>(); + return {arr.data(), arr.size()}; +} + } // namespace larra::xmpp::utils