Added global modifier option

This commit is contained in:
Blu3train 2021-10-29 09:25:54 +02:00 committed by Akash Mozumdar
parent 06b93e3def
commit 2316f59415
2 changed files with 38 additions and 11 deletions

View File

@ -17,7 +17,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
{ {
if (!std::ifstream(REGEX_REPLACEMENTS_SAVE_FILE).good()) if (!std::ifstream(REGEX_REPLACEMENTS_SAVE_FILE).good())
{ {
auto file = std::ofstream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary) << "\xff\xfe"; auto file = std::ofstream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary) << "\xff\xfe";
for (auto ch : std::wstring_view(REGEX_REPLACER_INSTRUCTIONS)) for (auto ch : std::wstring_view(REGEX_REPLACER_INSTRUCTIONS))
file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2)); file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2));
@ -36,16 +36,38 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
if (!sentenceInfo["current select"] || sentenceInfo["text number"] == 0) return false; if (!sentenceInfo["current select"] || sentenceInfo["text number"] == 0) return false;
std::regex::flag_type mod;
std::regex_constants::match_flag_type flag;
std::ifstream stream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary); std::ifstream stream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary);
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" }); BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" });
concurrency::reader_writer_lock::scoped_lock_read readLock(m); concurrency::reader_writer_lock::scoped_lock_read readLock(m);
while (auto read = savedFilters.Next()) { while (auto read = savedFilters.Next()) {
const auto& [regex, replacement, modifier] = read.value(); const auto& [regex, replacement, modifier] = read.value();
std::wregex regexp(regex, (modifier == L"i") ? std::regex::ECMAScript | std::regex::icase : std::regex::ECMAScript); if (modifier == L"g")
try { ::regex = regexp; } {
mod = std::regex::ECMAScript;
flag = std::regex_constants::format_default;
}
else if (modifier == L"gi" || modifier == L"ig")
{
mod = std::regex::icase;
flag = std::regex_constants::format_default;
}
else if (modifier == L"i")
{
mod = std::regex::icase;
flag = std::regex_constants::format_first_only;
}
else
{
mod = std::regex::ECMAScript;
flag = std::regex_constants::format_first_only;
}
try { ::regex = std::wregex(regex, mod); }
catch (std::regex_error) { continue; } catch (std::regex_error) { continue; }
sentence = std::regex_replace(sentence, ::regex.value(), replacement);
sentence = std::regex_replace(sentence, ::regex.value(), replacement, flag);
} }
return true; return true;
} }

View File

@ -226,9 +226,11 @@ Whitespace in original_text is ignored, but replacement_text can contain spaces,
This file must be encoded in Unicode (UTF-16 Little Endian).)"; This file must be encoded in Unicode (UTF-16 Little Endian).)";
const wchar_t* REGEX_REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Regex Replacer" extension is used. const wchar_t* REGEX_REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Regex Replacer" extension is used.
Replacement commands must be formatted like this: Replacement commands must be formatted like this:
|REGEX|regular_expression|BECOMES|replacement_text|MODIFIER|i|END| |REGEX|regular_expression|BECOMES|replacement_text|MODIFIER|modifiers|END|
If the "MODIFIER" parameter is set to "i" the replacement ignores the case. The "MODIFIER" parameter can contain the following modifiers:
Any other value is not considered and the replacement is case sensitive. "g" the replacement is global.
"i" the replacement ignores the case.
If the modifier is empty the replacement is only the first match and case sensitive.
All text in this file outside of a replacement command is ignored. All text in this file outside of a replacement command is ignored.
This file must be encoded in Unicode (UTF-16 Little Endian). This file must be encoded in Unicode (UTF-16 Little Endian).
Learn, build, & test Regular Expressions: https://regexr.com/)"; Learn, build, & test Regular Expressions: https://regexr.com/)";
@ -941,9 +943,12 @@ La spaziatura nel testo_originale è ignorato, ma testo_sostituito può contener
Questo file deve essere codificato in Unicode (UTF-16 Little Endian).)"; Questo file deve essere codificato in Unicode (UTF-16 Little Endian).)";
REGEX_REPLACER_INSTRUCTIONS = LR"(Questo file fa qualcosa solo quando l'estenzione "Regex Replacer" è utilizzata. REGEX_REPLACER_INSTRUCTIONS = LR"(Questo file fa qualcosa solo quando l'estenzione "Regex Replacer" è utilizzata.
I comandi di sostituzione devono essere formattati cosi: I comandi di sostituzione devono essere formattati cosi:
|REGEX|espressione_regolare|BECOMES|testo_sostituito|MODIFIER|i|END| |REGEX|espressione_regolare|BECOMES|testo_sostituito|MODIFIER|modificatori|END|
Se il parametro "MODIFIER" è impostato a "i" la sostituzione ignora maiuscole/minuscole Il parametro "MODIFIER" può contenere i seguenti modificatori:
Qualsiasi altro valore non è considerato e la sostituzione è case sensitive. "g" la sostituzione è globale.
"i" la sostituzione ignora maiuscole/minuscole.
Se il modificatore è vuoto, la sostituzione viene applicata alla sola prima corrispondenza
e fa distinzione tra maiuscole e minuscole.
Tutto il testo in questo file all'infuori di un comando di sostituzione è ignorato. Tutto il testo in questo file all'infuori di un comando di sostituzione è ignorato.
Questo file deve essere codificato in Unicode (UTF-16 Little Endian). Questo file deve essere codificato in Unicode (UTF-16 Little Endian).
Apprendere, creare e testare Espressioni Regolari: https://regexr.com/)"; Apprendere, creare e testare Espressioni Regolari: https://regexr.com/)";