Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/logging/formatter.h
ShadPKG / common / logging / formatter.h
1// SPDX-FileCopyrightText: Copyright 2014 Citra Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#pragma once
5 
6#include <type_traits>
7#include <fmt/format.h>
8 
9// Adapted from https://github.com/fmtlib/fmt/issues/2704
10// a generic formatter for enum classes
11#if FMT_VERSION >= 80100
12template <typename T>
13struct fmt::formatter<T, std::enable_if_t<std::is_enum_v<T>, char>>
14 : formatter<std::underlying_type_t<T>> {
15 template <typename FormatContext>
16 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
17 return fmt::formatter<std::underlying_type_t<T>>::format(
18 static_cast<std::underlying_type_t<T>>(value), ctx);
19 }
20};
21#endif
22 
23namespace fmt {
24template <typename T = std::string_view>
25struct UTF {
26 T data;
27 
28 explicit UTF(const std::u8string_view view) {
29 data = view.empty() ? T{} : T{(const char*)&view.front(), (const char*)&view.back() + 1};
30 }
31 
32 explicit UTF(const std::u8string& str) : UTF(std::u8string_view{str}) {}
33};
34} // namespace fmt
35 
36template <>
37struct fmt::formatter<fmt::UTF<std::string_view>, char> : formatter<std::string_view> {
38 template <typename FormatContext>
39 auto format(const UTF<std::string_view>& wrapper, FormatContext& ctx) const {
40 return formatter<std::string_view>::format(wrapper.data, ctx);
41 }
42};