Remove std::stoi completely; reimplement with std::from_chars and string_view.

This commit is contained in:
Stephen Anthony 2022-12-28 20:57:31 -03:30
parent e150a3dd58
commit 204aafa927
8 changed files with 24 additions and 43 deletions

View File

@ -27,10 +27,10 @@ BankRomCheat::BankRomCheat(OSystem& os, string_view name, string_view code)
if(myCode.length() == 7)
myCode = "0" + string{code};
bank = BSPF::stoi_16(myCode.substr(0, 2));
address = 0xf000 + BSPF::stoi_16(myCode.substr(2, 3));
value = static_cast<uInt8>(BSPF::stoi_16(myCode.substr(5, 2)));
count = static_cast<uInt8>(BSPF::stoi_16(myCode.substr(7, 1)) + 1);
bank = BSPF::stoi<16>(myCode.substr(0, 2));
address = 0xf000 + BSPF::stoi<16>(myCode.substr(2, 3));
value = static_cast<uInt8>(BSPF::stoi<16>(myCode.substr(5, 2)));
count = static_cast<uInt8>(BSPF::stoi<16>(myCode.substr(7, 1)) + 1);
// Back up original data; we need this if the cheat is ever disabled
for(int i = 0; i < count; ++i)

View File

@ -23,9 +23,9 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code)
: Cheat(os, name, code),
address{static_cast<uInt16>(0xf000 + BSPF::stoi_16(code.substr(0, 3)))},
value{static_cast<uInt8>(BSPF::stoi_16(code.substr(3, 2)))},
count{static_cast<uInt8>(BSPF::stoi_16(code.substr(5, 1)) + 1)}
address{static_cast<uInt16>(0xf000 + BSPF::stoi<16>(code.substr(0, 3)))},
value{static_cast<uInt8>(BSPF::stoi<16>(code.substr(3, 2)))},
count{static_cast<uInt8>(BSPF::stoi<16>(code.substr(5, 1)) + 1)}
{
// Back up original data; we need this if the cheat is ever disabled
for(int i = 0; i < count; ++i)

View File

@ -25,8 +25,8 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RamCheat::RamCheat(OSystem& os, string_view name, string_view code)
: Cheat(os, name, code),
address{static_cast<uInt16>(BSPF::stoi_16(myCode.substr(0, 2)))},
value{static_cast<uInt8>(BSPF::stoi_16(myCode.substr(2, 2)))}
address{static_cast<uInt16>(BSPF::stoi<16>(myCode.substr(0, 2)))},
value{static_cast<uInt8>(BSPF::stoi<16>(myCode.substr(2, 2)))}
{
}

View File

@ -539,13 +539,10 @@ HSM::ScoreAddresses HighScoresManager::getPropScoreAddr(const json& jprops)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uInt16 HighScoresManager::fromHexStr(string_view addr)
{
// TODO: convert away from using string
string naked{addr};
if(const auto pos = addr.find("0x") != std::string::npos)
addr = addr.substr(pos + 1);
if(const auto pos = naked.find("0x") != std::string::npos)
naked = naked.substr(pos + 1);
return static_cast<uInt16>(BSPF::stoi_16(naked));
return static_cast<uInt16>(BSPF::stoi<16>(addr));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -48,6 +48,7 @@ using uInt64 = uint64_t;
#include <memory>
#include <string>
#include <string_view>
#include <charconv>
#include <sstream>
#include <cstring>
#include <cctype>
@ -193,29 +194,13 @@ namespace BSPF
}
// Convert string to integer, using default value on any error
// TODO: reimplement stoi so only 'string_view' version is needed
template<int BASE = 10>
inline int stoi(string_view s, const int defaultValue = 0)
{
try { return std::stoi(string{s}); }
catch(...) { return defaultValue; }
}
inline int stoi(const string& s, const int defaultValue = 0)
{
try { return std::stoi(s); }
catch(...) { return defaultValue; }
}
// Convert string with base 16 to integer, using default value on any error
// TODO: reimplement stoi so only 'string_view' version is needed
inline int stoi_16(string_view s, const int defaultValue = 0)
{
try { return std::stoi(string{s}, nullptr, 16); }
catch(...) { return defaultValue; }
}
inline int stoi_16(const string& s, const int defaultValue = 0)
{
try { return std::stoi(s, nullptr, 16); }
catch(...) { return defaultValue; }
int i{};
s = s.substr(s.find_first_not_of(" "));
auto result = std::from_chars(s.data(), s.data() + s.size(), i, BASE);
return result.ec == std::errc() ? i : defaultValue;
}
// Compare two strings (case insensitive)

View File

@ -823,7 +823,7 @@ string CartDebug::loadListFile()
if(addr_s.length() == 0)
continue;
addr = BSPF::stoi_16(addr_s[0] == 'U' ? addr_s.substr(1) : addr_s);
addr = BSPF::stoi<16>(addr_s[0] == 'U' ? addr_s.substr(1) : addr_s);
// For now, completely ignore ROM addresses
if(!(addr & 0x1000))

View File

@ -68,7 +68,6 @@ enum class JoyHatDir {
CENTER = 4
};
// TODO - add bitmask class for 'enum class' and convert this
enum JoyHatMask {
EVENT_HATUP_M = 1<<0,
EVENT_HATDOWN_M = 1<<1,

View File

@ -1061,14 +1061,14 @@ void GameInfoDialog::saveHighScoresProperties()
string strAddr;
strAddr = myVarAddress->getText();
info.varsAddr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
info.varsAddr = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
strAddr = mySpecialAddress->getText();
info.specialAddr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
info.specialAddr = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
for (uInt32 a = 0; a < HSM::MAX_SCORE_ADDR; ++a)
{
strAddr = myScoreAddress[a]->getText();
info.scoreAddr[a] = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
info.scoreAddr[a] = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
}
const string strVars = myVariations->getText();
@ -1400,7 +1400,7 @@ void GameInfoDialog::updateHighScoresWidgets()
{
setAddressVal(myScoreAddress[a], myScoreAddressVal[a]);
const string strAddr = myScoreAddress[a]->getText();
scoreAddr[a] = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
scoreAddr[a] = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
}
else
myScoreAddressVal[a]->setText("");
@ -1427,7 +1427,7 @@ void GameInfoDialog::setAddressVal(const EditTextWidget* addressWidget, EditText
ostringstream ss;
// convert to number and read from memory
const uInt16 addr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
const uInt16 addr = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
uInt8 val = instance().highScores().peek(addr);
val = HighScoresManager::convert(val, maxVal, isBCD, zeroBased);