forked from Public-Mirror/Textractor
store trie match and use if another isnt found
This commit is contained in:
parent
ec0b9c077c
commit
f62f90a068
@ -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
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user