From 28a499c95a51c648d2d0355ae45aa8dafed2c7a0 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 1 May 2019 03:20:21 +0800 Subject: [PATCH] Fix bool parsing for non-lowercase values --- code/nel/include/nel/misc/string_common.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/code/nel/include/nel/misc/string_common.h b/code/nel/include/nel/misc/string_common.h index 2839c2bfc..02cafec92 100644 --- a/code/nel/include/nel/misc/string_common.h +++ b/code/nel/include/nel/misc/string_common.h @@ -242,6 +242,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, 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. +// (str[0] == '1' || (str[0] & 0xD2) == 0x50) +// - Kaetemi + inline bool fromString(const std::string &str, bool &val) { if (str.length() == 1) @@ -273,11 +277,12 @@ inline bool fromString(const std::string &str, bool &val) } else { - if (str == "true" || str == "yes") + std::string strl = toLower(str); + if (strl == "true" || strl == "yes") { val = true; } - else if (str == "false" || str == "no") + else if (strl == "false" || strl == "no") { val = false; }