Skip to content

Commit

Permalink
Pass String by const reference [3.0]
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 committed Dec 6, 2019
1 parent 8a6a7f7 commit 561b7f4
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 @@ -8,10 +8,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 @@ -20,7 +20,7 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
{
}

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

Expand All @@ -30,14 +30,14 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
return true;
}

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 @@ -46,7 +46,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 @@ -75,7 +75,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 @@ -85,7 +85,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 561b7f4

Please sign in to comment.