Increase usage of fmt and FMT_COMPILE (#17012)

This is a rather simple PR overall. It mostly touches either test
or tracing code. Only a few changes affect the actual runtime.

The goal of this changeset is to get rid of the `double` format tables
in the OpenConsole build by using `FMT_COMPILE` everywhere.
This commit is contained in:
Leonard Hecker 2024-04-05 20:55:04 +02:00 committed by GitHub
parent 8d6e7a8a78
commit 6a69b94fc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 30 additions and 75 deletions

View File

@ -29,7 +29,6 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <iomanip>
#include <iterator> #include <iterator>
#include <list> #include <list>
#include <map> #include <map>
@ -44,7 +43,6 @@
#include <set> #include <set>
#include <shared_mutex> #include <shared_mutex>
#include <span> #include <span>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string_view> #include <string_view>
#include <string> #include <string>

View File

@ -466,17 +466,12 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
std::wstring to_string() const std::wstring to_string() const
{ {
std::wstringstream wss; auto str = fmt::format(FMT_COMPILE(L"Bitmap of size {} contains the following dirty regions:\nRuns:"), _sz.to_string());
wss << std::endl
<< L"Bitmap of size " << _sz.to_string() << " contains the following dirty regions:" << std::endl;
wss << L"Runs:" << std::endl;
for (auto& item : *this) for (auto& item : *this)
{ {
wss << L"\t- " << item.to_string() << std::endl; fmt::format_to(std::back_inserter(str), FMT_COMPILE(L"\n\t- {}"), item.to_string());
} }
return str;
return wss.str();
} }
private: private:

View File

@ -198,24 +198,17 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
std::wstring to_string() const std::wstring to_string() const
{ {
std::wstringstream wss; return ToHexString(false);
wss << L"Color " << ToHexString(false);
return wss.str();
} }
std::wstring ToHexString(const bool omitAlpha = false) const std::wstring ToHexString(const bool omitAlpha = false) const
{ {
std::wstringstream wss; auto str = fmt::format(FMT_COMPILE(L"#{:02X}{:02X}{:02X}{:02X}"), r, g, b, a);
wss << L"#" << std::uppercase << std::setfill(L'0') << std::hex; if (omitAlpha)
// Force the compiler to promote from byte to int. Without it, the
// stringstream will try to write the components as chars
wss << std::setw(2) << static_cast<int>(r);
wss << std::setw(2) << static_cast<int>(g);
wss << std::setw(2) << static_cast<int>(b);
if (!omitAlpha)
{ {
wss << std::setw(2) << static_cast<int>(a); str.resize(7);
} }
return wss.str(); return str;
} }
}; };
#pragma warning(pop) #pragma warning(pop)

View File

@ -86,7 +86,6 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
inline constexpr wil::zwstring_view system_env_var_root{ LR"(SYSTEM\CurrentControlSet\Control\Session Manager\Environment)" }; inline constexpr wil::zwstring_view system_env_var_root{ LR"(SYSTEM\CurrentControlSet\Control\Session Manager\Environment)" };
inline constexpr wil::zwstring_view user_env_var_root{ LR"(Environment)" }; inline constexpr wil::zwstring_view user_env_var_root{ LR"(Environment)" };
inline constexpr wil::zwstring_view user_volatile_env_var_root{ LR"(Volatile Environment)" }; inline constexpr wil::zwstring_view user_volatile_env_var_root{ LR"(Volatile Environment)" };
inline constexpr wil::zwstring_view user_volatile_session_env_var_root_pattern{ LR"(Volatile Environment\{0:d})" };
}; };
}; };
@ -472,7 +471,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
// not processing autoexec.bat // not processing autoexec.bat
get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_env_var_root); get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_env_var_root);
get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root); get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root);
get_vars_from_registry(HKEY_CURRENT_USER, fmt::format(til::details::vars::reg::user_volatile_session_env_var_root_pattern, NtCurrentTeb()->ProcessEnvironmentBlock->SessionId)); get_vars_from_registry(HKEY_CURRENT_USER, fmt::format(FMT_COMPILE(LR"(Volatile Environment\{})"), NtCurrentTeb()->ProcessEnvironmentBlock->SessionId));
} }
std::wstring to_string() const std::wstring to_string() const

View File

