Drop len parameter from parse_number() (#2883)

This commit is contained in:
Oxan van Leeuwen 2021-12-07 20:46:25 +01:00 committed by GitHub
parent 2253d4bc16
commit 6fe4ff7f85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 11 deletions

View File

@ -79,7 +79,7 @@ void EZOSensor::loop() {
if (buf[i] == ',')
buf[i] = '\0';
float val = parse_number<float>((char *) &buf[1], sizeof(buf) - 2).value_or(0);
float val = parse_number<float>((char *) &buf[1]).value_or(0);
this->publish_state(val);
}

View File

@ -370,9 +370,9 @@ std::string str_sanitize(const std::string &str);
/// @name Parsing & formatting
///@{
/// Parse an unsigned decimal number (requires null-terminated string).
/// Parse an unsigned decimal number from a null-terminated string.
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
optional<T> parse_number(const char *str, size_t len) {
optional<T> parse_number(const char *str) {
char *end = nullptr;
unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
@ -382,11 +382,11 @@ optional<T> parse_number(const char *str, size_t len) {
/// Parse an unsigned decimal number.
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
optional<T> parse_number(const std::string &str) {
return parse_number<T>(str.c_str(), str.length() + 1);
return parse_number<T>(str.c_str());
}
/// Parse a signed decimal number (requires null-terminated string).
/// Parse a signed decimal number from a null-terminated string.
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
optional<T> parse_number(const char *str, size_t len) {
optional<T> parse_number(const char *str) {
char *end = nullptr;
signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
if (end == str || *end != '\0' || value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max())
@ -396,11 +396,10 @@ optional<T> parse_number(const char *str, size_t len) {
/// Parse a signed decimal number.
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
optional<T> parse_number(const std::string &str) {
return parse_number<T>(str.c_str(), str.length() + 1);
return parse_number<T>(str.c_str());
}
/// Parse a decimal floating-point number (requires null-terminated string).
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
optional<T> parse_number(const char *str, size_t len) {
/// Parse a decimal floating-point number from a null-terminated string.
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
char *end = nullptr;
float value = ::strtof(str, &end);
if (end == str || *end != '\0' || value == HUGE_VALF)
@ -410,7 +409,7 @@ optional<T> parse_number(const char *str, size_t len) {
/// Parse a decimal floating-point number.
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
optional<T> parse_number(const std::string &str) {
return parse_number<T>(str.c_str(), str.length() + 1);
return parse_number<T>(str.c_str());
}
///@}