store trie match and use if another isnt found

This commit is contained in:
Akash Mozumdar 2019-02-25 01:06:16 -05:00
parent ec0b9c077c
commit f62f90a068

View File

@ -25,13 +25,24 @@ public:
std::pair<int, std::optional<std::wstring>> Lookup(const std::wstring& text) std::pair<int, std::optional<std::wstring>> Lookup(const std::wstring& text)
{ {
std::pair<int, std::optional<std::wstring>> result = {};
int length = 0; int length = 0;
Node* current = &root; Node* current = &root;
for (auto ch : text) 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; else break;
return { length, current->value }; }
return result;
} }
private: private:
@ -124,12 +135,15 @@ TEST(
assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END| assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END|
|ORIG||BECOMES|idiot|END| |ORIG||BECOMES|idiot|END|
|ORIG| |BECOMES|hello|END|)") == 3); |ORIG| |BECOMES|hello|END|)") == 3);
std::wstring replaced = LR"(hello  std::wstring replaced = LR"(blahblah 
)"; )";
Replace(replaced); Replace(replaced);
assert(replaced.find(L"さよなら") == std::wstring::npos && 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"こんにちは") == std::wstring::npos &&
replaced.find(L"goodbye") != std::wstring::npos &&
replaced.find(L"idiot") != std::wstring::npos &&
replaced.find(L"hello") != std::wstring::npos
); );
} }
); );