2019-06-14 01:06:34 +08:00
|
|
|
|
#include "extension.h"
|
|
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
|
{
|
|
|
|
|
if (sentenceInfo["text number"] == 0) return false;
|
|
|
|
|
|
|
|
|
|
std::vector<int> repeatNumbers(sentence.size() + 1, 0);
|
2021-07-01 10:58:40 +08:00
|
|
|
|
for (int i = 0; i < sentence.size(); ++i)
|
2019-06-14 01:06:34 +08:00
|
|
|
|
{
|
2021-07-01 10:58:40 +08:00
|
|
|
|
if (sentence[i] != sentence[i + 1])
|
2019-06-14 01:06:34 +08:00
|
|
|
|
{
|
2021-07-01 10:58:40 +08:00
|
|
|
|
int j = i;
|
|
|
|
|
while (sentence[j] == sentence[i] && --j >= 0);
|
|
|
|
|
repeatNumbers[i - j] += 1;
|
2019-06-14 01:06:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-01 10:58:40 +08:00
|
|
|
|
int repeatNumber = std::distance(repeatNumbers.begin(), std::max_element(repeatNumbers.rbegin(), repeatNumbers.rend()).base() - 1);
|
|
|
|
|
if (repeatNumber < 2) return false;
|
2019-06-14 01:06:34 +08:00
|
|
|
|
|
|
|
|
|
std::wstring newSentence;
|
|
|
|
|
for (int i = 0; i < sentence.size();)
|
|
|
|
|
{
|
2021-07-01 10:58:40 +08:00
|
|
|
|
newSentence.push_back(sentence[i]);
|
2019-06-14 01:06:34 +08:00
|
|
|
|
for (int j = i; j <= sentence.size(); ++j)
|
|
|
|
|
{
|
2021-07-01 10:58:40 +08:00
|
|
|
|
if (j == sentence.size() || sentence[i] != sentence[j])
|
2019-06-14 01:06:34 +08:00
|
|
|
|
{
|
|
|
|
|
i += (j - i) % repeatNumber == 0 ? repeatNumber : 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sentence = newSentence;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(
|
|
|
|
|
{
|
2019-10-02 17:30:14 +08:00
|
|
|
|
InfoForExtension nonConsole[] = { { "text number", 1 }, {} };
|
|
|
|
|
|
2019-06-14 01:06:34 +08:00
|
|
|
|
std::wstring repeatedChars = L"aaaaaaaaaaaabbbbbbcccdddaabbbcccddd";
|
2021-07-01 10:58:40 +08:00
|
|
|
|
std::wstring someRepeatedChars = L"abcdefaabbccddeeff";
|
2019-10-02 17:30:14 +08:00
|
|
|
|
ProcessSentence(repeatedChars, { nonConsole });
|
2021-07-01 10:58:40 +08:00
|
|
|
|
ProcessSentence(someRepeatedChars, { nonConsole });
|
2019-06-14 01:06:34 +08:00
|
|
|
|
assert(repeatedChars.find(L"aaaabbcd") == 0);
|
2021-07-01 10:58:40 +08:00
|
|
|
|
assert(someRepeatedChars == L"abcdefabcdef");
|
2019-06-14 01:06:34 +08:00
|
|
|
|
|
|
|
|
|
std::wstring empty = L"", one = L" ", normal = L"This is a normal sentence. はい";
|
2019-10-02 17:30:14 +08:00
|
|
|
|
ProcessSentence(empty, { nonConsole });
|
|
|
|
|
ProcessSentence(one, { nonConsole });
|
|
|
|
|
ProcessSentence(normal, { nonConsole });
|
2019-06-14 01:06:34 +08:00
|
|
|
|
assert(empty == L"" && one == L" " && normal == L"This is a normal sentence. はい");
|
|
|
|
|
}
|
|
|
|
|
);
|