[PATCH] libbb/last_char_is: rewrite for smaller and faster code

Jody Bruchon jody at jodybruchon.com
Fri Jul 3 02:58:56 UTC 2020



On July 2, 2020 3:26:29 PM EDT, Tito <farmatito at tiscali.it> wrote:
>hi,
>just for fun I add my version:
>
>char* FAST_FUNC last_char_is(const char *s, int c)  {
>	while (s && *s && *(s + 1)) s++;
>	return (c == *s) ? (char *)s : NULL;
>}

That's pretty small, but it performs three comparisons for each character until the end and effectively scans the string twice in "parallel." If a bad pointer is passed in s, it'll segfault at the equality check in the return statement even though the first comparison is supposed to be a null pointer safeguard.


More information about the busybox mailing list