Porytiles
Loading...
Searching...
No Matches
porytiles::DynamicCasedName Class Reference

A smart string wrapper that preserves word structure for lossless case format conversion. More...

#include <dynamic_cased_name.hpp>

Public Member Functions

 DynamicCasedName ()=default
 
 DynamicCasedName (const std::string &input)
 Constructs a DynamicCasedName by auto-detecting the input format.
 
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).
 
std::string to_c_identifier () const
 Outputs PascalCase within each segment, with segments joined by underscores.
 
std::string to_flat_case () const
 Outputs all words joined with no separators, all lowercase.
 
const std::string & canonical () const
 Returns the canonical form (all words lowercase, no separators).
 
bool empty () const
 Checks if this name is empty (has no segments/words).
 
const std::vector< std::vector< std::string > > & segments () const
 Returns the internal two-level segment/word structure.
 
bool operator== (const DynamicCasedName &other) const
 Equality comparison based on canonical form.
 
auto operator<=> (const DynamicCasedName &other) const
 Three-way comparison based on canonical form.
 

Static Public Member Functions

static DynamicCasedName from_snake_case (const std::string &input)
 Constructs from a snake_case input string.
 
static DynamicCasedName from_pascal_case (const std::string &input)
 Constructs from a PascalCase input string.
 
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).
 

Detailed Description

A smart string wrapper that preserves word structure for lossless case format conversion.

Porytiles deals with tileset names, animation names, and identifiers that come in various case formats (PascalCase, snake_case, smoosh_case, and C identifiers with mixed casing around underscores). Converting between formats using simple string manipulation is lossy. Particularly, round-trips through snake_case lose information about the original underscore positions vs PascalCase word boundaries.

DynamicCasedName solves this by parsing the input into a two-level structure:

  • Outer vector ("segments"): groups separated by underscores in C identifier format
  • Inner vector ("words"): individual words within a segment, split by PascalCase boundaries
  • All words stored fully lowercase

This structure enables lossless conversion to any output format from any input format. Equality and ordering are based on the canonical (all-lowercase, no-separator) form, so names that represent the same identifier in different formats compare as equal.

Invariant
All words in segments_ are non-empty and fully lowercase.
canonical_ equals all words concatenated with no separators.

Definition at line 32 of file dynamic_cased_name.hpp.

Constructor & Destructor Documentation

◆ DynamicCasedName() [1/2]

porytiles::DynamicCasedName::DynamicCasedName ( )
default

◆ DynamicCasedName() [2/2]

porytiles::DynamicCasedName::DynamicCasedName ( const std::string &  input)
explicit

Constructs a DynamicCasedName by auto-detecting the input format.

Auto-detection uses the following heuristic:

  • Has underscores AND uppercase letters -> C identifier format
  • Has underscores but no uppercase -> snake_case
  • No underscores but has uppercase -> PascalCase
  • No underscores and no uppercase -> smoosh_case
Parameters
inputThe name string to parse.

Definition at line 125 of file dynamic_cased_name.cpp.

Member Function Documentation

◆ canonical()

const std::string & porytiles::DynamicCasedName::canonical ( ) const
inline

Returns the canonical form (all words lowercase, no separators).

The canonical form is used for equality, ordering, and hashing. Two DynamicCasedName objects that represent the same logical name in different formats will have identical canonical forms.

Returns
A const reference to the canonical string.

Definition at line 135 of file dynamic_cased_name.hpp.

◆ empty()

bool porytiles::DynamicCasedName::empty ( ) const
inline

Checks if this name is empty (has no segments/words).

Returns
True if the name contains no segments.

Definition at line 145 of file dynamic_cased_name.hpp.

◆ from_c_identifier()

DynamicCasedName porytiles::DynamicCasedName::from_c_identifier ( const std::string &  input)
static

Constructs from a C identifier format string (PascalCase segments joined by underscores).

First splits on underscores into tokens, then splits each token by PascalCase boundaries. Each underscore- delimited token becomes one segment containing its PascalCase-split words.

Parameters
inputThe C identifier string to parse (e.g., "Water_Current_LandWatersEdge").
Returns
A DynamicCasedName with the parsed structure.

Definition at line 194 of file dynamic_cased_name.cpp.

◆ from_flat_case()

DynamicCasedName porytiles::DynamicCasedName::from_flat_case ( const std::string &  input)
static

Constructs from a flatcase input string (all lowercase, no separators).

The entire lowercased input becomes a single atomic word in a single segment. No word splitting is attempted since flatcase provides no boundary information.

Parameters
inputThe flatcase string to parse.
Returns
A DynamicCasedName with the parsed structure.

Definition at line 214 of file dynamic_cased_name.cpp.

◆ from_pascal_case()

DynamicCasedName porytiles::DynamicCasedName::from_pascal_case ( const std::string &  input)
static

Constructs from a PascalCase input string.

Splits on PascalCase word boundaries. All words go into a single segment. Acronyms like "XML" or "TV" are handled using the same boundary logic as to_snake_case() in string_utils.hpp.

Parameters
inputThe PascalCase string to parse.
Returns
A DynamicCasedName with the parsed structure.

Definition at line 180 of file dynamic_cased_name.cpp.

◆ from_snake_case()

DynamicCasedName porytiles::DynamicCasedName::from_snake_case ( const std::string &  input)
static

Constructs from a snake_case input string.

Splits on underscores. Each token becomes a single-word segment. Empty tokens from leading, trailing, or consecutive underscores are filtered out.

Parameters
inputThe snake_case string to parse.
Returns
A DynamicCasedName with the parsed structure.

Definition at line 157 of file dynamic_cased_name.cpp.

◆ operator<=>()

auto porytiles::DynamicCasedName::operator<=> ( const DynamicCasedName other) const
inline

Three-way comparison based on canonical form.

Parameters
otherThe other DynamicCasedName to compare with.
Returns
The ordering relationship between the canonical forms.

Definition at line 177 of file dynamic_cased_name.hpp.

◆ operator==()

bool porytiles::DynamicCasedName::operator== ( const DynamicCasedName other) const
inline

Equality comparison based on canonical form.

Parameters
otherThe other DynamicCasedName to compare with.
Returns
True if both names have the same canonical form.

Definition at line 166 of file dynamic_cased_name.hpp.

◆ segments()

const std::vector< std::vector< std::string > > & porytiles::DynamicCasedName::segments ( ) const
inline

Returns the internal two-level segment/word structure.

Returns
A const reference to the segments vector.

Definition at line 155 of file dynamic_cased_name.hpp.

◆ to_c_identifier()

std::string porytiles::DynamicCasedName::to_c_identifier ( ) const

Outputs PascalCase within each segment, with segments joined by underscores.

Returns
The name in C identifier format.

Definition at line 260 of file dynamic_cased_name.cpp.

◆ to_flat_case()

std::string porytiles::DynamicCasedName::to_flat_case ( ) const

Outputs all words joined with no separators, all lowercase.

Returns
The name in flatcase format.

Definition at line 278 of file dynamic_cased_name.cpp.

◆ to_pascal_case()

std::string porytiles::DynamicCasedName::to_pascal_case ( ) const

Outputs all words flattened and joined in PascalCase (each word capitalized, no separators).

Returns
The name in PascalCase format.

Definition at line 247 of file dynamic_cased_name.cpp.

◆ to_snake_case()

std::string porytiles::DynamicCasedName::to_snake_case ( ) const

Outputs all words flattened and joined with underscores.

Returns
The name in snake_case format.

Definition at line 229 of file dynamic_cased_name.cpp.


The documentation for this class was generated from the following files: