Textractor_test/extensions/replacer.cpp

153 lines
4.3 KiB
C++
Raw Normal View History

2019-01-24 05:11:14 +08:00
#include "extension.h"
#include <cwctype>
#include <fstream>
2019-01-24 21:32:21 +08:00
#include <filesystem>
#include <process.h>
2019-01-24 05:11:14 +08:00
2019-02-28 00:33:17 +08:00
extern const wchar_t* REPLACER_INSTRUCTIONS;
2019-07-03 20:32:32 +08:00
2019-02-19 12:12:12 +08:00
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
2019-07-03 20:32:32 +08:00
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
2019-02-28 14:40:40 +08:00
std::shared_mutex m;
2019-01-24 05:11:14 +08:00
2019-07-03 20:32:32 +08:00
class Trie
2019-01-24 05:11:14 +08:00
{
public:
Trie(std::unordered_map<std::wstring, std::wstring> replacements)
2019-01-24 05:11:14 +08:00
{
2019-07-03 20:32:32 +08:00
for (const auto& [original, replacement] : replacements)
{
Node* current = &root;
for (auto ch : original) if (!Ignore(ch)) current = Next(current, ch);
if (current != &root)
current->value = owningStorage.insert(owningStorage.end(), replacement.c_str(), replacement.c_str() + replacement.size() + 1) - owningStorage.begin();
2019-07-03 20:32:32 +08:00
}
2019-01-24 05:11:14 +08:00
}
2019-07-03 20:32:32 +08:00
std::wstring Replace(const std::wstring& sentence) const
2019-01-24 05:11:14 +08:00
{
2019-07-03 20:32:32 +08:00
std::wstring result;
for (int i = 0; i < sentence.size();)
{
std::wstring_view replacement(sentence.c_str() + i, 1);
2019-07-03 20:32:32 +08:00
int originalLength = 1;
const Node* current = &root;
for (int j = i; current && j <= sentence.size(); ++j)
{
if (current->value >= 0)
2019-07-03 20:32:32 +08:00
{
replacement = owningStorage.data() + current->value;
2019-07-03 20:32:32 +08:00
originalLength = j - i;
}
if (!Ignore(sentence[j])) current = Next(current, sentence[j]);
}
2019-07-03 20:32:32 +08:00
result += replacement;
i += originalLength;
}
return result;
2019-01-24 05:11:14 +08:00
}
bool Empty()
{
return root.charMap.empty();
}
2019-01-24 05:11:14 +08:00
private:
2019-02-18 08:14:49 +08:00
static bool Ignore(wchar_t ch)
2019-01-24 05:11:14 +08:00
{
2019-02-18 08:14:49 +08:00
return ch <= 0x20 || std::iswspace(ch);
2019-01-24 05:11:14 +08:00
}
template <typename Node>
static Node* Next(Node* node, wchar_t ch)
{
auto it = std::lower_bound(node->charMap.begin(), node->charMap.end(), ch, [](const auto& one, auto two) { return one.first < two; });
if (it != node->charMap.end() && it->first == ch) return it->second.get();
if constexpr (!std::is_const_v<Node>) return node->charMap.insert(it, { ch, std::make_unique<Node>() })->second.get();
return nullptr;
}
2019-01-24 05:11:14 +08:00
struct Node
{
std::vector<std::pair<wchar_t, std::unique_ptr<Node>>> charMap;
ptrdiff_t value = -1;
2019-01-24 05:11:14 +08:00
} root;
std::vector<wchar_t> owningStorage;
2019-07-03 20:32:32 +08:00
} trie = { {} };
2019-01-24 05:11:14 +08:00
std::unordered_map<std::wstring, std::wstring> Parse(std::wstring_view replacementScript)
2019-01-24 05:11:14 +08:00
{
2019-07-03 20:32:32 +08:00
std::unordered_map<std::wstring, std::wstring> replacements;
for (size_t end = 0; ;)
2019-01-24 05:11:14 +08:00
{
2019-07-03 20:32:32 +08:00
size_t original = replacementScript.find(L"|ORIG|", end);
size_t becomes = replacementScript.find(L"|BECOMES|", original);
if ((end = replacementScript.find(L"|END|", becomes)) == std::wstring::npos) break;
replacements[std::wstring(replacementScript.substr(original + 6, becomes - original - 6))] = replacementScript.substr(becomes + 9, end - becomes - 9);
2019-02-21 11:14:32 +08:00
}
2019-07-03 20:32:32 +08:00
return replacements;
2019-01-24 05:11:14 +08:00
}
void UpdateReplacements()
{
try
{
if (replaceFileLastWrite.exchange(std::filesystem::last_write_time(REPLACE_SAVE_FILE)) == std::filesystem::last_write_time(REPLACE_SAVE_FILE)) return;
std::vector<BYTE> file(std::istreambuf_iterator(std::ifstream(REPLACE_SAVE_FILE, std::ios::binary)), {});
std::scoped_lock l(m);
trie = Trie(Parse({ (wchar_t*)file.data(), file.size() / sizeof(wchar_t) }));
}
catch (std::filesystem::filesystem_error) { replaceFileLastWrite.store({}); }
}
2019-01-24 05:11:14 +08:00
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
UpdateReplacements();
if (trie.Empty())
2019-01-24 21:32:21 +08:00
{
2019-09-10 10:08:24 +08:00
auto file = std::ofstream(REPLACE_SAVE_FILE, std::ios::binary) << "\xff\xfe";
for (auto ch : std::wstring_view(REPLACER_INSTRUCTIONS)) file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2));
2019-01-24 21:32:21 +08:00
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
}
2019-01-24 05:11:14 +08:00
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
return TRUE;
}
bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{
UpdateReplacements();
2019-01-24 21:32:21 +08:00
2019-07-03 20:32:32 +08:00
std::shared_lock l(m);
sentence = trie.Replace(sentence);
return true;
2019-01-24 05:11:14 +08:00
}
2019-07-03 20:32:32 +08:00
TEST(
2019-01-24 05:11:14 +08:00
{
2019-07-03 20:32:32 +08:00
auto replacements = Parse(LR"(
|ORIG||BECOMES|goodbye |END|Ignore this text
And this text   
2019-01-24 05:11:14 +08:00
|ORIG||BECOMES|idiot|END|
2019-07-03 20:32:32 +08:00
|ORIG| |BECOMES| hello|END||ORIG|delete this|BECOMES||END|)");
assert(replacements.size() == 4);
std::wstring original = LR"(Don't replace this 
delete this)";
std::wstring replaced = Trie(std::move(replacements)).Replace(original);
2019-07-03 20:32:32 +08:00
assert(replaced == L"Don't replace thisgoodbye idiot hello");
2019-01-24 05:11:14 +08:00
}
);