Moved ToKebapCasName to utils
This commit is contained in:
parent
f36b544b11
commit
54e9556478
2 changed files with 30 additions and 34 deletions
|
@ -2,43 +2,10 @@
|
|||
#include <libxml++/libxml++.h>
|
||||
|
||||
#include <larra/utils.hpp>
|
||||
#include <nameof.hpp>
|
||||
#include <ranges>
|
||||
#include <variant>
|
||||
|
||||
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<char>(ch + ('a' - 'A')) : ch;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto ToKebabCaseName() -> std::string_view {
|
||||
static constexpr auto rawStr = nameof::nameof_short_type<T>();
|
||||
|
||||
constexpr auto str = [] {
|
||||
return rawStr //
|
||||
| std::views::transform([](auto ch) {
|
||||
return impl::IsUpper(ch) ? std::array<char, 2>{'-', impl::ToLower(ch)} : std::array<char, 2>{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<utils::RangeToWrapper<std::array<char, std::ranges::distance(str())>>>();
|
||||
return {arr.data(), arr.size()};
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
namespace error::stream {
|
||||
|
||||
struct BaseError : std::exception {};
|
||||
|
@ -48,7 +15,7 @@ struct BaseError : std::exception {};
|
|||
template <typename T>
|
||||
struct ErrorImpl : BaseError {
|
||||
static constexpr auto kDefaultName = "stream:error";
|
||||
static inline const auto kKebabCaseName = static_cast<std::string>(impl::ToKebabCaseName<T>());
|
||||
static inline const auto kKebabCaseName = static_cast<std::string>(utils::ToKebabCaseName<T>());
|
||||
|
||||
static constexpr auto kErrorMessage = [] -> std::string_view {
|
||||
static constexpr auto name = nameof::nameof_short_type<T>();
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
#include <boost/pfr.hpp>
|
||||
#include <nameof.hpp>
|
||||
#include <ranges>
|
||||
#include <utempl/utils.hpp>
|
||||
|
||||
namespace larra::xmpp::utils {
|
||||
|
@ -309,4 +311,31 @@ auto AccumulateFieldLength(const T& obj) -> std::size_t {
|
|||
return totalLength;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto ToKebabCaseName() -> std::string_view {
|
||||
static constexpr auto rawStr = nameof::nameof_short_type<T>();
|
||||
|
||||
// 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<char>(ch + ('a' - 'A')) : ch;
|
||||
};
|
||||
|
||||
constexpr auto str = [] {
|
||||
return rawStr //
|
||||
| std::views::transform([](auto ch) {
|
||||
return isUpper(ch) ? std::array<char, 2>{'-', toLower(ch)} : std::array<char, 2>{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<utils::RangeToWrapper<std::array<char, std::ranges::distance(str())>>>();
|
||||
return {arr.data(), arr.size()};
|
||||
}
|
||||
|
||||
} // namespace larra::xmpp::utils
|
||||
|
|
Loading…
Reference in a new issue