STC icon indicating copy to clipboard operation
STC copied to clipboard

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

Open liigo opened this issue 1 year ago • 1 comments

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.

liigo avatar Oct 15 '24 10:10 liigo

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++.

tylov avatar Oct 15 '24 21:10 tylov

outdated (v5 was out) , closing

liigo avatar Jan 13 '25 14:01 liigo