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

Tito farmatito at tiscali.it
Fri Jul 3 19:15:07 UTC 2020



On 7/3/20 6:13 PM, Xabier Oneca -- xOneca wrote:
> Hi all,
> 
>>> On Fri, 2020-07-03 at 08:12 +0200, Tito wrote:
>>> [...]
>>>> Improved version:
>>>>
>>>> char* last_char_is(const char *s, int c)  {
>>>>      if (!s || !*s) return NULL;
>>>>      while (*(s + 1))s++;
>>>>      return (c == *s) ? (char *)s : NULL;
>>>> }
> 
> Let me "improve" the Tito's -11 bytes, probably introducing some subtle bug :)
> 
> char* FAST_FUNC last_char_is(const char *s, int c)
> {
>     if (!s || !*s) return NULL;
>     while (*(++s));
>     return (*(--s) == c) ? s : NULL;
> }
> 
> function                                             old     new   delta
> last_char_is                                          58      42     -16
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-16)             Total: -16 bytes
>    text    data     bss     dec     hex filename
>  956449    4235    1904  962588   eb01c busybox_old
>  956433    4235    1904  962572   eb00c busybox_unstripped
> 
> Also, there's a warning compiling this function: return discards
> ‘const’ qualifier from pointer target type

Hi,
cast it?

char* FAST_FUNC last_char_is(const char *s, int c)
{
     if (!s || !*s) return NULL;
     while (*(++s));
     return (*(--s) == c) ? (char *)s : NULL;
}

> Fun to play with you.
> 
> Cheers,
> 
> Xabier Oneca_,,_


More information about the busybox mailing list