refactor replacer

This commit is contained in:
Akash Mozumdar 2019-07-03 18:02:32 +05:30
parent c074bbf506
commit 5d83c9736b

View File

@ -5,15 +5,18 @@
#include <process.h> #include <process.h>
extern const wchar_t* REPLACER_INSTRUCTIONS; extern const wchar_t* REPLACER_INSTRUCTIONS;
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt"; constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite; std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
std::shared_mutex m; std::shared_mutex m;
struct class Trie
{ {
public: public:
void Put(std::wstring original, std::wstring replacement) Trie(const std::unordered_map<std::wstring, std::wstring>& replacements)
{
for (const auto& [original, replacement] : replacements)
{ {
Node* current = &root; Node* current = &root;
for (auto ch : original) for (auto ch : original)
@ -22,27 +25,32 @@ public:
else current = (next = std::make_unique<Node>()).get(); else current = (next = std::make_unique<Node>()).get();
if (current != &root) current->value = replacement; if (current != &root) current->value = replacement;
} }
}
std::pair<int, std::optional<std::wstring>> Lookup(const std::wstring& text) const std::wstring Replace(const std::wstring& sentence) const
{ {
std::pair<int, std::optional<std::wstring>> result = {}; std::wstring result;
int length = 0; for (int i = 0; i < sentence.size();)
{
std::wstring replacement(1, sentence[i]);
int originalLength = 1;
const Node* current = &root; const Node* current = &root;
for (auto ch : text) for (int j = i; j < sentence.size() + 1; ++j)
{ {
if (Ignore(ch)) if (current->value)
{ {
length += 1; replacement = current->value.value();
} originalLength = j - i;
else if (current->next.count(ch) != 0)
{
auto& next = current->next.at(ch);
length += 1;
current = next.get();
if (current->value) result = { length, current->value };
} }
if (current->next.count(sentence[j]) > 0) current = current->next.at(sentence[j]).get();
else if (Ignore(sentence[j]));
else break; else break;
} }
result += replacement;
i += originalLength;
}
return result; return result;
} }
@ -57,55 +65,34 @@ private:
std::unordered_map<wchar_t, std::unique_ptr<Node>> next; std::unordered_map<wchar_t, std::unique_ptr<Node>> next;
std::optional<std::wstring> value; std::optional<std::wstring> value;
} root; } root;
} replacementTrie; } trie = { {} };
int Parse(const std::wstring& file) std::unordered_map<std::wstring, std::wstring> Parse(const std::wstring& replacementScript)
{ {
std::lock_guard l(m); std::unordered_map<std::wstring, std::wstring> replacements;
replacementTrie = {};
int count = 0;
size_t end = 0; size_t end = 0;
while (true) while (true)
{ {
size_t original = file.find(L"|ORIG|", end); size_t original = replacementScript.find(L"|ORIG|", end);
size_t becomes = file.find(L"|BECOMES|", original); size_t becomes = replacementScript.find(L"|BECOMES|", original);
if ((end = file.find(L"|END|", becomes)) == std::wstring::npos) break; if ((end = replacementScript.find(L"|END|", becomes)) == std::wstring::npos) break;
replacementTrie.Put(file.substr(original + 6, becomes - original - 6), file.substr(becomes + 9, end - becomes - 9)); replacements[replacementScript.substr(original + 6, becomes - original - 6)] = replacementScript.substr(becomes + 9, end - becomes - 9);
count += 1;
} }
return count; return replacements;
}
bool Replace(std::wstring& sentence)
{
std::shared_lock l(m);
for (int i = 0; i < sentence.size(); ++i)
{
auto [length, replacement] = replacementTrie.Lookup(sentence.substr(i));
if (replacement)
{
sentence.replace(i, length, replacement.value());
i += replacement.value().size() - 1; // iterate to end of replacement
}
}
return true;
} }
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{ {
// not actually used to read/write, just to ensure it exists
static AutoHandle<> replacementFile = CreateFileA(REPLACE_SAVE_FILE, FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
switch (ul_reason_for_call) switch (ul_reason_for_call)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
{ {
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {}); std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
if (Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))) == 0) if (Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))).empty())
{ {
std::ofstream(REPLACE_SAVE_FILE, std::ios::out | std::ios::binary | std::ios::trunc).write((char*)REPLACER_INSTRUCTIONS, wcslen(REPLACER_INSTRUCTIONS) * sizeof(wchar_t)); std::ofstream(REPLACE_SAVE_FILE, std::ios::out | std::ios::binary | std::ios::trunc).write((char*)REPLACER_INSTRUCTIONS, wcslen(REPLACER_INSTRUCTIONS) * sizeof(wchar_t));
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user _spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
} }
replaceFileLastWrite = std::filesystem::last_write_time(REPLACE_SAVE_FILE);
} }
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
@ -118,30 +105,34 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{ {
static_assert(std::has_unique_object_representations_v<decltype(replaceFileLastWrite)::value_type>); try
if (!replaceFileLastWrite.compare_exchange_strong(std::filesystem::last_write_time(REPLACE_SAVE_FILE), std::filesystem::last_write_time(REPLACE_SAVE_FILE)))
{ {
static_assert(std::has_unique_object_representations_v<decltype(replaceFileLastWrite)::value_type>);
if (replaceFileLastWrite.exchange(std::filesystem::last_write_time(REPLACE_SAVE_FILE)) != std::filesystem::last_write_time(REPLACE_SAVE_FILE))
{
std::scoped_lock l(m);
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {}); std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))); trie = Trie(Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))));
} }
}
catch (std::filesystem::filesystem_error) {}
return Replace(sentence); std::shared_lock l(m);
sentence = trie.Replace(sentence);
return true;
} }
TEST_SYNC( TEST(
{ {
assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END| auto replacements = Parse(LR"(
|ORIG||BECOMES|goodbye |END|Ignore this text
And this text   
|ORIG||BECOMES|idiot|END| |ORIG||BECOMES|idiot|END|
|ORIG| |BECOMES|hello|END|)") == 3); |ORIG| |BECOMES| hello|END||ORIG|delete this|BECOMES||END|)");
std::wstring replaced = LR"(blahblah  assert(replacements.size() == 4);
)"; std::wstring original = LR"(Don't replace this 
Replace(replaced); delete this)";
assert(replaced.find(L"さよなら") == std::wstring::npos && std::wstring replaced = Trie(replacements).Replace(original);
replaced.find(L"バカ") == std::wstring::npos && assert(replaced == L"Don't replace thisgoodbye idiot hello");
replaced.find(L"こんにちは") == std::wstring::npos &&
replaced.find(L"goodbye") != std::wstring::npos &&
replaced.find(L"idiot") != std::wstring::npos &&
replaced.find(L"hello") != std::wstring::npos
);
} }
); );