46 const std::regex re{pattern};
47 return std::regex_match(str, re);
49 catch (
const std::regex_error &e) {
50 panic(std::string{
"regex error: "} + std::string{e.what()});
71[[nodiscard]]
inline std::string
trim_prefix(
const std::string &str,
const std::string &prefix)
73 if (str.starts_with(prefix)) {
74 return str.substr(prefix.size());
88inline void trim(std::string &
string)
92 string.begin(), std::ranges::find_if(
string, [](
const unsigned char ch) {
return !std::isspace(ch); }));
96 std::ranges::find_if(
string.rbegin(),
string.rend(), [](
const unsigned char ch) {
return !std::isspace(ch); })
112[[nodiscard]]
inline std::vector<std::string>
split(std::string input,
const std::string &delimiter)
114 std::vector<std::string> result;
116 while ((pos = input.find(delimiter)) != std::string::npos) {
117 std::string token = input.substr(0, pos);
118 result.push_back(token);
119 input.erase(0, pos + delimiter.length());
121 result.push_back(input);
137 while (!line.empty() && (line.back() ==
'\r' || line.back() ==
'\n')) {
155 std::string result = line;
156 while (!result.empty() && (result.back() ==
'\r' || result.back() ==
'\n')) {
176 return std::format(
"0x{:x}", t);
193 return std::format(
"{:02}", t);
207[[nodiscard]]
inline std::string
to_string(
const std::string &str)
225 return value ?
"true" :
"false";
240[[nodiscard]] std::string
to_string(
const std::vector<T> &vec)
242 std::string result =
"{";
243 for (std::size_t i = 0; i < vec.size(); ++i) {
276 result.reserve(s.size());
278 bool capitalize_next =
true;
279 for (
const char c : s) {
280 if (c ==
'_' || c ==
'-' || c ==
' ') {
281 capitalize_next =
true;
284 if (capitalize_next) {
285 result +=
static_cast<char>(std::toupper(
static_cast<unsigned char>(c)));
286 capitalize_next =
false;
322 result.reserve(s.size() + s.size() / 4);
324 for (std::size_t i = 0; i < s.size(); ++i) {
328 if (c ==
'_' || c ==
'-' || c ==
' ') {
330 if (!result.empty() && result.back() !=
'_') {
337 if (std::isupper(
static_cast<unsigned char>(c))) {
342 if (!result.empty() && result.back() !=
'_') {
343 const bool prev_is_lower = i > 0 && std::islower(
static_cast<unsigned char>(s[i - 1]));
344 const bool next_is_lower = i + 1 < s.size() && std::islower(
static_cast<unsigned char>(s[i + 1]));
345 if (prev_is_lower || next_is_lower) {
349 result +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
352 result +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
357 if (!result.empty() && result.back() ==
'_') {
383 std::ranges::transform(input, std::back_inserter(output), [](
const unsigned char c) {
385 return std::tolower(c);
423 constexpr std::string_view prefix =
"gTileset_";
424 if (tileset_name.starts_with(prefix)) {
425 return tileset_name.substr(prefix.size());
A smart string wrapper that preserves word structure for lossless case format conversion.
std::string to_pascal_case(const std::string &s)
Converts a string to PascalCase format.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
std::string to_snake_case(const std::string &s)
Converts a string to snake_case format.
std::string & trim_line_ending(std::string &line)
Removes line ending characters from a string in-place.
bool check_full_string_match(const std::string &str, const std::string &pattern)
Checks if a string fully matches a regular expression pattern.
std::string trim_prefix(const std::string &str, const std::string &prefix)
Removes a prefix from a string if present.
@ split
Split the animation into separate animations for each palette variant (not yet implemented).
std::string int_to_hex_str(T t)
Converts an integer value to a hexadecimal string with "0x" prefix.
std::string pad_two_digits(T t)
Converts an integer value to a minimum two-digit wide string representation.
std::string to_lower_str(const std::string &input)
Converts all characters in a string to lowercase.
std::string pal_filename(std::size_t pal_index)
Constructs a palette filename from a palette index.
std::string extract_tileset_shorthand(const std::string &tileset_name)
Extracts the Pascal-case tileset short name from the full name.
DynamicCasedName extract_tileset_cased_name(const std::string &tileset_name)
Extracts the tileset short name and wraps it in a DynamicCasedName.
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
void trim(std::string &string)
Removes leading and trailing whitespace from a string in-place.