@ -60,14 +60,6 @@ IFACEMETHODIMP UiaTextRange::Clone(_Outptr_result_maybenull_ ITextRangeProvider*
*ppRetVal = nullptr; *ppRetVal = nullptr;
RETURN_IF_FAILED(MakeAndInitialize<UiaTextRange>(ppRetVal, *this)); RETURN_IF_FAILED(MakeAndInitialize<UiaTextRange>(ppRetVal, *this));
#if defined(_DEBUG) && defined(UiaTextRangeBase_DEBUG_MSGS)
OutputDebugString(L"Clone\n");
std::wstringstream ss;
ss << _id << L" cloned to " << (static_cast<UiaTextRangeBase*>(*ppRetVal))->_id;
std::wstring str = ss.str();
OutputDebugString(str.c_str());
OutputDebugString(L"\n");
#endif
// TODO GitHub #1914: Re-attach Tracing to UIA Tree // TODO GitHub #1914: Re-attach Tracing to UIA Tree
// tracing // tracing
/*ApiMsgClone apiMsg; /*ApiMsgClone apiMsg;

View File

@ -721,9 +721,7 @@ using namespace Microsoft::Console::Types;
{ {
try try
{ {
std::wstringstream wss; const auto wstr = fmt::format(FMT_COMPILE(L"{:08d}"), wParam);
wss << std::setfill(L'0') << std::setw(8) << wParam;
auto wstr = wss.str();
LoadKeyboardLayout(wstr.c_str(), KLF_ACTIVATE); LoadKeyboardLayout(wstr.c_str(), KLF_ACTIVATE);
} }
catch (...) catch (...)

View File

@ -50,5 +50,9 @@
#include <base/numerics/safe_math.h> #include <base/numerics/safe_math.h>
#pragma warning(pop) #pragma warning(pop)
// {fmt}, a C++20-compatible formatting library
#include <fmt/format.h>
#include <fmt/compile.h>
#include <til.h> #include <til.h>
#include <til/bit.h> #include <til/bit.h>

View File

@ -65,29 +65,14 @@ UiaTracing::~UiaTracing() noexcept
std::wstring UiaTracing::_getValue(const ScreenInfoUiaProviderBase& siup) noexcept std::wstring UiaTracing::_getValue(const ScreenInfoUiaProviderBase& siup) noexcept
{ {
std::wstringstream stream; return fmt::format(FMT_COMPILE(L"_id:{}"), siup.GetId());
stream << "_id: " << siup.GetId();
return stream.str();
} }
std::wstring UiaTracing::_getValue(const UiaTextRangeBase& utr) noexcept std::wstring UiaTracing::_getValue(const UiaTextRangeBase& utr) noexcept
try
{ {
const auto start = utr.GetEndpoint(TextPatternRangeEndpoint_Start); const auto start = utr.GetEndpoint(TextPatternRangeEndpoint_Start);
const auto end = utr.GetEndpoint(TextPatternRangeEndpoint_End); const auto end = utr.GetEndpoint(TextPatternRangeEndpoint_End);
return fmt::format(FMT_COMPILE(L"_id:{} _start:{},{} _end:{},{} _degenerate:{} _wordDelimiters:{} content:{}"), utr.GetId(), start.x, start.y, end.x, end.y, utr.IsDegenerate(), utr._wordDelimiters, utr._getTextValue());
std::wstringstream stream;
stream << " _id: " << utr.GetId();
stream << " _start: { " << start.x << ", " << start.y << " }";
stream << " _end: { " << end.x << ", " << end.y << " }";
stream << " _degenerate: " << utr.IsDegenerate();
stream << " _wordDelimiters: " << utr._wordDelimiters;
stream << " content: " << utr._getTextValue();
return stream.str();
}
catch (...)
{
return {};
} }
std::wstring UiaTracing::_getValue(const TextPatternRangeEndpoint endpoint) noexcept std::wstring UiaTracing::_getValue(const TextPatternRangeEndpoint endpoint) noexcept
@ -485,7 +470,7 @@ void UiaTracing::TextProvider::get_ProviderOptions(const ScreenInfoUiaProviderBa
EnsureRegistration(); EnsureRegistration();
if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE)) if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE))
{ {
auto getOptions = [options]() { static constexpr auto getOptions = [](ProviderOptions options) {
switch (options) switch (options)
{ {
case ProviderOptions_ServerSideProvider: case ProviderOptions_ServerSideProvider:
@ -499,7 +484,7 @@ void UiaTracing::TextProvider::get_ProviderOptions(const ScreenInfoUiaProviderBa
g_UiaProviderTraceProvider, g_UiaProviderTraceProvider,
"ScreenInfoUiaProvider::get_ProviderOptions", "ScreenInfoUiaProvider::get_ProviderOptions",
TraceLoggingValue(_getValue(siup).c_str(), "base"), TraceLoggingValue(_getValue(siup).c_str(), "base"),
TraceLoggingValue(getOptions(), "providerOptions"), TraceLoggingValue(getOptions(options), "providerOptions"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE)); TraceLoggingKeyword(TIL_KEYWORD_TRACE));
} }
@ -510,7 +495,7 @@ void UiaTracing::TextProvider::GetPatternProvider(const ScreenInfoUiaProviderBas
EnsureRegistration(); EnsureRegistration();
if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE)) if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE))
{ {
auto getPattern = [patternId]() { static constexpr auto getPattern = [](PATTERNID patternId) {
switch (patternId) switch (patternId)
{ {
case UIA_TextPatternId: case UIA_TextPatternId:
@ -524,7 +509,7 @@ void UiaTracing::TextProvider::GetPatternProvider(const ScreenInfoUiaProviderBas
g_UiaProviderTraceProvider, g_UiaProviderTraceProvider,
"ScreenInfoUiaProvider::get_ProviderOptions", "ScreenInfoUiaProvider::get_ProviderOptions",
TraceLoggingValue(_getValue(siup).c_str(), "base"), TraceLoggingValue(_getValue(siup).c_str(), "base"),
TraceLoggingValue(getPattern(), "patternId"), TraceLoggingValue(getPattern(patternId), "patternId"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE)); TraceLoggingKeyword(TIL_KEYWORD_TRACE));
} }
@ -535,7 +520,7 @@ void UiaTracing::TextProvider::GetPropertyValue(const ScreenInfoUiaProviderBase&
EnsureRegistration(); EnsureRegistration();
if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE)) if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE))
{ {
auto getProperty = [propertyId]() { static constexpr auto getProperty = [](PROPERTYID propertyId) {
switch (propertyId) switch (propertyId)
{ {
case UIA_ControlTypePropertyId: case UIA_ControlTypePropertyId:
@ -565,7 +550,7 @@ void UiaTracing::TextProvider::GetPropertyValue(const ScreenInfoUiaProviderBase&
g_UiaProviderTraceProvider, g_UiaProviderTraceProvider,
"ScreenInfoUiaProvider::GetPropertyValue", "ScreenInfoUiaProvider::GetPropertyValue",
TraceLoggingValue(_getValue(siup).c_str(), "base"), TraceLoggingValue(_getValue(siup).c_str(), "base"),
TraceLoggingValue(getProperty(), "propertyId"), TraceLoggingValue(getProperty(propertyId), "propertyId"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE)); TraceLoggingKeyword(TIL_KEYWORD_TRACE));
} }
@ -677,17 +662,15 @@ void UiaTracing::TextProvider::RangeFromPoint(const ScreenInfoUiaProviderBase& s
EnsureRegistration(); EnsureRegistration();
if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE)) if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE))
{ {
auto getPoint = [point]() { static constexpr auto getPoint = [](const UiaPoint& point) {
std::wstringstream stream; return fmt::format(FMT_COMPILE(L"{},{}"), (float)point.x, (float)point.y);
stream << "{ " << point.x << ", " << point.y << " }";
return stream.str();
}; };
TraceLoggingWrite( TraceLoggingWrite(
g_UiaProviderTraceProvider, g_UiaProviderTraceProvider,
"ScreenInfoUiaProvider::RangeFromPoint", "ScreenInfoUiaProvider::RangeFromPoint",
TraceLoggingValue(_getValue(siup).c_str(), "base"), TraceLoggingValue(_getValue(siup).c_str(), "base"),
TraceLoggingValue(getPoint().c_str(), "uiaPoint"), TraceLoggingValue(getPoint(point).c_str(), "uiaPoint"),
TraceLoggingValue(_getValue(result).c_str(), "result (utr)"), TraceLoggingValue(_getValue(result).c_str(), "result (utr)"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE)); TraceLoggingKeyword(TIL_KEYWORD_TRACE));
@ -714,7 +697,7 @@ void UiaTracing::TextProvider::get_SupportedTextSelection(const ScreenInfoUiaPro
EnsureRegistration(); EnsureRegistration();
if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE)) if (TraceLoggingProviderEnabled(g_UiaProviderTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE))
{ {
auto getResult = [result]() { static constexpr auto getResult = [](SupportedTextSelection result) {
switch (result) switch (result)
{ {
case SupportedTextSelection_Single: case SupportedTextSelection_Single:
@ -728,7 +711,7 @@ void UiaTracing::TextProvider::get_SupportedTextSelection(const ScreenInfoUiaPro
g_UiaProviderTraceProvider, g_UiaProviderTraceProvider,
"ScreenInfoUiaProvider::get_SupportedTextSelection", "ScreenInfoUiaProvider::get_SupportedTextSelection",
TraceLoggingValue(_getValue(siup).c_str(), "base"), TraceLoggingValue(_getValue(siup).c_str(), "base"),
TraceLoggingValue(getResult(), "result"), TraceLoggingValue(getResult(result), "result"),
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE)); TraceLoggingKeyword(TIL_KEYWORD_TRACE));
} }

View File

@ -93,14 +93,7 @@ GUID Utils::CreateGuid()
// - a string representation of the color // - a string representation of the color
std::string Utils::ColorToHexString(const til::color color) std::string Utils::ColorToHexString(const til::color color)
{ {
std::stringstream ss; return fmt::format(FMT_COMPILE("#{:02X}{:02X}{:02X}"), color.r, color.g, color.b);
ss << "#" << std::uppercase << std::setfill('0') << std::hex;
// Force the compiler to promote from byte to int. Without it, the
// stringstream will try to write the components as chars
ss << std::setw(2) << static_cast<int>(color.r);
ss << std::setw(2) << static_cast<int>(color.g);
ss << std::setw(2) << static_cast<int>(color.b);
return ss.str();
} }
// Function Description: // Function Description:
@ -809,7 +802,7 @@ std::tuple<std::wstring, std::wstring> Utils::MangleStartingDirectoryForWSL(std:
} }
return { return {
fmt::format(LR"("{}" --cd "{}" {})", executablePath.native(), mangledDirectory, arguments), fmt::format(FMT_COMPILE(LR"("{}" --cd "{}" {})"), executablePath.native(), mangledDirectory, arguments),
std::wstring{} std::wstring{}
}; };
} }