Eliminate double lookups in parse_string_param_ and date/time/datetime handlers

- parse_string_param_: use arg() and check empty instead of hasArg+arg
- date/time/datetime: inline arg() call to avoid redundant hasArg+parse_string_param_ triple lookup
This commit is contained in:
J. Nick Koston
2026-02-11 18:08:23 -06:00
parent c2bb55ff5d
commit 920f84fa1d
2 changed files with 15 additions and 12 deletions
+12 -9
View File
@@ -1173,12 +1173,13 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat
auto call = obj->make_call();
if (!request->hasArg(ESPHOME_F("value"))) {
// .c_str() is required for Arduino framework where arg() returns Arduino String instead of std::string
std::string value = request->arg(ESPHOME_F("value")).c_str(); // NOLINT(readability-redundant-string-cstr)
if (value.empty()) {
request->send(409);
return;
}
parse_string_param_(request, ESPHOME_F("value"), call, &decltype(call)::set_date);
call.set_date(value);
DEFER_ACTION(call, call.perform());
request->send(200);
@@ -1233,12 +1234,13 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat
auto call = obj->make_call();
if (!request->hasArg(ESPHOME_F("value"))) {
// .c_str() is required for Arduino framework where arg() returns Arduino String instead of std::string
std::string value = request->arg(ESPHOME_F("value")).c_str(); // NOLINT(readability-redundant-string-cstr)
if (value.empty()) {
request->send(409);
return;
}
parse_string_param_(request, ESPHOME_F("value"), call, &decltype(call)::set_time);
call.set_time(value);
DEFER_ACTION(call, call.perform());
request->send(200);
@@ -1292,12 +1294,13 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur
auto call = obj->make_call();
if (!request->hasArg(ESPHOME_F("value"))) {
// .c_str() is required for Arduino framework where arg() returns Arduino String instead of std::string
std::string value = request->arg(ESPHOME_F("value")).c_str(); // NOLINT(readability-redundant-string-cstr)
if (value.empty()) {
request->send(409);
return;
}
parse_string_param_(request, ESPHOME_F("value"), call, &decltype(call)::set_datetime);
call.set_datetime(value);
DEFER_ACTION(call, call.perform());
request->send(200);
+3 -3
View File
@@ -543,9 +543,9 @@ class WebServer : public Controller,
template<typename T, typename Ret>
void parse_string_param_(AsyncWebServerRequest *request, ParamNameType param_name, T &call,
Ret (T::*setter)(const std::string &)) {
if (request->hasArg(param_name)) {
// .c_str() is required for Arduino framework where arg() returns Arduino String instead of std::string
std::string value = request->arg(param_name).c_str(); // NOLINT(readability-redundant-string-cstr)
// .c_str() is required for Arduino framework where arg() returns Arduino String instead of std::string
std::string value = request->arg(param_name).c_str(); // NOLINT(readability-redundant-string-cstr)
if (!value.empty()) {
(call.*setter)(value);
}
}