Hello, On Fri, Jul 3, 2020 at 9:15 PM Tito <[email protected]> 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 _______________________________________________ busybox mailing list [email protected] http://lists.busybox.net/mailman/listinfo/busybox
