-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
url: use foreach-style C++ loop #23138
Conversation
src/node_url.h
Outdated
@@ -159,9 +159,9 @@ class URL { | |||
|
|||
std::string path() const { | |||
std::string ret; | |||
for (auto i = context_.path.begin(); i != context_.path.end(); i++) { | |||
for (const auto& i : context_.path) { |
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.
Can we replace auto
with std::string
?
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.
I would ask not to.
As per the discussion in #23028
src/node_url.h
Outdated
ret += '/'; | ||
ret += *i; | ||
ret += i; |
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.
There are two possible minor improvements.
One is to use stringstream
The other is to hint to the compiler the the /
always goes with the next part:
ret += '/' + i;
YMMV
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.
Theoretically this should be the best solution, but I'm not sure I like it:
return std::accumulate(cbegin(_path), cend(_path), std::string(),
[](auto const& a, auto const& b) { return a + '/' + b; });
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.
I’d prefer std::accumulate
over stringstream
but otherwise the suggestions in these two comments seem fine (as does the current state of the PR)
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.
The idea behind using a range-based for loop was to improve readability, and using accumulate
for such a trivial thing seems like doing the opposite.
src/node_url.h
Outdated
ret += '/'; | ||
ret += *i; | ||
ret += i; |
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.
I’d prefer std::accumulate
over stringstream
but otherwise the suggestions in these two comments seem fine (as does the current state of the PR)
bcad6b4
to
b3f40ee
Compare
b3f40ee
to
2440d9c
Compare
src/node_url.h
Outdated
for (auto i = context_.path.begin(); i != context_.path.end(); i++) { | ||
ret += '/'; | ||
ret += *i; | ||
for (const std::string& i : context_.path) { |
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.
Maybe rename it to something like path_element
?
To me, i
means "index" and that's how it's always used in JS.
Landed in 1d9ec04 |
PR-URL: #23138 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
PR-URL: #23138 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
PR-URL: #23138 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Change
URL::parse
to use a range-based for loop for better readability.CI on commit before opening PR: https://ci.nodejs.org/job/node-test-commit/21840/
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes