Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions hide-secret.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <cstring>

void hide_secret(char* const text, const char* const secret)
{
if (text == nullptr || secret == nullptr)
return;
int secretLength = strlen(secret);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Длину text тоже можно заранее проверить

if ( secretLength == 0)
return;
for (int i = 0; text[i] != '\0'; i++)
{
bool matched = true;
bool needEnd = false;
for (int j = i; j - i < secretLength; j++)
{
if (text[j] == '\0')
{
needEnd = true;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если заранее проверить длину text, то в needEnd не будет нужды

}
if (text[j] != secret[j - i])
{
matched = false;
break;
}
}

if (!matched)
{
continue;
}
if (needEnd)
{
break;
}
text[i] = '\x01';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вам, пожалуй, стоит перед работой функции посмотреть, нет ли в ней \x01. И если есть, прекращать работу.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно так же попробовать организовать поиск неиспользуемого символа вместо \x01

}

int lastStart = -1;
for (int i = 0; text[i] != '\0'; i++)
{
if (text[i] == '\x01')
{
lastStart = i;
}

if (lastStart == -1)
{
continue;
}

if (i - lastStart < secretLength)
{
text[i] = 'x';
}
}
}