diff options
| author | get <45425365+Minty-Meeo@users.noreply.github.com> | 2023-06-10 07:56:40 -0500 |
|---|---|---|
| committer | get <45425365+Minty-Meeo@users.noreply.github.com> | 2023-06-22 00:06:50 -0500 |
| commit | 445bf8d2c69bf176ed1b7899c814fc9354058b0e (patch) | |
| tree | 5eec7ec4bb8d8a178d64ccb0f795a51777e4ba01 /Source/Core/Common | |
| parent | 5029924ba104fdc5bc78a804fb35cdb5d7418a8f (diff) | |
Kill AsciiToHex
Now superseded by Common::FromChars
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/NandPaths.cpp | 8 | ||||
| -rw-r--r-- | Source/Core/Common/StringUtil.cpp | 19 | ||||
| -rw-r--r-- | Source/Core/Common/StringUtil.h | 21 |
3 files changed, 25 insertions, 23 deletions
diff --git a/Source/Core/Common/NandPaths.cpp b/Source/Core/Common/NandPaths.cpp index a69ccfe9b5..c9217565bb 100644 --- a/Source/Core/Common/NandPaths.cpp +++ b/Source/Core/Common/NandPaths.cpp @@ -90,7 +90,8 @@ bool IsTitlePath(const std::string& path, std::optional<FromWhichRoot> from, u64 } u32 title_id_high, title_id_low; - if (!AsciiToHex(components[0], title_id_high) || !AsciiToHex(components[1], title_id_low)) + if (Common::FromChars(components[0], title_id_high, 16).ec != std::errc{} || + Common::FromChars(components[1], title_id_low, 16).ec != std::errc{}) { return false; } @@ -155,8 +156,11 @@ std::string UnescapeFileName(const std::string& filename) { u32 character; if (pos + 6 <= result.size() && result[pos + 4] == '_' && result[pos + 5] == '_') - if (AsciiToHex(result.substr(pos + 2, 2), character)) + if (Common::FromChars(std::string_view{result}.substr(pos + 2, 2), character, 16).ec == + std::errc{}) + { result.replace(pos, 6, {static_cast<char>(character)}); + } ++pos; } diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index ba07db1206..033bc66407 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -85,25 +85,6 @@ std::string HexDump(const u8* data, size_t size) return out; } -// faster than sscanf -bool AsciiToHex(const std::string& _szValue, u32& result) -{ - // Set errno to a good state. - errno = 0; - - char* endptr = nullptr; - const u32 value = strtoul(_szValue.c_str(), &endptr, 16); - - if (!endptr || *endptr) - return false; - - if (errno == ERANGE) - return false; - - result = value; - return true; -} - bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args) { int writtenCount; diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index 262af614b7..f45f437b87 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -3,6 +3,7 @@ #pragma once +#include <charconv> #include <cstdarg> #include <cstddef> #include <cstdlib> @@ -147,8 +148,24 @@ std::string ValueToString(T value) // Generates an hexdump-like representation of a binary data blob. std::string HexDump(const u8* data, size_t size); -// TODO: kill this -bool AsciiToHex(const std::string& _szValue, u32& result); +namespace Common +{ +template <typename T, typename std::enable_if_t<std::is_integral_v<T>>* = nullptr> +std::from_chars_result FromChars(std::string_view sv, T& value, int base = 10) +{ + const char* const first = sv.data(); + const char* const last = first + sv.size(); + return std::from_chars(first, last, value, base); +} +template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>>* = nullptr> +std::from_chars_result FromChars(std::string_view sv, T& value, + std::chars_format fmt = std::chars_format::general) +{ + const char* const first = sv.data(); + const char* const last = first + sv.size(); + return std::from_chars(first, last, value, fmt); +} +}; // namespace Common std::string TabsToSpaces(int tab_size, std::string str); |
