From 1c09d2ea4bd0ceb2847d3bc8a169e54a9a2b6bed Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sat, 31 Oct 2020 04:19:02 +0800 Subject: [PATCH] Avoid useless string operations --- nel/include/nel/misc/string_common.h | 7 ++++--- nel/src/gui/interface_element.cpp | 9 ++------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/nel/include/nel/misc/string_common.h b/nel/include/nel/misc/string_common.h index 96f7f7bd1..71ab05a30 100644 --- a/nel/include/nel/misc/string_common.h +++ b/nel/include/nel/misc/string_common.h @@ -254,9 +254,10 @@ inline bool fromString(const std::string &str, double &val) { bool ret = sscanf( inline bool fromString(const std::string &str, wchar_t &val) { return fromString(str, reinterpret_cast(val)); } #endif -// Fast string to bool, reliably defined for strings starting with 0, 1, t, T, f, F, y, Y, n, N, anything else is undefined. -// (str[0] == '1' || (str[0] & 0xD2) == 0x50) -// - Kaetemi +/// 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. +/// - 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); diff --git a/nel/src/gui/interface_element.cpp b/nel/src/gui/interface_element.cpp index cf5646077..bc5da4e03 100644 --- a/nel/src/gui/interface_element.cpp +++ b/nel/src/gui/interface_element.cpp @@ -1028,21 +1028,16 @@ namespace NLGUI } // ------------------------------------------------------------------------------------------------ - bool CInterfaceElement::convertBool (const char *ptr) + bool CInterfaceElement::convertBool (const char *ptr) { - std::string str = toLower(ptr); - bool b = false; - fromString( str, b ); - return b; + return NLMISC::toBool(ptr); } // ------------------------------------------------------------------------------------------------ NLMISC::CVector CInterfaceElement::convertVector (const char *ptr) { float x = 0.0f, y = 0.0f, z = 0.0f; - sscanf (ptr, "%f %f %f", &x, &y, &z); - return CVector(x,y,z); }