From c105f6700fc0a58ce16f94737cc70cd69e843239 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 22 Nov 2018 16:02:45 -0500 Subject: [PATCH] extract repetition filter --- GUI/host/textthread.cpp | 11 +---------- GUI/host/textthread.h | 4 +--- GUI/host/util.cpp | 9 +++++++++ GUI/host/util.h | 2 ++ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index f7f9d75..5a5e554 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -38,15 +38,6 @@ void TextThread::Push(const BYTE* data, int len) lastPushTime = GetTickCount(); } -bool TextThread::FilterRepetition(std::wstring& sentence) -{ - wchar_t* end = sentence.data() + sentence.size(); - for (int len = sentence.size() / 3; len > 6; --len) - if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0) - return true | FilterRepetition(sentence = end - len); - return false; -} - void TextThread::Flush() { std::wstring sentence; @@ -57,7 +48,7 @@ void TextThread::Flush() sentence = buffer; buffer.clear(); - if (FilterRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end()); + if (Util::RemoveRepetition(sentence)) repeatingChars = std::unordered_set(sentence.begin(), sentence.end()); else repeatingChars.clear(); } AddSentence(sentence); diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index f6af2f4..d793e89 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -30,15 +30,13 @@ public: const HookParam hp; private: - // see https://github.com/Artikash/Textractor/issues/40 - static bool FilterRepetition(std::wstring& sentence); void Flush(); std::wstring buffer; std::unordered_set repeatingChars; std::mutex bufferMutex; std::wstring storage; - std::recursive_mutex storageMutex; + std::mutex storageMutex; HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL); std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); }); diff --git a/GUI/host/util.cpp b/GUI/host/util.cpp index 23ecda6..6816f8a 100644 --- a/GUI/host/util.cpp +++ b/GUI/host/util.cpp @@ -34,4 +34,13 @@ namespace Util return L""; } } + + bool RemoveRepetition(std::wstring& text) + { + wchar_t* end = text.data() + text.size(); + for (int len = text.size() / 3; len > 6; --len) + if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0) + return true | RemoveRepetition(text = end - len); + return false; + } } \ No newline at end of file diff --git a/GUI/host/util.h b/GUI/host/util.h index 43bb841..1a3ebad 100644 --- a/GUI/host/util.h +++ b/GUI/host/util.h @@ -6,4 +6,6 @@ namespace Util { std::optional GetClipboardText(); std::wstring StringToWideString(std::string text, UINT encoding = CP_UTF8); + // return true if repetition found (see https://github.com/Artikash/Textractor/issues/40) + bool RemoveRepetition(std::wstring& text); }