From da501b9294f5b338ba74fc6d196108c084891b74 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Fri, 29 Nov 2024 15:04:25 +1000 Subject: [PATCH] StringUtil: Add ParseFixedHexString() --- src/common/string_util.h | 21 +++++++++++++++++++++ src/core/bios.cpp | 16 +--------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index cd259f504..e5b821840 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -260,6 +260,27 @@ ALWAYS_INLINE static bool IsHexDigit(T ch) (ch >= static_cast('0') && ch <= static_cast('9'))); } +/// Returns a byte array from the provided hex string, computed at compile-time. +template +static constexpr std::array ParseFixedHexString(const char str[]) +{ + std::array h{}; + for (int i = 0; str[i] != '\0'; i++) + { + u8 nibble = 0; + char ch = str[i]; + if (ch >= '0' && ch <= '9') + nibble = str[i] - '0'; + else if (ch >= 'a' && ch <= 'z') + nibble = 0xA + (str[i] - 'a'); + else if (ch >= 'A' && ch <= 'Z') + nibble = 0xA + (str[i] - 'A'); + + h[i / 2] |= nibble << (((i & 1) ^ 1) * 4); + } + return h; +} + /// StartsWith/EndsWith variants which aren't case sensitive. ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view str, const std::string_view prefix) { diff --git a/src/core/bios.cpp b/src/core/bios.cpp index 767b5f53d..c8b0c7eab 100644 --- a/src/core/bios.cpp +++ b/src/core/bios.cpp @@ -21,21 +21,7 @@ namespace BIOS { static constexpr ImageInfo::Hash MakeHashFromString(const char str[]) { - ImageInfo::Hash h{}; - for (int i = 0; str[i] != '\0'; i++) - { - u8 nibble = 0; - char ch = str[i]; - if (ch >= '0' && ch <= '9') - nibble = str[i] - '0'; - else if (ch >= 'a' && ch <= 'z') - nibble = 0xA + (str[i] - 'a'); - else if (ch >= 'A' && ch <= 'Z') - nibble = 0xA + (str[i] - 'A'); - - h[i / 2] |= nibble << (((i & 1) ^ 1) * 4); - } - return h; + return StringUtil::ParseFixedHexString(str); } // clang-format off