diff --git a/include/LightGBM/utils/common.h b/include/LightGBM/utils/common.h index dd71782fb1ae..43573573d3eb 100644 --- a/include/LightGBM/utils/common.h +++ b/include/LightGBM/utils/common.h @@ -1082,12 +1082,20 @@ struct __StringToTHelper { // Fast (common) path: For numeric inputs in RFC 7159 format: const bool fast_parse_succeeded = fast_double_parser::parse_number(str.c_str(), &tmp); - // Rare path: Not in RFC 7159 format. Possible "inf", "nan", etc. Fallback to standard library: + // Rare path: Not in RFC 7159 format. Possible "inf", "nan", etc. if (!fast_parse_succeeded) { - std::stringstream ss; - Common::C_stringstream(ss); - ss << str; - ss >> tmp; + std::string strlower(str); + std::transform(strlower.begin(), strlower.end(), strlower.begin(), [](int c) -> char { return static_cast(::tolower(c)); }); + if (strlower == std::string("inf")) + tmp = std::numeric_limits::infinity(); + else if (strlower == std::string("-inf")) + tmp = -std::numeric_limits::infinity(); + else if (strlower == std::string("nan")) + tmp = std::numeric_limits::quiet_NaN(); + else if (strlower == std::string("-nan")) + tmp = -std::numeric_limits::quiet_NaN(); + else + Log::Fatal("Failed to parse double: %s", str.c_str()); } return static_cast(tmp);