8000 c_NPOS is not -1, differs from `std::string::npos` · Issue #103 · stclib/STC · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

c_NPOS is not -1, differs from std::string::npos #103

Closed
liigo opened this issue Oct 15, 2024 · 2 comments
Closed

c_NPOS is not -1, differs from std::string::npos #103

liigo opened this issue Oct 15, 2024 · 2 comments

Comments

@liigo
Copy link
Contributor
liigo commented Oct 15, 2024

in C++, the std::string::npos is -1, so someone may write:

string s = "test";
int i = s.find('e');
if (i >= 0) // same as `i != std::string::npos`
    cout << "found it at " << i << "\n";

but STC's c_NPOS is defined as INTPTR_MAX which is not -1.
if you check i >= 0, then the code is error-prone.

@tylov
Copy link
Collaborator
tylov commented Oct 15, 2024

I will consider changing to -1 because of some other reason, but it is more complicated:
In c++, npos is of type size_t which is unsigned the largest number possible, but switching type or relying on the value of npos is not good. There are some functions, e.g. cstr_subview(s, pos, len), where you can pass in c_POS for len, and it will return to the end of the view (it stops at which comes first). I think this is also how it is working for some functions in c++.

@liigo
Copy link
Contributor Author
liigo commented Jan 13, 2025

outdated (v5 was out) , closing

@liigo liigo closed this as completed Jan 13, 2025
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

No branches or pull requests

2 participants
0