mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
handle conversion failure
This commit is contained in:
@@ -194,15 +194,17 @@ namespace internal {
|
|||||||
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
|
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, F conv) {
|
||||||
char *end;
|
char *end;
|
||||||
R result = conv(str.c_str(), &end);
|
R result = conv(str.c_str(), &end);
|
||||||
|
// Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
|
||||||
if (pos)
|
if (pos)
|
||||||
*pos = static_cast<size_t>(end - str.c_str());
|
*pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) {
|
template<typename R, typename F> inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) {
|
||||||
char *end;
|
char *end;
|
||||||
R result = conv(str.c_str(), &end, base);
|
R result = conv(str.c_str(), &end, base);
|
||||||
|
// Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number
|
||||||
if (pos)
|
if (pos)
|
||||||
*pos = static_cast<size_t>(end - str.c_str());
|
*pos = (end == str.c_str()) ? 0 : static_cast<size_t>(end - str.c_str());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
// NOLINTEND(google-runtime-int)
|
// NOLINTEND(google-runtime-int)
|
||||||
|
|||||||
Reference in New Issue
Block a user