Skip to content

Commit

Permalink
Minor improvements for SplitToOem (#14746)
Browse files Browse the repository at this point in the history
When working on #14745 I noticed that `SplitToOem` was in a bit of a poor state
as well. Instead of simply iterating over its `deque` argument and writing the
results into a new `deque` it used `pop` to advance the head of both queues.
This isn't quite exception safe and rather bloaty. Additionally there's no need
to call `WideCharToMultiByte` twice on each character if we know that the most
verbose encoding is UTF-8 which can't be any more than 4 chars anyways.

Related to #8000.

## PR Checklist
* 2 unit tests cover this ✅
  • Loading branch information
lhecker authored Feb 2, 2023
1 parent ddc349b commit 8100d24
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/host/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,23 @@ BOOL CheckBisectProcessW(const SCREEN_INFORMATION& ScreenInfo,
// Note: may throw on error
void SplitToOem(std::deque<std::unique_ptr<IInputEvent>>& events)
{
const auto codepage = ServiceLocator::LocateGlobals().getConsoleInformation().CP;

// convert events to oem codepage
const auto cp = ServiceLocator::LocateGlobals().getConsoleInformation().CP;
std::deque<std::unique_ptr<IInputEvent>> convertedEvents;
while (!events.empty())

for (auto& currentEvent : events)
{
auto currentEvent = std::move(events.front());
events.pop_front();
if (currentEvent->EventType() == InputEventType::KeyEvent)
{
const auto pKeyEvent = static_cast<const KeyEvent* const>(currentEvent.get());
// convert from wchar to char
std::wstring wstr{ pKeyEvent->GetCharData() };
const auto str = ConvertToA(codepage, wstr);
const auto wch = pKeyEvent->GetCharData();

char buffer[8];
const auto length = WideCharToMultiByte(cp, 0, &wch, 1, &buffer[0], sizeof(buffer), nullptr, nullptr);
THROW_LAST_ERROR_IF(length <= 0);

const std::string_view str{ &buffer[0], gsl::narrow_cast<size_t>(length) };

for (auto& ch : str)
for (const auto& ch : str)
{
auto tempEvent = std::make_unique<KeyEvent>(*pKeyEvent);
tempEvent->SetCharData(ch);
Expand All @@ -218,12 +219,8 @@ void SplitToOem(std::deque<std::unique_ptr<IInputEvent>>& events)
convertedEvents.push_back(std::move(currentEvent));
}
}
// move all events back
while (!convertedEvents.empty())
{
events.push_back(std::move(convertedEvents.front()));
convertedEvents.pop_front();
}

events = std::move(convertedEvents);
}

// Routine Description:
Expand Down

0 comments on commit 8100d24

Please sign in to comment.