Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/Windows/libraries/formatters/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(m_formatters PUBLIC FILE_SET HEADERS FILES
m/formatters/FILE_ACTION.h
m/formatters/FILE_NOTIFY_EXTENDED_INFORMATION.h
m/formatters/FILE_NOTIFY_INFORMATION.h
m/formatters/HKEY.h
m/formatters/HRESULT.h
m/formatters/NTSTATUS.h
m/formatters/OVERLAPPED.h
Expand Down
82 changes: 82 additions & 0 deletions src/Windows/libraries/formatters/include/m/formatters/HKEY.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

#pragma once

#include <algorithm>
#include <array>
#include <chrono>
Comment on lines +6 to +7
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

Unused includes detected. The headers <array> and <chrono> are included but not used in this file. These should be removed to reduce compilation dependencies and improve build times.

Suggested change
#include <array>
#include <chrono>

Copilot uses AI. Check for mistakes.
#include <format>
#include <string_view>

#include <Windows.h>

using namespace std::string_view_literals;

template <typename CharT>
struct std::formatter<HKEY, CharT>
{
template <typename ParseContext>
constexpr ParseContext::iterator
parse(ParseContext& ctx)
{
auto it = ctx.begin();
auto const end = ctx.end();

if (it != end && *it != '}')
throw std::format_error("Invalid format string");

return it;
}

template <typename FormatContext>
FormatContext::iterator
format(HKEY hkey, FormatContext& ctx) const
{
auto out = ctx.out();

//
// Macros are to be hated but they are to be tolerated here.
//
// There are a number of "named" HKEY values.
//

#pragma push_macro("X")

#undef X
#define X(p) \
case reinterpret_cast<uint64_t>(p): \
{ \
constexpr auto lit = #p##sv; \
return std::ranges::copy(lit.begin(), lit.end(), out).out; \
}

switch (reinterpret_cast<uint64_t>(hkey))
{
X(HKEY_CURRENT_USER)
X(HKEY_CLASSES_ROOT)
X(HKEY_LOCAL_MACHINE)
X(HKEY_USERS)
X(HKEY_PERFORMANCE_DATA)
X(HKEY_PERFORMANCE_TEXT)
X(HKEY_PERFORMANCE_NLSTEXT)
X(HKEY_CURRENT_CONFIG)
X(HKEY_DYN_DATA)
X(HKEY_CURRENT_USER_LOCAL_SETTINGS)
}

#pragma pop_macro("X")

if constexpr (std::is_same_v<CharT, char>)
{
return std::format_to(out, "{{hkey 0x{:x}}}", reinterpret_cast<uintptr_t>(hkey));
}
else if constexpr (std::is_same_v<CharT, wchar_t>)
{
return std::format_to(out, L"{{hkey 0x{:x}}}", reinterpret_cast<uintptr_t>(hkey));
}
else
{
throw std::runtime_error("Bad CharT");
}
}
};
3 changes: 2 additions & 1 deletion src/Windows/libraries/formatters/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ add_executable(${TEST_EXE_NAME}
test_FILE_ACTION.cpp
test_FILE_NOTIFY_EXTENDED_INFORMATION.cpp
test_FILE_NOTIFY_INFORMATION.cpp
test_HKEY.cpp
test_HRESULT.cpp
test_NTSTATUS.cpp
test_OVERLAPPED.cpp
test_Win32ErrorCode.cpp
)
)
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

Inconsistent whitespace formatting. The closing parenthesis has a leading space which differs from the standard formatting convention. For consistency with typical CMake style, the closing parenthesis should either be on the same line as the last entry or on its own line without leading whitespace.

Suggested change
)
)

Copilot uses AI. Check for mistakes.

target_compile_features(${TEST_EXE_NAME} PUBLIC ${M_CXX_STD})

Expand Down
34 changes: 34 additions & 0 deletions src/Windows/libraries/formatters/test/test_HKEY.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <gtest/gtest.h>

#include <format>
#include <string>
#include <string_view>

#include <m/formatters/HKEY.h>

#include <Windows.h>

using namespace std::string_literals;
using namespace std::string_view_literals;

TEST(HKEY, format_HKEY_CURRENT_USER)
{
auto s = std::format(L"{}", HKEY_CURRENT_USER);
EXPECT_EQ(s, L"HKEY_CURRENT_USER"s);
}

TEST(HKEY, format_HKEY_LOCAL_MACHINE)
{
auto s = std::format(L"{}", HKEY_LOCAL_MACHINE);
EXPECT_EQ(s, L"HKEY_LOCAL_MACHINE"s);
}

TEST(HKEY, format_default_hkey)
{
auto s = std::format(L"{}", HKEY{});
EXPECT_EQ(s, L"{hkey 0x0}"s);
}

Comment on lines +17 to +34
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

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

Test coverage is incomplete. The implementation handles 10 different named HKEY values (HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA, HKEY_PERFORMANCE_TEXT, HKEY_PERFORMANCE_NLSTEXT, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA, HKEY_CURRENT_USER_LOCAL_SETTINGS) but only 2 are tested. Additionally, all tests use wide character formatting (L"{}") but narrow character formatting is also supported and should be tested.

Copilot uses AI. Check for mistakes.
Loading