Skip to content

Commit

Permalink
more replacements of std::regex with ctre
Browse files Browse the repository at this point in the history
  • Loading branch information
DubbleClick committed Mar 8, 2025
1 parent a44e7c8 commit f45b0c4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
6 changes: 3 additions & 3 deletions GWToolboxdll/GWToolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,9 @@ std::filesystem::path GWToolbox::SaveSettings()
ToolboxSettings::LoadModules(ini);
ASSERT(Resources::SaveIniToFile(ini->location_on_disk, ini) == 0);
const auto dir = ini->location_on_disk.parent_path();
const auto dirstr = dir.string();
const auto printable = TextUtils::ctre_regex_replace<R"(\\)">(dirstr, "/");
Log::Log("Toolbox settings saved to %s", printable.c_str());
const auto dirstr = dir.wstring();
const auto printable = TextUtils::ctre_regex_replace<LR"(\\)">(dirstr, L"/");
Log::LogW(L"Toolbox settings saved to %s", printable.c_str());
settings_folder_changed = false;
return ini->location_on_disk;
}
Expand Down
8 changes: 4 additions & 4 deletions GWToolboxdll/Modules/ChatCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ void CHAT_CMD_FUNC(ChatCommands::CmdTB)
const auto file_location = GWToolbox::SaveSettings();
const auto dir = file_location.parent_path();
const auto dirstr = dir.wstring();
const auto printable = std::regex_replace(dirstr, std::wregex(L"\\\\"), L"/");
const auto printable = TextUtils::ctre_regex_replace<LR"(\\)">(dirstr, L"/");
Log::InfoW(L"Settings saved to %s", printable.c_str());
}
else if (arg1 == L"load") {
Expand All @@ -1845,7 +1845,7 @@ void CHAT_CMD_FUNC(ChatCommands::CmdTB)
const auto file_location = GWToolbox::LoadSettings();
const auto dir = file_location.parent_path();
const auto dirstr = dir.wstring();
const auto printable = std::regex_replace(dirstr, std::wregex(L"\\\\"), L"/");
const auto printable = TextUtils::ctre_regex_replace<L"\\\\">(dirstr, L"/");
Log::InfoW(L"Settings loaded from %s", printable.c_str());
}
else if (arg1 == L"reset") {
Expand Down Expand Up @@ -1915,7 +1915,7 @@ void CHAT_CMD_FUNC(ChatCommands::CmdTB)
const auto file_location = GWToolbox::SaveSettings();
const auto dir = file_location.parent_path();
const auto dirstr = dir.wstring();
const auto printable = std::regex_replace(dirstr, std::wregex(L"\\\\"), L"/");
const auto printable = TextUtils::ctre_regex_replace<LR"(\\)">(dirstr, L"/");
Log::InfoW(L"Settings saved to %s", printable.c_str());
}
else if (arg1 == L"load") {
Expand All @@ -1931,7 +1931,7 @@ void CHAT_CMD_FUNC(ChatCommands::CmdTB)
const auto file_location = GWToolbox::LoadSettings();
const auto dir = file_location.parent_path();
const auto dirstr = dir.wstring();
const auto printable = std::regex_replace(dirstr, std::wregex(L"\\\\"), L"/");
const auto printable = TextUtils::ctre_regex_replace<LR"(\\)">(dirstr, L"/");
Log::InfoW(L"Settings loaded from %s", printable.c_str());
}
else {
Expand Down
2 changes: 1 addition & 1 deletion GWToolboxdll/Modules/ChatSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PendingChatMessage {
bool invalid = true; // Set when we can't find the agent name for some reason, or arguments passed are empty.

protected:
std::vector<std::wstring> SanitiseForSend();
std::vector<std::wstring> SanitiseForSend() const;
bool PrintMessage();
bool Send();
void Init();
Expand Down
31 changes: 27 additions & 4 deletions GWToolboxdll/Utils/TextUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,33 @@ namespace TextUtils {
}

template <ctll::fixed_string Pattern>
std::wstring ctre_regex_replace_w(const std::wstring_view input, const std::wstring_view replacement) {
const auto in = WStringToString(input);
const auto res = TextUtils::ctre_regex_replace<Pattern>(in, WStringToString(replacement));
return StringToWString(res);
std::wstring ctre_regex_replace(const std::wstring_view input, const std::wstring_view replacement) {
std::wstring result;
// Pre-allocate memory to avoid frequent reallocations
result.reserve(input.size());

size_t last_pos = 0;

// Iterate through all matches
for (const auto match : ctre::search_all<Pattern>(input)) {
// Get the matched substring and its position
const auto matched_view = match.get<0>().to_view();
const auto pos = std::distance(input.begin(), match.get<0>().begin());

// Append text before the match
result.append(input.substr(last_pos, pos - last_pos));
// Append the replacement
result.append(replacement);
// Update position
last_pos = pos + matched_view.size();
}

// Append the remaining text
if (last_pos < input.size()) {
result.append(input.substr(last_pos));
}

return result;
}

template <ctll::fixed_string Pattern, typename Formatter>
Expand Down
40 changes: 20 additions & 20 deletions GWToolboxdll/Utils/ToolboxUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,30 +745,30 @@ namespace ToolboxUtils {

// Change "Life draining -3, Health regeneration -1" > "Vampiric" (add at end of description)
if (ctre::match<L"\x2\x102\x2.\x10A\xA86\x10A\xA54\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA53\x1\x101.\x1">(original)) {
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\xA86\x10A\xA54\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA53\x1\x101.\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\xA86\x10A\xA54\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA53\x1\x101.\x1">(original, L"");
original += L"\x2\x102\x2\x108\x107Vampiric\x1";
}

// Change "Energy gain on hit 1, Energy regeneration -1" > "Zealous" (add at end of description)
if (ctre::match<L"\x2\x102\x2.\x10A\xA86\x10A\xA50\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA51\x1\x101.\x1">(original)) {
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\xA86\x10A\xA50\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA51\x1\x101.\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\xA86\x10A\xA50\x1\x101.\x1\x2\x102\x2.\x10A\xA7E\x10A\xA51\x1\x101.\x1">(original, L"");
original += L"\x2\x102\x2\x108\x107Zealous\x1";
}

// Change "Damage" > "Dmg"
original = TextUtils::ctre_regex_replace_w<L"\xA4C">(original, L"\xA4E");
original = TextUtils::ctre_regex_replace<L"\xA4C">(original, L"\xA4E");

// Change Bow "Two-Handed" > ""
original = TextUtils::ctre_regex_replace_w<L"\x8102\x1227">(original, L"\xA3E");
original = TextUtils::ctre_regex_replace<L"\x8102\x1227">(original, L"\xA3E");

// Change "Halves casting time of spells" > "HCT"
original = TextUtils::ctre_regex_replace_w<L"\xA80\x10A\xA47\x1">(original, L"\x108\x107HCT\x1");
original = TextUtils::ctre_regex_replace<L"\xA80\x10A\xA47\x1">(original, L"\x108\x107HCT\x1");

// Change "Halves skill recharge of spells" > "HSR"
original = TextUtils::ctre_regex_replace_w<L"\xA80\x10A\xA58\x1">(original, L"\x108\x107HSR\x1");
original = TextUtils::ctre_regex_replace<L"\xA80\x10A\xA58\x1">(original, L"\x108\x107HSR\x1");

// Remove (Stacking) and (Non-stacking) rubbish
original = TextUtils::ctre_regex_replace_w<L"\x2.\x10A\xAA8\x10A[\xAB1\xAB2]\x1\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2.\x10A\xAA8\x10A[\xAB1\xAB2]\x1\x1">(original, L"");

// Replace (while affected by a(n) to just (n)
original = TextUtils::ctre_regex_replace_with_formatter<L"\x8101\x4D9C\x10A.\x1">(
Expand All @@ -779,12 +779,12 @@ namespace ToolboxUtils {
});

// Replace (while xxx) to just (xxx)
original = TextUtils::ctre_regex_replace_w<L"\xAB4">(original, L"\x108\x107" L"Attacking\x1");
original = TextUtils::ctre_regex_replace_w<L"\xAB5">(original, L"\x108\x107" L"Casting\x1");
original = TextUtils::ctre_regex_replace_w<L"\xAB6">(original, L"\x108\x107" L"Condition\x1");
original = TextUtils::ctre_regex_replace_w<L"[\xAB7\x4B6]">(original, L"\x108\x107" L"Enchanted\x1");
original = TextUtils::ctre_regex_replace_w<L"[\xAB8\x4B4]">(original, L"\x108\x107" L"Hexed\x1");
original = TextUtils::ctre_regex_replace_w<L"[\xAB9\xABA]">(original, L"\x108\x107" L"Stance\x1");
original = TextUtils::ctre_regex_replace<L"\xAB4">(original, L"\x108\x107" L"Attacking\x1");
original = TextUtils::ctre_regex_replace<L"\xAB5">(original, L"\x108\x107" L"Casting\x1");
original = TextUtils::ctre_regex_replace<L"\xAB6">(original, L"\x108\x107" L"Condition\x1");
original = TextUtils::ctre_regex_replace<L"[\xAB7\x4B6]">(original, L"\x108\x107" L"Enchanted\x1");
original = TextUtils::ctre_regex_replace<L"[\xAB8\x4B4]">(original, L"\x108\x107" L"Hexed\x1");
original = TextUtils::ctre_regex_replace<L"[\xAB9\xABA]">(original, L"\x108\x107" L"Stance\x1");

// Combine Attribute + 3, Attribute + 1 to Attribute +3 +1 (e.g. headpiece)
original = TextUtils::ctre_regex_replace_with_formatter<L".\x10A\xA84\x10A.\x1\x101.\x1\x2\x102\x2.\x10A\xA84\x10A.\x1\x101.\x1">(
Expand All @@ -800,28 +800,28 @@ namespace ToolboxUtils {
});

// Remove "Value: 122 gold"
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2\xA3E\x10A\xA8A\x10A\xA59\x1\x10B.\x101.(\x102.)?\x1\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2\xA3E\x10A\xA8A\x10A\xA59\x1\x10B.\x101.(\x102.)?\x1\x1">(original, L"");

// Remove other "greyed" generic terms e.g. "Two-Handed", "Unidentified"
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2\xA3E\x10A.\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2\xA3E\x10A.\x1">(original, L"");

// Remove "Necromancer Munne sometimes gives these to me in trade" etc
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\x8102.\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\x8102.\x1">(original, L"");

// Remove "Inscription: None"
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\x8101\x5A1F\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\x8101\x5A1F\x1">(original, L"");

// Remove "Crafted in tribute to an enduring legend." etc
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\x8103.\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\x8103.\x1">(original, L"");

// Remove "20% Additional damage during festival events" > "Dmg +20% (Festival)"
original = TextUtils::ctre_regex_replace_w<L".\x10A\x108\x10A\x8103\xB71\x101\x100\x1\x1">(
original = TextUtils::ctre_regex_replace<L".\x10A\x108\x10A\x8103\xB71\x101\x100\x1\x1">(
original, L"\xA85\x10A\xA4E\x1\x101\x114\x2\xAA8\x10A\x108\x107" L"Festival\x1\x1");

// Check for customized item with +20% damage
if (item->customized && ctre::match<L"\x2\x102\x2.\x10A\xA85\x10A[\xA4C\xA4E]\x1\x101\x114\x1">(original)) {
// Remove "\nDamage +20%" > "\n"
original = TextUtils::ctre_regex_replace_w<L"\x2\x102\x2.\x10A\xA85\x10A[\xA4C\xA4E]\x1\x101\x114\x1">(original, L"");
original = TextUtils::ctre_regex_replace<L"\x2\x102\x2.\x10A\xA85\x10A[\xA4C\xA4E]\x1\x101\x114\x1">(original, L"");
// Append "Customized"
original += L"\x2\x102\x2\x108\x107Customized\x1";
}
Expand Down

0 comments on commit f45b0c4

Please sign in to comment.