From 445bf8d2c69bf176ed1b7899c814fc9354058b0e Mon Sep 17 00:00:00 2001 From: get <45425365+Minty-Meeo@users.noreply.github.com> Date: Sat, 10 Jun 2023 07:56:40 -0500 Subject: [PATCH] Kill AsciiToHex Now superseded by Common::FromChars --- Source/Core/Common/NandPaths.cpp | 8 +++++-- Source/Core/Common/StringUtil.cpp | 19 ----------------- Source/Core/Common/StringUtil.h | 21 +++++++++++++++++-- .../Core/Core/Debugger/PPCDebugInterface.cpp | 20 ++++++++++-------- 4 files changed, 36 insertions(+), 32 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 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(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 #include #include #include @@ -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 >* = 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 >* = 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); diff --git a/Source/Core/Core/Debugger/PPCDebugInterface.cpp b/Source/Core/Core/Debugger/PPCDebugInterface.cpp index 855916545c..bfff81b826 100644 --- a/Source/Core/Core/Debugger/PPCDebugInterface.cpp +++ b/Source/Core/Core/Debugger/PPCDebugInterface.cpp @@ -14,6 +14,7 @@ #include "Common/Align.h" #include "Common/GekkoDisassembler.h" +#include "Common/StringUtil.h" #include "Core/Config/MainSettings.h" #include "Core/Core.h" @@ -451,22 +452,23 @@ PPCDebugInterface::GetMemoryAddressFromInstruction(const std::string& instructio if (!std::regex_search(instruction, match, re)) return std::nullopt; - // Output: match.str(1): negative sign for offset or no match. match.str(2): 0xNNNN, 0, or - // rNN. Check next for 'r' to see if a gpr needs to be loaded. match.str(3): will either be p, - // toc, or NN. Always a gpr. - const std::string offset_match = match.str(2); - const std::string register_match = match.str(3); + // match[1]: negative sign for offset or no match. + // match[2]: 0xNNNN, 0, or rNN. Check next for 'r' to see if a gpr needs to be loaded. + // match[3]: will either be p, toc, or NN. Always a gpr. + const std::string_view offset_match{&*match[2].first, size_t(match[2].length())}; + const std::string_view register_match{&*match[3].first, size_t(match[3].length())}; constexpr char is_reg = 'r'; u32 offset = 0; if (is_reg == offset_match[0]) { - const int register_index = std::stoi(offset_match.substr(1), nullptr, 10); + unsigned register_index; + Common::FromChars(offset_match.substr(1), register_index, 10); offset = (register_index == 0 ? 0 : m_system.GetPPCState().gpr[register_index]); } else { - offset = static_cast(std::stoi(offset_match, nullptr, 16)); + Common::FromChars(offset_match, offset, 16); } // sp and rtoc need to be converted to 1 and 2. @@ -479,11 +481,11 @@ PPCDebugInterface::GetMemoryAddressFromInstruction(const std::string& instructio else if (is_rtoc == register_match[0]) i = 2; else - i = std::stoi(register_match, nullptr, 10); + Common::FromChars(register_match, i, 10); const u32 base_address = m_system.GetPPCState().gpr[i]; - if (!match.str(1).empty()) + if (std::string_view sign{&*match[1].first, size_t(match[1].length())}; !sign.empty()) return base_address - offset; return base_address + offset;