[PATCH 4/4] last_char_is: code shrink

Denys Vlasenko vda.linux at googlemail.com
Tue Jun 30 06:33:54 UTC 2020


This scans the string twice, unnecessarily. Let's not do that.

On Thu, Jun 11, 2020 at 3:45 PM Martin Lewis <martin.lewis.x84 at gmail.com> wrote:
>
> function                                             old     new   delta
> last_char_is                                          53      30     -23
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-23)             Total: -23 bytes
>    text    data     bss     dec     hex filename
>  981322   16915    1872 1000109   f42ad busybox_old
>  981299   16915    1872 1000086   f4296 busybox_unstripped
>
> Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
> ---
>  libbb/last_char_is.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c
> index 66f2e3635..1fff08f9f 100644
> --- a/libbb/last_char_is.c
> +++ b/libbb/last_char_is.c
> @@ -13,11 +13,10 @@
>   */
>  char* FAST_FUNC last_char_is(const char *s, int c)
>  {
> -       if (s && *s) {
> -               size_t sz = strlen(s) - 1;
> -               s += sz;
> -               if ( (unsigned char)*s == c)
> -                       return (char*)s;
> +       if (s) {
> +               char *index = strrchr(s, c);
> +               if (index && *(index + 1) == '\0')
> +                       return index;
>         }
>         return NULL;
>  }
> --
> 2.11.0
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox


More information about the busybox mailing list