draconisplusplus/include/rfl/internal/transform_snake_case.hpp

36 lines
1.2 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_
#define RFL_INTERNAL_TRANSFORMSNAKECASE_HPP_
#include "StringLiteral.hpp"
namespace rfl::internal {
2024-06-08 14:10:59 -04:00
/// Capitalizes a lower-case character.
template <char c>
consteval char to_upper() {
if constexpr (c >= 'a' && c <= 'z') {
return c + ('A' - 'a');
} else {
return c;
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
/// Transforms the field name from snake case to camel case.
2024-06-16 00:13:15 -04:00
template <internal::StringLiteral _name, bool _capitalize, size_t _i = 0, char... chars>
2024-06-08 14:10:59 -04:00
consteval auto transform_snake_case() {
if constexpr (_i == _name.arr_.size()) {
return StringLiteral<sizeof...(chars) + 1>(chars...);
} else if constexpr (_name.arr_[_i] == '_') {
return transform_snake_case<_name, true, _i + 1, chars...>();
} else if constexpr (_name.arr_[_i] == '\0') {
return transform_snake_case<_name, false, _name.arr_.size(), chars...>();
} else if constexpr (_capitalize) {
2024-06-16 00:13:15 -04:00
return transform_snake_case<_name, false, _i + 1, chars..., to_upper<_name.arr_[_i]>()>();
2024-06-08 14:10:59 -04:00
} else {
2024-06-16 00:13:15 -04:00
return transform_snake_case<_name, false, _i + 1, chars..., _name.arr_[_i]>();
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
} // namespace rfl::internal
2024-05-31 22:59:00 -04:00
#endif