Avoid useless string operations

feature/bonusmalus-timers
kaetemi 4 years ago committed by Nimetu
parent f83d8a206e
commit dba92deea5

@ -246,9 +246,10 @@ inline bool fromString(const std::string &str, sint64 &val) { bool ret = sscanf(
inline bool fromString(const std::string &str, float &val) { bool ret = sscanf(str.c_str(), "%f", &val) == 1; if (!ret) val = 0.0f; return ret; } inline bool fromString(const std::string &str, float &val) { bool ret = sscanf(str.c_str(), "%f", &val) == 1; if (!ret) val = 0.0f; return ret; }
inline bool fromString(const std::string &str, double &val) { bool ret = sscanf(str.c_str(), "%lf", &val) == 1; if (!ret) val = 0.0; return ret; } inline bool fromString(const std::string &str, double &val) { bool ret = sscanf(str.c_str(), "%lf", &val) == 1; if (!ret) val = 0.0; return ret; }
// Fast string to bool, reliably defined for strings starting with 0, 1, t, T, f, F, y, Y, n, N, anything else is undefined. /// Fast string to bool, reliably defined for strings starting with 0, 1, t, T, f, F, y, Y, n, N, and empty strings, anything else is undefined.
// (str[0] == '1' || (str[0] & 0xD2) == 0x50) /// - Kaetemi
// - Kaetemi inline bool toBool(const char *str) { return str[0] == '1' || (str[0] & 0xD2) == 0x50; }
inline bool toBool(const std::string &str) { return toBool(str.c_str()); } // Safe because first byte may be null
bool fromString(const std::string &str, bool &val); bool fromString(const std::string &str, bool &val);

@ -1030,19 +1030,14 @@ namespace NLGUI
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool CInterfaceElement::convertBool (const char *ptr) bool CInterfaceElement::convertBool (const char *ptr)
{ {
std::string str = toLower(ptr); return NLMISC::toBool(ptr);
bool b = false;
fromString( str, b );
return b;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
NLMISC::CVector CInterfaceElement::convertVector (const char *ptr) NLMISC::CVector CInterfaceElement::convertVector (const char *ptr)
{ {
float x = 0.0f, y = 0.0f, z = 0.0f; float x = 0.0f, y = 0.0f, z = 0.0f;
sscanf (ptr, "%f %f %f", &x, &y, &z); sscanf (ptr, "%f %f %f", &x, &y, &z);
return CVector(x,y,z); return CVector(x,y,z);
} }

Loading…
Cancel
Save