Skip to content

Conversation

@tniessen
Copy link
Member

@tniessentniessen commented Sep 28, 2018

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), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-botnodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Sep 28, 2018
src/node_url.h Outdated
std::string path() const{
std::string ret;
for (auto i = context_.path.begin(); i != context_.path.end(); i++){
for (constauto& 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

src/node_url.h Outdated
for (constauto& i : context_.path){
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
MemberAuthor

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
for (constauto& i : context_.path){
ret += '/';
ret += *i;
ret += i;
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)

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){
Copy link
Member

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.

@tniessen
Copy link
MemberAuthor

tniessen commented Sep 29, 2018

@addaleax
Copy link
Member

Landed in 1d9ec04

@addaleaxaddaleax closed this Oct 1, 2018
addaleax pushed a commit that referenced this pull request Oct 1, 2018
PR-URL: #23138 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
targos pushed a commit that referenced this pull request Oct 1, 2018
PR-URL: #23138 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
targos pushed a commit that referenced this pull request Oct 3, 2018
PR-URL: #23138 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@targostargos mentioned this pull request Oct 7, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++Issues and PRs that require attention from people who are familiar with C++.whatwg-urlIssues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@tniessen@nodejs-github-bot@addaleax@refack@jasnell@thefourtheye@TimothyGu@targos@cjihrig@joyeecheung