Skip to content

Commit

Permalink
Pass String by const reference [3.0] (#6583)
Browse files Browse the repository at this point in the history
Passing String by value means a full copy-constructor/temporary
string creation which is slightly inefficient. Avoid that
by using const references.
  • Loading branch information
dirkmueller authored Jul 10, 2020
1 parent df9bc38 commit 83158af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions libraries/ESP8266WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class RequestHandler {
using WebServerType = ESP8266WebServerTemplate<ServerType>;
public:
virtual ~RequestHandler() { }
virtual bool canHandle(HTTPMethod method, String uri) { (void) method; (void) uri; return false; }
virtual bool canUpload(String uri) { (void) uri; return false; }
virtual bool handle(WebServerType& server, HTTPMethod requestMethod, String requestUri) { (void) server; (void) requestMethod; (void) requestUri; return false; }
virtual void upload(WebServerType& server, String requestUri, HTTPUpload& upload) { (void) server; (void) requestUri; (void) upload; }
virtual bool canHandle(HTTPMethod method, const String& uri) { (void) method; (void) uri; return false; }
virtual bool canUpload(const String& uri) { (void) uri; return false; }
virtual bool handle(WebServerType& server, HTTPMethod requestMethod, const String& requestUri) { (void) server; (void) requestMethod; (void) requestUri; return false; }
virtual void upload(WebServerType& server, const String& requestUri, HTTPUpload& upload) { (void) server; (void) requestUri; (void) upload; }

RequestHandler<ServerType>* next() { return _next; }
void next(RequestHandler<ServerType>* r) { _next = r; }
Expand Down
14 changes: 8 additions & 6 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
delete _uri;
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
bool canHandle(HTTPMethod requestMethod, const String& requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod)
return false;

return _uri->canHandle(requestUri, RequestHandler<ServerType>::pathArgs);
}

bool canUpload(String requestUri) override {
bool canUpload(const String& requestUri) override {
if (!_ufn || !canHandle(HTTP_POST, requestUri))
return false;

return true;
}

bool handle(WebServerType& server, HTTPMethod requestMethod, String requestUri) override {
bool handle(WebServerType& server, HTTPMethod requestMethod, const String& requestUri) override {
(void) server;
if (!canHandle(requestMethod, requestUri))
return false;
Expand All @@ -48,7 +48,7 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
return true;
}

void upload(WebServerType& server, String requestUri, HTTPUpload& upload) override {
void upload(WebServerType& server, const String& requestUri, HTTPUpload& upload) override {
(void) server;
(void) upload;
if (canUpload(requestUri))
Expand Down Expand Up @@ -85,7 +85,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
_baseUriLength = _uri.length();
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
bool canHandle(HTTPMethod requestMethod, const String& requestUri) override {
if ((requestMethod != HTTP_GET) && (requestMethod != HTTP_HEAD))
return false;

Expand All @@ -95,7 +95,9 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
return true;
}

bool handle(WebServerType& server, HTTPMethod requestMethod, String requestUri) override {
bool handle(WebServerType& server, HTTPMethod requestMethod, const String& __requestUri) override {
String requestUri(__requestUri);

if (!canHandle(requestMethod, requestUri))
return false;

Expand Down

0 comments on commit 83158af

Please sign in to comment.