diff --git a/esphome/components/ezo/ezo.cpp b/esphome/components/ezo/ezo.cpp index 3c1b6e33e8..2ee5782ff6 100644 --- a/esphome/components/ezo/ezo.cpp +++ b/esphome/components/ezo/ezo.cpp @@ -79,7 +79,7 @@ void EZOSensor::loop() { if (buf[i] == ',') buf[i] = '\0'; - float val = parse_number((char *) &buf[1], sizeof(buf) - 2).value_or(0); + float val = parse_number((char *) &buf[1]).value_or(0); this->publish_state(val); } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 8b61e6aa38..208c555afd 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -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::value && std::is_unsigned::value), int> = 0> -optional parse_number(const char *str, size_t len) { +optional 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::max()) @@ -382,11 +382,11 @@ optional parse_number(const char *str, size_t len) { /// Parse an unsigned decimal number. template::value && std::is_unsigned::value), int> = 0> optional parse_number(const std::string &str) { - return parse_number(str.c_str(), str.length() + 1); + return parse_number(str.c_str()); } -/// Parse a signed decimal number (requires null-terminated string). +/// Parse a signed decimal number from a null-terminated string. template::value && std::is_signed::value), int> = 0> -optional parse_number(const char *str, size_t len) { +optional 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::min() || value > std::numeric_limits::max()) @@ -396,11 +396,10 @@ optional parse_number(const char *str, size_t len) { /// Parse a signed decimal number. template::value && std::is_signed::value), int> = 0> optional parse_number(const std::string &str) { - return parse_number(str.c_str(), str.length() + 1); + return parse_number(str.c_str()); } -/// Parse a decimal floating-point number (requires null-terminated string). -template::value), int> = 0> -optional parse_number(const char *str, size_t len) { +/// Parse a decimal floating-point number from a null-terminated string. +template::value), int> = 0> optional 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 parse_number(const char *str, size_t len) { /// Parse a decimal floating-point number. template::value), int> = 0> optional parse_number(const std::string &str) { - return parse_number(str.c_str(), str.length() + 1); + return parse_number(str.c_str()); } ///@}