From 2316f594154d0132dbe10d67cca88f2b860bd23e Mon Sep 17 00:00:00 2001 From: Blu3train Date: Fri, 29 Oct 2021 09:25:54 +0200 Subject: [PATCH] Added global modifier option --- extensions/regexreplacer.cpp | 32 +++++++++++++++++++++++++++----- text.cpp | 17 +++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/extensions/regexreplacer.cpp b/extensions/regexreplacer.cpp index c8019bb..fe65da6 100644 --- a/extensions/regexreplacer.cpp +++ b/extensions/regexreplacer.cpp @@ -17,7 +17,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved case DLL_PROCESS_ATTACH: { if (!std::ifstream(REGEX_REPLACEMENTS_SAVE_FILE).good()) - { + { auto file = std::ofstream(REGEX_REPLACEMENTS_SAVE_FILE, std::ios::binary) << "\xff\xfe"; 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)); @@ -36,16 +36,38 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { 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); BlockMarkupIterator savedFilters(stream, Array{ L"|REGEX|", L"|BECOMES|", L"|MODIFIER|" }); 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(); - std::wregex regexp(regex, (modifier == L"i") ? std::regex::ECMAScript | std::regex::icase : std::regex::ECMAScript); - try { ::regex = regexp; } + if (modifier == L"g") + { + 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; } - sentence = std::regex_replace(sentence, ::regex.value(), replacement); + + sentence = std::regex_replace(sentence, ::regex.value(), replacement, flag); } return true; } diff --git a/text.cpp b/text.cpp index 521fb77..121ec3f 100644 --- a/text.cpp +++ b/text.cpp @@ -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).)"; 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: -|REGEX|regular_expression|BECOMES|replacement_text|MODIFIER|i|END| -If the "MODIFIER" parameter is set to "i" the replacement ignores the case. -Any other value is not considered and the replacement is case sensitive. +|REGEX|regular_expression|BECOMES|replacement_text|MODIFIER|modifiers|END| +The "MODIFIER" parameter can contain the following modifiers: +"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. This file must be encoded in Unicode (UTF-16 Little Endian). 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).)"; REGEX_REPLACER_INSTRUCTIONS = LR"(Questo file fa qualcosa solo quando l'estenzione "Regex Replacer" è utilizzata. I comandi di sostituzione devono essere formattati cosi: -|REGEX|espressione_regolare|BECOMES|testo_sostituito|MODIFIER|i|END| -Se il parametro "MODIFIER" è impostato a "i" la sostituzione ignora maiuscole/minuscole -Qualsiasi altro valore non è considerato e la sostituzione è case sensitive. +|REGEX|espressione_regolare|BECOMES|testo_sostituito|MODIFIER|modificatori|END| +Il parametro "MODIFIER" può contenere i seguenti modificatori: +"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. Questo file deve essere codificato in Unicode (UTF-16 Little Endian). Apprendere, creare e testare Espressioni Regolari: https://regexr.com/)";