Consider the following program:
#include <string>
#include <iostream>
int main() {
std::string token {"abc"};
const char* option_name_valid_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"_-";
auto pos = token.find_first_not_of(option_name_valid_chars, 0, 1);
std::cout << pos << std::endl;
}
This prints 0 (GodBolt), while I expect it to print the value of npos: All 1 characters at positions starting from 0 are valid, so the "first" position of an invalid char is npos.. or am I misunderstanding the semantics of find_first_not_of()?
You're misunderstanding
count, the third parameter.What you thought it means: Limit the haystack - limit the number of characters of
tokento search.What it actually means: Limit the needle - limit the of characters of
option_name_valid_charsto consider.What you should do is:
string_viewof your substring of interest (this requires C++17; otherwise you could make a copy with thetoken.substr(...))npos.