Skip to content

Add equality operator between string_view and char* #12

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

Closed
wants to merge 1 commit into from
Closed

Add equality operator between string_view and char* #12

wants to merge 1 commit into from

Conversation

nico159
Copy link

@nico159 nico159 commented May 13, 2013

Hi,
boost::string_ref don't have an equality operator between string_view and char*, so I added one

It first compare using std::equal and if it return true then search if we are at the end of the char*

I didn't search first for the possible \0 charter at size() + 1 because possible memory check made by OS/C++ runtime (or it's safe?)

I'm just started learning C++, so I don't know if this commit it's ok

The test suite for uri it's all green

inline
bool operator == (const string_view lhs, const char* rhs) {
return std::equal(lhs.begin(), lhs.end(), rhs) && !rhs[lhs.size() + 1];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really should be using strcmp here -- and I'm not sure if this is also something you want to add to the URI library.

I would suggest you bring this up with Boost.string_ref maintainers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi
I didn't use strcmp because string_ref is not \0 termined. Is it ok to use it in this situation?

I didn't talk this on the boost mailing list because from what I understand string_ref it's a implementation of a future C++ spec proposal, and the string_ref proposal was replaced by string_view, and string_view will not even be part of C++14
So I was thinking "I can't talk this on boost because they must follow a now rejected ISO C++ proposal, and I'm only a student...I'm not sure about discussing of the string_view proposal in the ISO C++ mailing list. All I want is be able to compile netlib tip"

What do you think it's the correct way to fix it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why you need this to make the code compile -- can you show me the sample code that you can't compile?

I would rather you bring this up with Boost (it doesn't matter really if you're still a student, everybody's welcome on the Boost mailing list as long as you don't troll and are agreeable in discussion). :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Can you just change that part of cpp-netlib instead that doesn't build, use string_ref.compare(...) , and send a PR to fix that case? I would suppose you still want to ask the Boost mailing list why string_ref doesn't have equality operator for const char* when it really should have one.

@nico159
Copy link
Author

nico159 commented May 17, 2013

Ok, you have a pull request for cpp-netlib

As written in the commit using compare has some overhead, so I will post on the Boost mailing list for asking the inclusion of an equality operator between string_ref and char*

@nico159
Copy link
Author

nico159 commented May 19, 2013

The next version of Boost will have the string_ref operators that works with both string and const char*
https://groups.google.com/forum/?fromgroups#!topic/boost-developers-archive/ymrEnEPXDzU

@deanberris
Copy link
Member

Sounds like this is something that has been resolved.

@deanberris deanberris closed this May 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants