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

Tito farmatito at tiscali.it
Thu Jul 2 19:26:29 UTC 2020


On 7/2/20 12:02 AM, Jody Bruchon wrote:
> From 10389ff81b4ee5a7db293cf5d6205d8944e51901 Mon Sep 17 00:00:00 2001
> From: Jody Bruchon <jody at jodybruchon.com>
> Date: Wed, 1 Jul 2020 17:55:04 -0400
> Subject: [PATCH] libbb/last_char_is: rewrite for smaller and faster code
> 
> Signed-off-by: Jody Bruchon <jody at jodybruchon.com>
> ---
>  libbb/last_char_is.c | 14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/libbb/last_char_is.c b/libbb/last_char_is.c
> index 918526e6c..b146b606f 100644
> --- a/libbb/last_char_is.c
> +++ b/libbb/last_char_is.c
> @@ -9,16 +9,12 @@
>  #include "libbb.h"
> 
>  /* Find out if the last character of a string matches the one given */
> -char* FAST_FUNC last_char_is(const char *s, int c)
> +char* FAST_FUNC last_char_is(char *s, char c)
>  {
> -    if (s) {
> -        size_t sz = strlen(s);
> -        /* Don't underrun the buffer if the string length is 0 */
> -        if (sz != 0) {
> -            s += sz - 1;
> -            if ((unsigned char)*s == c)
> -                return (char*)s;
> -        }
> +    if (s && *s) {
> +        while (*s != '\0') s++;
> +        s--;
> +        if (*s == c) return s;
>      }
>      return NULL;
>  }


hi,
just for fun I add my version:

char* FAST_FUNC last_char_is(const char *s, int c)  {
	while (s && *s && *(s + 1)) s++;
	return (c == *s) ? (char *)s : NULL;
}


make bloatcheck 
function                                             old     new   delta
last_char_is                                          53      47      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-6)               Total: -6 bytes
   text    data     bss     dec     hex filename
 980860   16891    1872  999623   f40c7 busybox_old
 980854   16891    1872  999617   f40c1 busybox_unstripped

Ciao,
Tito



More information about the busybox mailing list