++stats.find;
- // on npos input std::string scans from the end of hay
- if (endPos == npos || endPos > length())
- endPos = length();
-
- // on empty hay std::string returns npos
+ // needle is bigger than haystack, impossible find
if (length() < needle.length())
return npos;
- // consistent with std::string: on empty needle return min(endpos,length())
+ // if startPos is npos, std::string scans from the end of hay
+ if (endPos == npos || endPos > length()-needle.length())
+ endPos = length()-needle.length();
+
+ // an empty needle found at the end of the haystack
if (needle.length() == 0)
return endPos;
char *bufBegin = buf();
char *cur = bufBegin+endPos;
- char needleBegin = needle[0];
+ const char needleBegin = needle[0];
while (cur >= bufBegin) {
if (*cur == needleBegin) {
if (0 == memcmp(needle.buf(), cur, needle.length())) {