<div dir="ltr">Hi,<br><br>I'm not sure what I'm missing, from a quick glance at strrchr (glibc's) it seems that it scans the string only once?<br><br>Thank you,<br>Martin<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 30 Jun 2020 at 01:34, Denys Vlasenko <<a href="mailto:vda.linux@googlemail.com">vda.linux@googlemail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This scans the string twice, unnecessarily. Let's not do that.<br>
<br>
On Thu, Jun 11, 2020 at 3:45 PM Martin Lewis <<a href="mailto:martin.lewis.x84@gmail.com" target="_blank">martin.lewis.x84@gmail.com</a>> wrote:<br>
><br>
> function                                             old     new   delta<br>
> last_char_is                                          53      30     -23<br>
> ------------------------------------------------------------------------------<br>
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-23)             Total: -23 bytes<br>
>    text    data     bss     dec     hex filename<br>
>  981322   16915    1872 1000109   f42ad busybox_old<br>
>  981299   16915    1872 1000086   f4296 busybox_unstripped<br>
><br>
> Signed-off-by: Martin Lewis <<a href="mailto:martin.lewis.x84@gmail.com" target="_blank">martin.lewis.x84@gmail.com</a>><br>
> ---<br>
>  libbb/last_char_is.c | 9 ++++-----<br>
>  1 file changed, 4 insertions(+), 5 deletions(-)<br>
><br>
> diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c<br>
> index 66f2e3635..1fff08f9f 100644<br>
> --- a/libbb/last_char_is.c<br>
> +++ b/libbb/last_char_is.c<br>
> @@ -13,11 +13,10 @@<br>
>   */<br>
>  char* FAST_FUNC last_char_is(const char *s, int c)<br>
>  {<br>
> -       if (s && *s) {<br>
> -               size_t sz = strlen(s) - 1;<br>
> -               s += sz;<br>
> -               if ( (unsigned char)*s == c)<br>
> -                       return (char*)s;<br>
> +       if (s) {<br>
> +               char *index = strrchr(s, c);<br>
> +               if (index && *(index + 1) == '\0')<br>
> +                       return index;<br>
>         }<br>
>         return NULL;<br>
>  }<br>
> --<br>
> 2.11.0<br>
><br>
> _______________________________________________<br>
> busybox mailing list<br>
> <a href="mailto:busybox@busybox.net" target="_blank">busybox@busybox.net</a><br>
> <a href="http://lists.busybox.net/mailman/listinfo/busybox" rel="noreferrer" target="_blank">http://lists.busybox.net/mailman/listinfo/busybox</a><br>
</blockquote></div>