From f62f90a0681262f347084f8baa69b034ee318e18 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Mon, 25 Feb 2019 01:06:16 -0500 Subject: [PATCH] store trie match and use if another isnt found --- extensions/replacer.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/extensions/replacer.cpp b/extensions/replacer.cpp index 09844bf..d92aa67 100644 --- a/extensions/replacer.cpp +++ b/extensions/replacer.cpp @@ -25,13 +25,24 @@ public: std::pair> Lookup(const std::wstring& text) { + std::pair> result = {}; int length = 0; Node* current = &root; for (auto ch : text) - if (Ignore(ch)) ++length; - else if (auto& next = current->next[ch]) ++length, current = next.get(); + { + if (Ignore(ch)) + { + length += 1; + } + else if (auto& next = current->next[ch]) + { + length += 1; + current = next.get(); + if (current->value) result = { length, current->value }; + } else break; - return { length, current->value }; + } + return result; } private: @@ -124,12 +135,15 @@ TEST( assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END| |ORIG|バカ|BECOMES|idiot|END| |ORIG|こんにちは |BECOMES|hello|END|)") == 3); - std::wstring replaced = LR"(hello  + std::wstring replaced = LR"(blahblah  さよなら バカ こんにちは)"; Replace(replaced); assert(replaced.find(L"さよなら") == std::wstring::npos && replaced.find(L"バカ") == std::wstring::npos && - replaced.find(L"こんにちは") == std::wstring::npos + 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 ); } );