From fa09ecc17f3ccb2b2787fb3065184ce681962002 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Fri, 27 Jan 2023 17:20:06 +0100 Subject: [PATCH] Minor improvements for SplitToOem --- src/host/misc.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/host/misc.cpp b/src/host/misc.cpp index f30fb7d3acd..671da86d7a6 100644 --- a/src/host/misc.cpp +++ b/src/host/misc.cpp @@ -191,22 +191,23 @@ BOOL CheckBisectProcessW(const SCREEN_INFORMATION& ScreenInfo, // Note: may throw on error void SplitToOem(std::deque>& events) { - const auto codepage = ServiceLocator::LocateGlobals().getConsoleInformation().CP; - - // convert events to oem codepage + const auto cp = ServiceLocator::LocateGlobals().getConsoleInformation().CP; std::deque> 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(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(length) }; - for (auto& ch : str) + for (const auto& ch : str) { auto tempEvent = std::make_unique(*pKeyEvent); tempEvent->SetCharData(ch); @@ -218,12 +219,8 @@ void SplitToOem(std::deque>& 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: