Skip to content
Permalink
Browse files
Merge pull request #8826 from iwubcode/try_parse_any_base
Common / Core: Update StringUtil to allow specifying the base
  • Loading branch information
leoetlino committed May 24, 2020
2 parents 7449c71 + 85e11cd commit 166633b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
@@ -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);

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;

@@ -67,9 +67,9 @@ bool TryParse(const std::string& str, T* output)
ReadType value;

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
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.
if (end_ptr == nullptr || *end_ptr != '\0')
@@ -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)
{
AREntry op;
bool success_addr = TryParse(std::string("0x") + pieces[0], &op.cmd_addr);
bool success_val = TryParse(std::string("0x") + pieces[1], &op.value);
bool success_addr = TryParse(pieces[0], &op.cmd_addr, 16);
bool success_val = TryParse(pieces[1], &op.value, 16);

if (success_addr && success_val)
{

0 comments on commit 166633b

Please sign in to comment.