From 3fae9e0274ca0a3ccde9d4eb95f8f6c81d3b7f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 31 Jul 2026 23:12:10 +0800 Subject: [PATCH] Libc (Windows): simplifies code --- src/detection/libc/libc_windows.cpp | 49 +++++++++++------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/detection/libc/libc_windows.cpp b/src/detection/libc/libc_windows.cpp index 20bbeda00..c3d866eb4 100644 --- a/src/detection/libc/libc_windows.cpp +++ b/src/detection/libc/libc_windows.cpp @@ -7,48 +7,37 @@ extern "C" { #endif template -class version_t { - constexpr static auto buflen() noexcept { - unsigned int len = 2; // "." - if (Major == 0) { +struct version_t { + static constexpr uint32_t digits(uint32_t n) noexcept { + uint32_t len = 1; + for (auto temp = n / 10; temp; temp /= 10) { len++; - } else { - for (auto n = Major; n; len++, n /= 10); - } - - if (Minor == 0) { - len++; - } else { - for (auto n = Minor; n; len++, n /= 10); } return len; } - char buf[buflen()] = {}; + char buf[digits(Major) + digits(Minor) + 2]; - public: constexpr version_t() noexcept { - auto ptr = buf + buflen(); + auto ptr = buf + sizeof(buf); *--ptr = '\0'; - if (Minor == 0) { - *--ptr = '0'; - } else { - for (auto n = Minor; n; n /= 10) { - *--ptr = "0123456789"[n % 10]; + auto append = [&](uint32_t n) noexcept { + if (n == 0) { + *--ptr = '0'; + } else { + for (; n; n /= 10) { + *--ptr = static_cast('0' + n % 10); + } } - } + }; + + append(Minor); *--ptr = '.'; - if (Major == 0) { - *--ptr = '0'; - } else { - for (auto n = Major; n; n /= 10) { - *--ptr = "0123456789"[n % 10]; - } - } + append(Major); } - constexpr operator const char*() const { + constexpr operator const char*() const noexcept { return buf; } }; @@ -63,6 +52,6 @@ extern "C" const char* ffDetectLibc(FFLibcResult* result) { result->name = "msvcrt"; #endif - result->version = version<(__MSVCRT_VERSION__ >> 8), (__MSVCRT_VERSION__ & 8)>; + result->version = version<(__MSVCRT_VERSION__ >> 8), (__MSVCRT_VERSION__ & 0xFF)>; return nullptr; }