diff --git a/src/cheat/BankRomCheat.cxx b/src/cheat/BankRomCheat.cxx index 8fd6d95dc..6047e1262 100644 --- a/src/cheat/BankRomCheat.cxx +++ b/src/cheat/BankRomCheat.cxx @@ -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(BSPF::stoi_16(myCode.substr(5, 2))); - count = static_cast(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(BSPF::stoi<16>(myCode.substr(5, 2))); + count = static_cast(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) diff --git a/src/cheat/CheetahCheat.cxx b/src/cheat/CheetahCheat.cxx index 59f302c73..5bcf8d92a 100644 --- a/src/cheat/CheetahCheat.cxx +++ b/src/cheat/CheetahCheat.cxx @@ -23,9 +23,9 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code) : Cheat(os, name, code), - address{static_cast(0xf000 + BSPF::stoi_16(code.substr(0, 3)))}, - value{static_cast(BSPF::stoi_16(code.substr(3, 2)))}, - count{static_cast(BSPF::stoi_16(code.substr(5, 1)) + 1)} + address{static_cast(0xf000 + BSPF::stoi<16>(code.substr(0, 3)))}, + value{static_cast(BSPF::stoi<16>(code.substr(3, 2)))}, + count{static_cast(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) diff --git a/src/cheat/RamCheat.cxx b/src/cheat/RamCheat.cxx index a39b0995e..0ec4143b8 100644 --- a/src/cheat/RamCheat.cxx +++ b/src/cheat/RamCheat.cxx @@ -25,8 +25,8 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - RamCheat::RamCheat(OSystem& os, string_view name, string_view code) : Cheat(os, name, code), - address{static_cast(BSPF::stoi_16(myCode.substr(0, 2)))}, - value{static_cast(BSPF::stoi_16(myCode.substr(2, 2)))} + address{static_cast(BSPF::stoi<16>(myCode.substr(0, 2)))}, + value{static_cast(BSPF::stoi<16>(myCode.substr(2, 2)))} { } diff --git a/src/common/HighScoresManager.cxx b/src/common/HighScoresManager.cxx index 66c1a5476..a905e51c7 100644 --- a/src/common/HighScoresManager.cxx +++ b/src/common/HighScoresManager.cxx @@ -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(BSPF::stoi_16(naked)); + return static_cast(BSPF::stoi<16>(addr)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/bspf.hxx b/src/common/bspf.hxx index 19aa7cf47..75a18e85f 100644 --- a/src/common/bspf.hxx +++ b/src/common/bspf.hxx @@ -48,6 +48,7 @@ using uInt64 = uint64_t; #include #include #include +#include #include #include #include @@ -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 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) diff --git a/src/debugger/CartDebug.cxx b/src/debugger/CartDebug.cxx index 0583dbca3..3d2a03afd 100644 --- a/src/debugger/CartDebug.cxx +++ b/src/debugger/CartDebug.cxx @@ -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)) diff --git a/src/emucore/EventHandlerConstants.hxx b/src/emucore/EventHandlerConstants.hxx index 8ed0801cf..1945bbf79 100644 --- a/src/emucore/EventHandlerConstants.hxx +++ b/src/emucore/EventHandlerConstants.hxx @@ -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, diff --git a/src/gui/GameInfoDialog.cxx b/src/gui/GameInfoDialog.cxx index 3536153d2..087b4d7a4 100644 --- a/src/gui/GameInfoDialog.cxx +++ b/src/gui/GameInfoDialog.cxx @@ -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);