43 const std::regex re{pattern};
44 return std::regex_match(str, re);
46 catch (
const std::regex_error &e) {
47 panic(std::string{
"regex error: "} + std::string{e.what()});
66[[nodiscard]]
inline std::string
trim_prefix(
const std::string &str,
const std::string &prefix)
68 if (str.starts_with(prefix)) {
69 return str.substr(prefix.size());
81inline void trim(std::string &
string)
85 string.begin(), std::ranges::find_if(
string, [](
const unsigned char ch) {
return !std::isspace(ch); }));
89 std::ranges::find_if(
string.rbegin(),
string.rend(), [](
const unsigned char ch) {
return !std::isspace(ch); })
103[[nodiscard]]
inline std::vector<std::string>
split(std::string input,
const std::string &delimiter)
105 std::vector<std::string> result;
107 while ((pos = input.find(delimiter)) != std::string::npos) {
108 std::string token = input.substr(0, pos);
109 result.push_back(token);
110 input.erase(0, pos + delimiter.length());
112 result.push_back(input);
131[[nodiscard]]
inline std::string
132join_quoted(
const std::vector<std::string> &values,
const std::string &delimiter =
", ")
135 for (
const auto &value : values) {
136 if (!joined.empty()) {
139 joined +=
"'" + value +
"'";
154 while (!line.empty() && (line.back() ==
'\r' || line.back() ==
'\n')) {
170 std::string result = line;
171 while (!result.empty() && (result.back() ==
'\r' || result.back() ==
'\n')) {
189 return std::format(
"0x{:x}", t);
204 return std::format(
"{:02}", t);
216[[nodiscard]]
inline std::string
to_string(
const std::string &str)
232 return value ?
"true" :
"false";
245[[nodiscard]] std::string
to_string(
const std::vector<T> &vec)
247 std::string result =
"{";
248 for (std::size_t i = 0; i < vec.size(); ++i) {
279 result.reserve(s.size());
281 bool capitalize_next =
true;
282 for (
const char c : s) {
283 if (c ==
'_' || c ==
'-' || c ==
' ') {
284 capitalize_next =
true;
287 if (capitalize_next) {
288 result +=
static_cast<char>(std::toupper(
static_cast<unsigned char>(c)));
289 capitalize_next =
false;
323 result.reserve(s.size() + s.size() / 4);
325 for (std::size_t i = 0; i < s.size(); ++i) {
329 if (c ==
'_' || c ==
'-' || c ==
' ') {
331 if (!result.empty() && result.back() !=
'_') {
338 if (std::isupper(
static_cast<unsigned char>(c))) {
343 if (!result.empty() && result.back() !=
'_') {
344 const bool prev_is_lower = i > 0 && std::islower(
static_cast<unsigned char>(s[i - 1]));
345 const bool next_is_lower = i + 1 < s.size() && std::islower(
static_cast<unsigned char>(s[i + 1]));
346 if (prev_is_lower || next_is_lower) {
350 result +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
353 result +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
358 if (!result.empty() && result.back() ==
'_') {
383 std::ranges::transform(input, std::back_inserter(output), [](
const unsigned char c) {
385 return std::tolower(c);
419 constexpr std::string_view prefix =
"gTileset_";
420 if (tileset_name.starts_with(prefix)) {
421 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.
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 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 palette_filename(std::size_t palette_index)
Constructs a palette filename from a palette index.
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
@ split
Split the animation into separate animations for each palette variant (not yet implemented).
void trim(std::string &string)
Removes leading and trailing whitespace from a string in-place.
std::string join_quoted(const std::vector< std::string > &values, const std::string &delimiter=", ")
Joins strings into a delimited list, wrapping each element in single quotes.