Porytiles
Loading...
Searching...
No Matches
dynamic_cased_name.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <compare>
4#include <format>
5#include <ostream>
6#include <string>
7#include <vector>
8
9namespace porytiles {
10
31 public:
32 DynamicCasedName() = default;
33
44 explicit DynamicCasedName(const std::string &input);
45
54 [[nodiscard]] static DynamicCasedName from_snake_case(const std::string &input);
55
64 [[nodiscard]] static DynamicCasedName from_pascal_case(const std::string &input);
65
74 [[nodiscard]] static DynamicCasedName from_c_identifier(const std::string &input);
75
84 [[nodiscard]] static DynamicCasedName from_flat_case(const std::string &input);
85
89 [[nodiscard]] std::string to_snake_case() const;
90
94 [[nodiscard]] std::string to_pascal_case() const;
95
99 [[nodiscard]] std::string to_c_identifier() const;
100
104 [[nodiscard]] std::string to_flat_case() const;
105
113 [[nodiscard]] const std::string &canonical() const
114 {
115 return canonical_;
116 }
117
121 [[nodiscard]] bool empty() const
122 {
123 return segments_.empty();
124 }
125
129 [[nodiscard]] const std::vector<std::vector<std::string>> &segments() const
130 {
131 return segments_;
132 }
133
138 [[nodiscard]] bool operator==(const DynamicCasedName &other) const
139 {
140 return canonical_ == other.canonical_;
141 }
142
147 [[nodiscard]] auto operator<=>(const DynamicCasedName &other) const
148 {
149 return canonical_ <=> other.canonical_;
150 }
151
152 private:
153 std::vector<std::vector<std::string>> segments_;
154 std::string canonical_;
155
156 explicit DynamicCasedName(std::vector<std::vector<std::string>> segments);
157 void compute_canonical();
158};
159
164[[nodiscard]] std::string to_string(const DynamicCasedName &value);
165
171inline std::ostream &operator<<(std::ostream &os, const DynamicCasedName &value)
172{
173 os << to_string(value);
174 return os;
175}
176
177} // namespace porytiles
178
179template <>
180struct std::hash<porytiles::DynamicCasedName> {
181 std::size_t operator()(const porytiles::DynamicCasedName &name) const noexcept
182 {
183 return std::hash<std::string>{}(name.canonical());
184 }
185};
186
192template <>
193struct std::formatter<porytiles::DynamicCasedName> {
194 constexpr auto parse(std::format_parse_context &ctx)
195 {
196 return ctx.begin();
197 }
198
199 auto format(const porytiles::DynamicCasedName &value, auto &ctx) const
200 {
201 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
202 }
203};
A smart string wrapper that preserves word structure for lossless case format conversion.
static DynamicCasedName from_c_identifier(const std::string &input)
Constructs from a C identifier format string (PascalCase segments joined by underscores).
static DynamicCasedName from_flat_case(const std::string &input)
Constructs from a flatcase input string (all lowercase, no separators).
bool operator==(const DynamicCasedName &other) const
Equality comparison based on canonical form.
std::string to_snake_case() const
Outputs all words flattened and joined with underscores.
std::string to_pascal_case() const
Outputs all words flattened and joined in PascalCase (each word capitalized, no separators).
const std::string & canonical() const
Returns the canonical form (all words lowercase, no separators).
std::string to_c_identifier() const
Outputs PascalCase within each segment, with segments joined by underscores.
static DynamicCasedName from_pascal_case(const std::string &input)
Constructs from a PascalCase input string.
bool empty() const
Checks if this name is empty (has no segments/words).
std::string to_flat_case() const
Outputs all words joined with no separators, all lowercase.
const std::vector< std::vector< std::string > > & segments() const
Returns the internal two-level segment/word structure.
auto operator<=>(const DynamicCasedName &other) const
Three-way comparison based on canonical form.
static DynamicCasedName from_snake_case(const std::string &input)
Constructs from a snake_case input string.
std::string name
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
constexpr auto parse(std::format_parse_context &ctx)
auto format(const porytiles::DynamicCasedName &value, auto &ctx) const
std::size_t operator()(const porytiles::DynamicCasedName &name) const noexcept