/* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
* last char, not before. */
- if (*needle == 0)
+ if (needle[0] == 0)
return (char*) strchr(haystack, 0);
+ /* Special case: for single character strings, just use optimized strrchr() */
+ if (needle[1] == 0)
+ return (char*) strrchr(haystack, needle[0]);
+
for (const char *p = strstr(haystack, needle), *q; p; p = q) {
q = strstr(p + 1, needle);
if (!q)
- return (char *) p;
+ return (char*) p;
}
return NULL;
}