From 34670c6151d28dee8284e90f7957e470fc03f9d3 Mon Sep 17 00:00:00 2001 From: TellowKrinkle Date: Tue, 21 Jul 2026 20:20:07 -0500 Subject: [PATCH] Common: Move To and FromChars implementations out of header --- common/FileSystem.cpp | 1 + common/StringUtil.cpp | 154 +++++++++++++++++++++++ common/StringUtil.h | 148 ++-------------------- pcsx2/GS/GSState.cpp | 1 + pcsx2/GS/Renderers/SW/GSRasterizer.cpp | 4 +- pcsx2/GS/Renderers/SW/GSRendererSW.cpp | 2 + pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp | 2 + pcsx2/IPU/IPU_MultiISA.cpp | 2 + pcsx2/x86/Vif_Dynarec.cpp | 2 + pcsx2/x86/Vif_UnpackSSE.cpp | 2 + 10 files changed, 178 insertions(+), 140 deletions(-) diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 24eddeb3b8..1e68f1e1a2 100644 --- a/common/FileSystem.cpp +++ b/common/FileSystem.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp index c17c36bf53..83933beec0 100644 --- a/common/StringUtil.cpp +++ b/common/StringUtil.cpp @@ -5,17 +5,29 @@ #include "StringUtil.h" #include +#include #include #include +#include #include #include #include "fmt/format.h" +#include "fast_float/fast_float.h" #ifdef _WIN32 #include "RedtapeWindows.h" #endif +// Older versions of libstdc++ are missing support for from_chars() with floats, and was only recently +// merged in libc++. So, just fall back to stringstream (yuck!) on everywhere except MSVC. +#if !defined(_MSC_VER) +#include +#ifdef __APPLE__ +#include +#endif +#endif + std::string StringUtil::StdStringFromFormat(const char* format, ...) { std::va_list ap; @@ -176,6 +188,148 @@ std::size_t StringUtil::Strlcpy(char* dst, const std::string_view src, std::size return len; } +template requires std::is_integral_v +std::optional StringUtil::FromChars(const std::string_view str, int base) +{ + T value; + + const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value, base); + if (result.ec != std::errc()) + return std::nullopt; + + return value; +} + +template std::optional StringUtil::FromChars(const std::string_view, int); +template std::optional StringUtil::FromChars(const std::string_view, int); +template std::optional StringUtil::FromChars(const std::string_view, int); +template std::optional StringUtil::FromChars(const std::string_view, int); + +template requires std::is_integral_v +std::optional StringUtil::FromChars(const std::string_view str, int base, std::string_view* endptr) +{ + T value; + + const char* ptr = str.data(); + const char* end = ptr + str.length(); + const std::from_chars_result result = std::from_chars(ptr, end, value, base); + if (result.ec != std::errc()) + return std::nullopt; + + if (endptr) + *endptr = (result.ptr < end) ? std::string_view(result.ptr, end - result.ptr) : std::string_view(); + + return value; +} + +template std::optional StringUtil::FromChars(const std::string_view, int, std::string_view*); +template std::optional StringUtil::FromChars(const std::string_view, int, std::string_view*); +template std::optional StringUtil::FromChars(const std::string_view, int, std::string_view*); +template std::optional StringUtil::FromChars(const std::string_view, int, std::string_view*); + +template requires std::is_floating_point_v +std::optional StringUtil::FromChars(const std::string_view str) +{ + T value; + + const fast_float::from_chars_result result = fast_float::from_chars(str.data(), str.data() + str.length(), value); + if (result.ec != std::errc()) + return std::nullopt; + + return value; +} + +template std::optional StringUtil::FromChars(const std::string_view); +template std::optional StringUtil::FromChars(const std::string_view); + +template requires std::is_floating_point_v +std::optional StringUtil::FromChars(const std::string_view str, std::string_view* endptr) +{ + T value; + + const char* ptr = str.data(); + const char* end = ptr + str.length(); + const fast_float::from_chars_result result = fast_float::from_chars(ptr, end, value); + if (result.ec != std::errc()) + return std::nullopt; + + if (endptr) + *endptr = (result.ptr < end) ? std::string_view(result.ptr, end - result.ptr) : std::string_view(); + + return value; +} + +template std::optional StringUtil::FromChars(const std::string_view, std::string_view*); +template std::optional StringUtil::FromChars(const std::string_view, std::string_view*); + +template requires std::is_integral_v +std::string StringUtil::ToChars(T value, int base) +{ + // to_chars() requires macOS 10.15+. +#if !defined(__APPLE__) || MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15 + constexpr size_t MAX_SIZE = 32; + char buf[MAX_SIZE]; + std::string ret; + + const std::to_chars_result result = std::to_chars(buf, buf + MAX_SIZE, value, base); + if (result.ec == std::errc()) + ret.append(buf, result.ptr - buf); + + return ret; +#else + std::ostringstream ss; + ss.imbue(std::locale::classic()); + ss << std::setbase(base) << value; + return ss.str(); +#endif +} + +template std::string StringUtil::ToChars(s32, int); +template std::string StringUtil::ToChars(u32, int); + +template requires std::is_floating_point_v +std::string StringUtil::ToChars(T value) +{ + // No to_chars() in older versions of libstdc++/libc++. +#ifdef _MSC_VER + constexpr size_t MAX_SIZE = 64; + char buf[MAX_SIZE]; + std::string ret; + const std::to_chars_result result = std::to_chars(buf, buf + MAX_SIZE, value); + if (result.ec == std::errc()) + ret.append(buf, result.ptr - buf); + return ret; +#else + std::ostringstream ss; + ss.imbue(std::locale::classic()); + ss << value; + return ss.str(); +#endif +} + +template std::string StringUtil::ToChars(float); +template std::string StringUtil::ToChars(double); + +template <> +std::optional StringUtil::FromChars(const std::string_view str, int base) +{ + if (Strncasecmp("true", str.data(), str.length()) == 0 || Strncasecmp("yes", str.data(), str.length()) == 0 || + Strncasecmp("on", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0 || + Strncasecmp("enabled", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0) + { + return true; + } + + if (Strncasecmp("false", str.data(), str.length()) == 0 || Strncasecmp("no", str.data(), str.length()) == 0 || + Strncasecmp("off", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0 || + Strncasecmp("disabled", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0) + { + return false; + } + + return std::nullopt; +} + std::optional> StringUtil::DecodeHex(const std::string_view in) { std::vector data; diff --git a/common/StringUtil.h b/common/StringUtil.h index 4a4f691be1..6b9e635453 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -4,28 +4,14 @@ #pragma once #include "Pcsx2Types.h" #include -#include #include #include #include -#include #include #include #include #include -#include "fast_float/fast_float.h" - -// Older versions of libstdc++ are missing support for from_chars() with floats, and was only recently -// merged in libc++. So, just fall back to stringstream (yuck!) on everywhere except MSVC. -#if !defined(_MSC_VER) -#include -#include -#ifdef __APPLE__ -#include -#endif -#endif - namespace StringUtil { /// Constructs a std::string from a format string. @@ -65,134 +51,18 @@ namespace StringUtil #endif } - /// Wrapper around std::from_chars - template - requires std::is_integral_v - inline std::optional FromChars(const std::string_view str, int base = 10) - { - T value; - - const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value, base); - if (result.ec != std::errc()) - return std::nullopt; - - return value; - } - template - requires std::is_integral_v - inline std::optional FromChars(const std::string_view str, int base, std::string_view* endptr) - { - T value; - - const char* ptr = str.data(); - const char* end = ptr + str.length(); - const std::from_chars_result result = std::from_chars(ptr, end, value, base); - if (result.ec != std::errc()) - return std::nullopt; - - if (endptr) - *endptr = (result.ptr < end) ? std::string_view(result.ptr, end - result.ptr) : std::string_view(); - - return value; - } - - template - requires std::is_floating_point_v - inline std::optional FromChars(const std::string_view str) - { - T value; - - const fast_float::from_chars_result result = fast_float::from_chars(str.data(), str.data() + str.length(), value); - if (result.ec != std::errc()) - return std::nullopt; - - return value; - } - template - requires std::is_floating_point_v - inline std::optional FromChars(const std::string_view str, std::string_view* endptr) - { - T value; - - const char* ptr = str.data(); - const char* end = ptr + str.length(); - const fast_float::from_chars_result result = fast_float::from_chars(ptr, end, value); - if (result.ec != std::errc()) - return std::nullopt; - - if (endptr) - *endptr = (result.ptr < end) ? std::string_view(result.ptr, end - result.ptr) : std::string_view(); - - return value; - } - - /// Wrapper around std::to_chars - template - requires std::is_integral_v - inline std::string ToChars(T value, int base = 10) - { - // to_chars() requires macOS 10.15+. -#if !defined(__APPLE__) || MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15 - constexpr size_t MAX_SIZE = 32; - char buf[MAX_SIZE]; - std::string ret; - - const std::to_chars_result result = std::to_chars(buf, buf + MAX_SIZE, value, base); - if (result.ec == std::errc()) - ret.append(buf, result.ptr - buf); - - return ret; -#else - std::ostringstream ss; - ss.imbue(std::locale::classic()); - ss << std::setbase(base) << value; - return ss.str(); -#endif - } - - template - requires std::is_floating_point_v - inline std::string ToChars(T value) - { - // No to_chars() in older versions of libstdc++/libc++. -#ifdef _MSC_VER - constexpr size_t MAX_SIZE = 64; - char buf[MAX_SIZE]; - std::string ret; - const std::to_chars_result result = std::to_chars(buf, buf + MAX_SIZE, value); - if (result.ec == std::errc()) - ret.append(buf, result.ptr - buf); - return ret; -#else - std::ostringstream ss; - ss.imbue(std::locale::classic()); - ss << value; - return ss.str(); -#endif - } + /// Wrapper around `std::from_chars` + template requires std::is_integral_v std::optional FromChars(const std::string_view str, int base = 10); + template requires std::is_integral_v std::optional FromChars(const std::string_view str, int base, std::string_view* endptr); + template requires std::is_floating_point_v std::optional FromChars(const std::string_view str); + template requires std::is_floating_point_v std::optional FromChars(const std::string_view str, std::string_view* endptr); + /// Wrapper around `std::to_chars` + template requires std::is_integral_v std::string ToChars(T value, int base = 10); + template requires std::is_floating_point_v std::string ToChars(T value); /// Explicit override for booleans - template <> - inline std::optional FromChars(const std::string_view str, int base) - { - if (Strncasecmp("true", str.data(), str.length()) == 0 || Strncasecmp("yes", str.data(), str.length()) == 0 || - Strncasecmp("on", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0 || - Strncasecmp("enabled", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0) - { - return true; - } - - if (Strncasecmp("false", str.data(), str.length()) == 0 || Strncasecmp("no", str.data(), str.length()) == 0 || - Strncasecmp("off", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0 || - Strncasecmp("disabled", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0) - { - return false; - } - - return std::nullopt; - } - + template <> std::optional FromChars(const std::string_view str, int base); template <> inline std::string ToChars(bool value, int base) { diff --git a/pcsx2/GS/GSState.cpp b/pcsx2/GS/GSState.cpp index 76327f82bc..317e247f02 100644 --- a/pcsx2/GS/GSState.cpp +++ b/pcsx2/GS/GSState.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include diff --git a/pcsx2/GS/Renderers/SW/GSRasterizer.cpp b/pcsx2/GS/Renderers/SW/GSRasterizer.cpp index ab369577a7..8fef9645d4 100644 --- a/pcsx2/GS/Renderers/SW/GSRasterizer.cpp +++ b/pcsx2/GS/Renderers/SW/GSRasterizer.cpp @@ -13,6 +13,8 @@ #include "common/Console.h" #include "common/StringUtil.h" +#include + #define ENABLE_DRAW_STATS 0 MULTI_ISA_UNSHARED_IMPL; @@ -1663,4 +1665,4 @@ const GSRasterizer::DrawEdgeLinePtr GSRasterizer::m_draw_edge_line[2][2][2][2] = #undef INIT0 #undef INIT1 #undef INIT2 -#undef INIT3 \ No newline at end of file +#undef INIT3 diff --git a/pcsx2/GS/Renderers/SW/GSRendererSW.cpp b/pcsx2/GS/Renderers/SW/GSRendererSW.cpp index c0277a99da..caddcdfaad 100644 --- a/pcsx2/GS/Renderers/SW/GSRendererSW.cpp +++ b/pcsx2/GS/Renderers/SW/GSRendererSW.cpp @@ -8,6 +8,8 @@ #include "common/StringUtil.h" +#include + MULTI_ISA_UNSHARED_IMPL; GSRenderer* CURRENT_ISA::makeGSRendererSW(int threads) diff --git a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp index f9aab2a812..5d2f99fa44 100644 --- a/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp +++ b/pcsx2/GS/Renderers/Vulkan/GSDeviceVK.cpp @@ -29,6 +29,8 @@ #include #include +#include + // Tweakables enum : u32 { diff --git a/pcsx2/IPU/IPU_MultiISA.cpp b/pcsx2/IPU/IPU_MultiISA.cpp index 7398b23e82..cdd905616b 100644 --- a/pcsx2/IPU/IPU_MultiISA.cpp +++ b/pcsx2/IPU/IPU_MultiISA.cpp @@ -14,6 +14,8 @@ #include "IPU/yuv2rgb.h" #include "IPU/IPU_MultiISA.h" +#include + // the IPU is fixed to 16 byte strides (128-bit / QWC resolution): static const uint decoder_stride = 16; diff --git a/pcsx2/x86/Vif_Dynarec.cpp b/pcsx2/x86/Vif_Dynarec.cpp index b373e81db1..7694895b96 100644 --- a/pcsx2/x86/Vif_Dynarec.cpp +++ b/pcsx2/x86/Vif_Dynarec.cpp @@ -6,6 +6,8 @@ #include "common/Perf.h" #include "common/StringUtil.h" +#include + void dVifReset(int idx) { nVif[idx].vifBlocks.reset(); diff --git a/pcsx2/x86/Vif_UnpackSSE.cpp b/pcsx2/x86/Vif_UnpackSSE.cpp index 8641e83f59..09b07350f1 100644 --- a/pcsx2/x86/Vif_UnpackSSE.cpp +++ b/pcsx2/x86/Vif_UnpackSSE.cpp @@ -4,6 +4,8 @@ #include "Vif_UnpackSSE.h" #include "common/Perf.h" +#include + #define xMOV8(regX, loc) xMOVSSZX(regX, loc) #define xMOV16(regX, loc) xMOVSSZX(regX, loc) #define xMOV32(regX, loc) xMOVSSZX(regX, loc)