From 5585b5967ed4f77ceb6aca40babb2f13f358178c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 11 Feb 2026 18:11:49 -0600 Subject: [PATCH] Avoid std::string copy in date/time/datetime handlers Use const auto& to bind directly to arg() result (std::string on IDF, Arduino String on Arduino) and pass c_str()/length() to the setter. No intermediate std::string copy needed. --- esphome/components/web_server/web_server.cpp | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index b83c11f2600..d8fe9b8c7ec 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -1173,13 +1173,13 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat auto call = obj->make_call(); - // .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()) { + const auto &value = request->arg(ESPHOME_F("value")); + // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility + if (value.length() == 0) { // NOLINT(readability-container-size-empty) request->send(409); return; } - call.set_date(value.c_str(), value.size()); + call.set_date(value.c_str(), value.length()); DEFER_ACTION(call, call.perform()); request->send(200); @@ -1234,13 +1234,13 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat auto call = obj->make_call(); - // .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()) { + const auto &value = request->arg(ESPHOME_F("value")); + // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility + if (value.length() == 0) { // NOLINT(readability-container-size-empty) request->send(409); return; } - call.set_time(value.c_str(), value.size()); + call.set_time(value.c_str(), value.length()); DEFER_ACTION(call, call.perform()); request->send(200); @@ -1294,13 +1294,13 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur auto call = obj->make_call(); - // .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()) { + const auto &value = request->arg(ESPHOME_F("value")); + // Arduino String has isEmpty() not empty(), use length() for cross-platform compatibility + if (value.length() == 0) { // NOLINT(readability-container-size-empty) request->send(409); return; } - call.set_datetime(value.c_str(), value.size()); + call.set_datetime(value.c_str(), value.length()); DEFER_ACTION(call, call.perform()); request->send(200);