Merge pull request #8826 from iwubcode/try_parse_any_base

Common / Core: Update StringUtil to allow specifying the base
This commit is contained in:
Léo Lam 2020-05-24 15:13:49 +02:00 committed by GitHub
commit 166633bf27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -55,7 +55,7 @@ std::string ReplaceAll(std::string result, std::string_view src, std::string_vie
bool TryParse(const std::string& str, bool* output); bool TryParse(const std::string& str, bool* output);
template <typename T, std::enable_if_t<std::is_integral_v<T> || std::is_enum_v<T>>* = nullptr> template <typename T, std::enable_if_t<std::is_integral_v<T> || std::is_enum_v<T>>* = nullptr>
bool TryParse(const std::string& str, T* output) bool TryParse(const std::string& str, T* output, int base = 0)
{ {
char* end_ptr = nullptr; char* end_ptr = nullptr;
@ -67,9 +67,9 @@ bool TryParse(const std::string& str, T* output)
ReadType value; ReadType value;
if constexpr (std::is_unsigned_v<T>) if constexpr (std::is_unsigned_v<T>)
value = std::strtoull(str.c_str(), &end_ptr, 0); value = std::strtoull(str.c_str(), &end_ptr, base);
else else
value = std::strtoll(str.c_str(), &end_ptr, 0); value = std::strtoll(str.c_str(), &end_ptr, base);
// Fail if the end of the string wasn't reached. // Fail if the end of the string wasn't reached.
if (end_ptr == nullptr || *end_ptr != '\0') if (end_ptr == nullptr || *end_ptr != '\0')

View File

@ -236,8 +236,8 @@ std::vector<ARCode> LoadCodes(const IniFile& global_ini, const IniFile& local_in
if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8) if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8)
{ {
AREntry op; AREntry op;
bool success_addr = TryParse(std::string("0x") + pieces[0], &op.cmd_addr); bool success_addr = TryParse(pieces[0], &op.cmd_addr, 16);
bool success_val = TryParse(std::string("0x") + pieces[1], &op.value); bool success_val = TryParse(pieces[1], &op.value, 16);
if (success_addr && success_val) if (success_addr && success_val)
{ {