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
33 public:
34 DynamicCasedName() = default;
35
48 explicit DynamicCasedName(const std::string &input);
49
60 [[nodiscard]] static DynamicCasedName from_snake_case(const std::string &input);
61
72 [[nodiscard]] static DynamicCasedName from_pascal_case(const std::string &input);
73
84 [[nodiscard]] static DynamicCasedName from_c_identifier(const std::string &input);
85
96 [[nodiscard]] static DynamicCasedName from_flat_case(const std::string &input);
97
103 [[nodiscard]] std::string to_snake_case() const;
104
110 [[nodiscard]] std::string to_pascal_case() const;
111
117 [[nodiscard]] std::string to_c_identifier() const;
118
124 [[nodiscard]] std::string to_flat_case() const;
125
135 [[nodiscard]] const std::string &canonical() const
136 {
137 return canonical_;
138 }
139
145 [[nodiscard]] bool empty() const
146 {
147 return segments_.empty();
148 }
149
155 [[nodiscard]] const std::vector<std::vector<std::string>> &segments() const
156 {
157 return segments_;
158 }
159
166 [[nodiscard]] bool operator==(const DynamicCasedName &other) const
167 {
168 return canonical_ == other.canonical_;
169 }
170
177 [[nodiscard]] auto operator<=>(const DynamicCasedName &other) const
178 {
179 return canonical_ <=> other.canonical_;
180 }
181
182 private:
183 std::vector<std::vector<std::string>> segments_;
184 std::string canonical_;
185
186 explicit DynamicCasedName(std::vector<std::vector<std::string>> segments);
187 void compute_canonical();
188};
189
196[[nodiscard]] std::string to_string(const DynamicCasedName &value);
197
205inline std::ostream &operator<<(std::ostream &os, const DynamicCasedName &value)
206{
207 os << to_string(value);
208 return os;
209}
210
211} // namespace porytiles
212
213template <>
214struct std::hash<porytiles::DynamicCasedName> {
215 std::size_t operator()(const porytiles::DynamicCasedName &name) const noexcept
216 {
217 return std::hash<std::string>{}(name.canonical());
218 }
219};
220
228template <>
229struct std::formatter<porytiles::DynamicCasedName> {
230 constexpr auto parse(std::format_parse_context &ctx)
231 {
232 return ctx.begin();
233 }
234
235 auto format(const porytiles::DynamicCasedName &value, auto &ctx) const
236 {
237 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
238 }
239};
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::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