[PATCH] libbb/last_char_is: rewrite for smaller and faster code
Emmanuel Deloget
logout at free.fr
Sat Jul 4 08:56:45 UTC 2020
Hello,
On Fri, Jul 3, 2020 at 9:15 PM Tito <farmatito at tiscali.it> wrote:
>
>
>
> 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;
> }
>
In favor of const-correctness, wouldn't it be better to return a const
char*? It's quite strange to promote a string to const and to a part
of it that is non-const (not to mention that if you feed the function
with a real const char* then the cast is not a good thing to do).
>
> > Fun to play with you.
> >
> > Cheers,
> >
> > Xabier Oneca_,,_
Best regards,
-- Emmanuel Deloget
More information about the busybox
mailing list