TODO @Alexey: r1198 utilit.cpp StartsWith()
why use rfind() here ?
rfind() finds the last occurrence (i.e. searches right-to-left), so
string s("foobarfoo");
int rfound = s.rfind("foo"); //-- rfound=6
int found = s.find("foo"); //-- found=0
assert(StartsWith(s,"foo")); //-- fails!
wouldn't it be better (and more intutively match c++20 std::string::starts_with() semantics) to use compare() or find()?
//body.find(prefix) == 0; //-- correct but inefficient for long body strings
body.compare(0, prefix.size(), prefix) == 0; //-- correct and reasonably efficient
please change to use compare() unless there's some important reason to use rfind() that I'm not seeing; if there is, please change the name of the method to something other than "StartsWith()", because that's misleading.
Bryan, have you really tried ?
string s("foobarfoo");
assert(StartsWith(s,"foo")); //-- fails!
Because it does not fail on my gcc. Please, tell me the version of your gcc
Please note that I use rfind(s, 0) (two arguments).
indeed you are correct , and I was wrong: the assert() succeeds. my error: I had been overlooking (or misinterpreting) the 2nd argument. Sorry!