-
-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Vector
and HttpHeaders
iteration
#2745
Conversation
Allows comparison with things other than an object instance, e.g. its name, depending on operator overloads.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Can you make also the suggested name changes?
@@ -367,9 +367,9 @@ void HttpClientConnection::sendRequestHeaders(HttpRequest* request) | |||
request->headers[HTTP_HEADER_TRANSFER_ENCODING] = _F("chunked"); | |||
} | |||
|
|||
for(unsigned i = 0; i < request->headers.count(); i++) { | |||
for(auto hdr : request->headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename hdr
to header
.
HttpHeaderFieldName fieldName = headers.keyAt(i); | ||
auto fieldNameString = headers.toString(fieldName); | ||
operator[](fieldNameString) = headers.valueAt(i); | ||
for(auto hdr : headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -77,8 +77,8 @@ String HttpRequest::toString() const | |||
if(!headers.contains(HTTP_HEADER_HOST)) { | |||
content += headers.toString(HTTP_HEADER_HOST, uri.getHostWithPort()); | |||
} | |||
for(unsigned i = 0; i < headers.count(); i++) { | |||
content += headers[i]; | |||
for(auto hdr : headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -137,8 +137,8 @@ String HttpResponse::toString() const | |||
content += ' '; | |||
content += ::toString(code); | |||
content += " \r\n"; | |||
for(unsigned i = 0; i < headers.count(); i++) { | |||
content += headers[i]; | |||
for(auto hdr : headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -312,8 +312,8 @@ void HttpServerConnection::sendResponseHeaders(HttpResponse* response) | |||
response->headers[HTTP_HEADER_DATE] = DateTime(SystemClock.now(eTZ_UTC)).toHTTPDate(); | |||
} | |||
|
|||
for(unsigned i = 0; i < response->headers.count(); i++) { | |||
sendString(response->headers[i]); | |||
for(auto hdr : response->headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -306,8 +306,8 @@ void SmtpClient::sendMailHeaders(MailMessage* mail) | |||
mail->stream = mStream; | |||
} | |||
|
|||
for(unsigned i = 0; i < mail->headers.count(); i++) { | |||
sendString(mail->headers[i]); | |||
for(auto hdr : mail->headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -40,8 +40,8 @@ int onDownload(HttpConnection& connection, bool success) | |||
Serial << _F(", received ") << stream->available() << _F(" bytes") << endl; | |||
|
|||
auto& headers = connection.getResponse()->headers; | |||
for(unsigned i = 0; i < headers.count(); ++i) { | |||
Serial.print(headers[i]); | |||
for(auto hdr : headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -36,8 +36,8 @@ void CUserData::printMessage(WebsocketConnection& connection, const String& msg) | |||
|
|||
void CUserData::logOut() | |||
{ | |||
for(unsigned i = 0; i < activeWebSockets.count(); i++) { | |||
activeWebSockets[i]->setUserData(nullptr); | |||
for(auto skt : activeWebSockets) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skt
-> socket
@@ -49,8 +49,8 @@ class HttpTest : public TestGroup | |||
{ | |||
#if DEBUG_VERBOSE_LEVEL == DBG | |||
Serial << _F(" count: ") << headers.count() << endl; | |||
for(unsigned i = 0; i < headers.count(); ++i) { | |||
String s = headers[i]; | |||
for(auto hdr : headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hdr
-> header
@@ -154,8 +154,8 @@ class Client | |||
|
|||
host_debug_i("Available commands:"); | |||
|
|||
for(size_t i = 0; i < commands.count(); i++) { | |||
host_debug_i("\t%s => %d", commands.keyAt(i).c_str(), commands.valueAt(i)); | |||
for(auto cmd : commands) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd
-> command
Things like cmd, hdr, skt are used liberally throughout the framework. If these were, say, class member variable names then I'd agree, but not sure it adds anything used in such a restricted context. If you want these changed, we should apply the same change across the entire framework... |
Ok, I agree with you and will put it on my list of things to do. I will merge the PR when the CI/CD finishes. |
This PR improves use of C++ iterator pattern instead of traditional
for(i=0; i<list.count(); ++i)
approach.Template Vector
indexOf
methodsAllows comparison with things other than an object instance, e.g. its name, depending on operator overloads.
Instead of recoding a comparison loop, just implement the appropriate
operator==
for the classes and useVector::indexOf
.Enable iteration for HttpHeaders
This is based on HashMap but stores an enumeration for the key as it's more efficient.
HashMap iteration returns a proxy object, so here we provide a customised version with an implicit
String()
operator.This makes for simpler usage.