[go: up one dir, main page]

Menu

#1 StartsWith

1.0
closed
None
2019-11-07
2019-11-07
No

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.

Discussion

  • Alexey Sokirko

    Alexey Sokirko - 2019-11-07

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

     
  • Bryan Jurish

    Bryan Jurish - 2019-11-07

    indeed you are correct , and I was wrong: the assert() succeeds. my error: I had been overlooking (or misinterpreting) the 2nd argument. Sorry!

     
  • Bryan Jurish

    Bryan Jurish - 2019-11-07
    • status: open --> accepted
     
  • Alexey Sokirko

    Alexey Sokirko - 2019-11-07
    • status: accepted --> closed
     

Log in to post a comment.