mirror of https://github.com/stella-emu/stella.git
Remove std::stoi completely; reimplement with std::from_chars and string_view.
This commit is contained in:
parent
200caa8423
commit
5efaebca76
|
@ -27,10 +27,10 @@ BankRomCheat::BankRomCheat(OSystem& os, string_view name, string_view code)
|
||||||
if(myCode.length() == 7)
|
if(myCode.length() == 7)
|
||||||
myCode = "0" + string{code};
|
myCode = "0" + string{code};
|
||||||
|
|
||||||
bank = BSPF::stoi_16(myCode.substr(0, 2));
|
bank = BSPF::stoi<16>(myCode.substr(0, 2));
|
||||||
address = 0xf000 + BSPF::stoi_16(myCode.substr(2, 3));
|
address = 0xf000 + BSPF::stoi<16>(myCode.substr(2, 3));
|
||||||
value = static_cast<uInt8>(BSPF::stoi_16(myCode.substr(5, 2)));
|
value = static_cast<uInt8>(BSPF::stoi<16>(myCode.substr(5, 2)));
|
||||||
count = static_cast<uInt8>(BSPF::stoi_16(myCode.substr(7, 1)) + 1);
|
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
|
// Back up original data; we need this if the cheat is ever disabled
|
||||||
for(int i = 0; i < count; ++i)
|
for(int i = 0; i < count; ++i)
|
||||||
|
|
|
@ -23,9 +23,9 @@
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code)
|
CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code)
|
||||||
: Cheat(os, name, code),
|
: Cheat(os, name, code),
|
||||||
address{static_cast<uInt16>(0xf000 + BSPF::stoi_16(code.substr(0, 3)))},
|
address{static_cast<uInt16>(0xf000 + BSPF::stoi<16>(code.substr(0, 3)))},
|
||||||
value{static_cast<uInt8>(BSPF::stoi_16(code.substr(3, 2)))},
|
value{static_cast<uInt8>(BSPF::stoi<16>(code.substr(3, 2)))},
|
||||||
count{static_cast<uInt8>(BSPF::stoi_16(code.substr(5, 1)) + 1)}
|
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
|
// Back up original data; we need this if the cheat is ever disabled
|
||||||
for(int i = 0; i < count; ++i)
|
for(int i = 0; i < count; ++i)
|
||||||
|
|
|
@ -25,8 +25,8 @@
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
RamCheat::RamCheat(OSystem& os, string_view name, string_view code)
|
RamCheat::RamCheat(OSystem& os, string_view name, string_view code)
|
||||||
: Cheat(os, name, code),
|
: Cheat(os, name, code),
|
||||||
address{static_cast<uInt16>(BSPF::stoi_16(myCode.substr(0, 2)))},
|
address{static_cast<uInt16>(BSPF::stoi<16>(myCode.substr(0, 2)))},
|
||||||
value{static_cast<uInt8>(BSPF::stoi_16(myCode.substr(2, 2)))}
|
value{static_cast<uInt8>(BSPF::stoi<16>(myCode.substr(2, 2)))}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -539,13 +539,10 @@ HSM::ScoreAddresses HighScoresManager::getPropScoreAddr(const json& jprops)
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
uInt16 HighScoresManager::fromHexStr(string_view addr)
|
uInt16 HighScoresManager::fromHexStr(string_view addr)
|
||||||
{
|
{
|
||||||
// TODO: convert away from using string
|
if(const auto pos = addr.find("0x") != std::string::npos)
|
||||||
string naked{addr};
|
addr = addr.substr(pos + 1);
|
||||||
|
|
||||||
if(const auto pos = naked.find("0x") != std::string::npos)
|
return static_cast<uInt16>(BSPF::stoi<16>(addr));
|
||||||
naked = naked.substr(pos + 1);
|
|
||||||
|
|
||||||
return static_cast<uInt16>(BSPF::stoi_16(naked));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
|
@ -48,6 +48,7 @@ using uInt64 = uint64_t;
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <charconv>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
@ -193,29 +194,13 @@ namespace BSPF
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert string to integer, using default value on any error
|
// 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)
|
inline int stoi(string_view s, const int defaultValue = 0)
|
||||||
{
|
{
|
||||||
try { return std::stoi(string{s}); }
|
int i{};
|
||||||
catch(...) { return defaultValue; }
|
s = s.substr(s.find_first_not_of(" "));
|
||||||
}
|
auto result = std::from_chars(s.data(), s.data() + s.size(), i, BASE);
|
||||||
inline int stoi(const string& s, const int defaultValue = 0)
|
return result.ec == std::errc() ? i : defaultValue;
|
||||||
{
|
|
||||||
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; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare two strings (case insensitive)
|
// Compare two strings (case insensitive)
|
||||||
|
|
|
@ -823,7 +823,7 @@ string CartDebug::loadListFile()
|
||||||
if(addr_s.length() == 0)
|
if(addr_s.length() == 0)
|
||||||
continue;
|
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
|
// For now, completely ignore ROM addresses
|
||||||
if(!(addr & 0x1000))
|
if(!(addr & 0x1000))
|
||||||
|
|
|
@ -68,7 +68,6 @@ enum class JoyHatDir {
|
||||||
CENTER = 4
|
CENTER = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO - add bitmask class for 'enum class' and convert this
|
|
||||||
enum JoyHatMask {
|
enum JoyHatMask {
|
||||||
EVENT_HATUP_M = 1<<0,
|
EVENT_HATUP_M = 1<<0,
|
||||||
EVENT_HATDOWN_M = 1<<1,
|
EVENT_HATDOWN_M = 1<<1,
|
||||||
|
|
|
@ -1061,14 +1061,14 @@ void GameInfoDialog::saveHighScoresProperties()
|
||||||
string strAddr;
|
string strAddr;
|
||||||
|
|
||||||
strAddr = myVarAddress->getText();
|
strAddr = myVarAddress->getText();
|
||||||
info.varsAddr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS);
|
info.varsAddr = BSPF::stoi<16>(strAddr, HSM::DEFAULT_ADDRESS);
|
||||||
strAddr = mySpecialAddress->getText();
|
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)
|
for (uInt32 a = 0; a < HSM::MAX_SCORE_ADDR; ++a)
|
||||||
{
|
{
|
||||||
strAddr = myScoreAddress[a]->getText();
|
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();
|
const string strVars = myVariations->getText();
|
||||||
|
@ -1400,7 +1400,7 @@ void GameInfoDialog::updateHighScoresWidgets()
|
||||||
{
|
{
|
||||||
setAddressVal(myScoreAddress[a], myScoreAddressVal[a]);
|
setAddressVal(myScoreAddress[a], myScoreAddressVal[a]);
|
||||||
const string strAddr = myScoreAddress[a]->getText();
|
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
|
else
|
||||||
myScoreAddressVal[a]->setText("");
|
myScoreAddressVal[a]->setText("");
|
||||||
|
@ -1427,7 +1427,7 @@ void GameInfoDialog::setAddressVal(const EditTextWidget* addressWidget, EditText
|
||||||
ostringstream ss;
|
ostringstream ss;
|
||||||
|
|
||||||
// convert to number and read from memory
|
// 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);
|
uInt8 val = instance().highScores().peek(addr);
|
||||||
val = HighScoresManager::convert(val, maxVal, isBCD, zeroBased);
|
val = HighScoresManager::convert(val, maxVal, isBCD, zeroBased);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue