Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consume all input during extract sequence #4882

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
40 changes: 20 additions & 20 deletions src/AppInstallerCLICore/VTSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,31 @@ namespace AppInstaller::CLI::VirtualTerminal
// Extracts a VT sequence, expected one of the form ESCAPE + prefix + result + suffix, returning the result part.
std::string ExtractSequence(std::istream& inStream, std::string_view prefix, std::string_view suffix)
{
std::string result;
// Force discovery of available input
std::ignore = inStream.peek();

if (inStream.peek() == AICLI_VT_ESCAPE[0])
{
result.resize(4095);
inStream.readsome(&result[0], result.size());
THROW_HR_IF(E_UNEXPECTED, static_cast<size_t>(inStream.gcount()) >= result.size());
static constexpr std::streamsize s_bufferSize = 1024;
char buffer[s_bufferSize];
std::streamsize bytesRead = inStream.readsome(buffer, s_bufferSize);
THROW_HR_IF(E_UNEXPECTED, bytesRead >= s_bufferSize);

result.resize(static_cast<size_t>(inStream.gcount()));
std::string_view resultView{ buffer, static_cast<size_t>(bytesRead) };
size_t escapeIndex = resultView.find(AICLI_VT_ESCAPE[0]);
if (escapeIndex == std::string_view::npos)
{
return {};
}

std::string_view resultView = result;
size_t overheadLength = 1 + prefix.length() + suffix.length();
if (resultView.length() <= overheadLength ||
resultView.substr(1, prefix.length()) != prefix ||
resultView.substr(resultView.length() - suffix.length()) != suffix)
{
result.clear();
}
else
{
result = result.substr(1 + prefix.length(), result.length() - overheadLength);
}
resultView = resultView.substr(escapeIndex);
size_t overheadLength = 1 + prefix.length() + suffix.length();
if (resultView.length() <= overheadLength ||
resultView.substr(1, prefix.length()) != prefix ||
resultView.substr(resultView.length() - suffix.length()) != suffix)
{
return {};
}

return result;
return std::string{ resultView.substr(1 + prefix.length(), resultView.length() - overheadLength) };
}
}

Expand Down
Loading