mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 21:44:55 +01:00
Drop len parameter from parse_number() (#2883)
This commit is contained in:
parent
2253d4bc16
commit
6fe4ff7f85
2 changed files with 10 additions and 11 deletions
|
@ -79,7 +79,7 @@ void EZOSensor::loop() {
|
||||||
if (buf[i] == ',')
|
if (buf[i] == ',')
|
||||||
buf[i] = '\0';
|
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);
|
this->publish_state(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -370,9 +370,9 @@ std::string str_sanitize(const std::string &str);
|
||||||
/// @name Parsing & formatting
|
/// @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>
|
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;
|
char *end = nullptr;
|
||||||
unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
|
unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
|
||||||
if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
|
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.
|
/// Parse an unsigned decimal number.
|
||||||
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
|
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) {
|
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>
|
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;
|
char *end = nullptr;
|
||||||
signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
|
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())
|
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.
|
/// Parse a signed decimal number.
|
||||||
template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
|
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) {
|
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).
|
/// 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>
|
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
|
||||||
optional<T> parse_number(const char *str, size_t len) {
|
|
||||||
char *end = nullptr;
|
char *end = nullptr;
|
||||||
float value = ::strtof(str, &end);
|
float value = ::strtof(str, &end);
|
||||||
if (end == str || *end != '\0' || value == HUGE_VALF)
|
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.
|
/// Parse a decimal floating-point number.
|
||||||
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
|
template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
|
||||||
optional<T> parse_number(const std::string &str) {
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
///@}
|
///@}
|
||||||
|
|
Loading…
Reference in a new issue