Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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?

Copy link
Contributor

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

ret += '/';
ret += *i;
ret += i;
Copy link
Contributor

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

Copy link
Contributor

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; });

Copy link
Member

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)

Copy link
Member Author

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.

}
return ret;
}
Expand Down