[PATCH] libbb: shrink is_suffixed_with()

Tito farmatito at tiscali.it
Wed Aug 26 17:49:36 UTC 2015



On 08/26/2015 07:18 PM, Isaac Dunham wrote:
> By my test (Debian Jessie, GCC 4.9.2, glibc shared build), the results are:
> function                                             old     new   delta
> is_suffixed_with                                      61      45     -16
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-16)             Total: -16 bytes
>     text	   data	    bss	    dec	    hex	filename
>   760460	   2092	   9080	 771632	  bc630	busybox_old
>   760444	   2092	   9080	 771616	  bc620	busybox_unstripped
> ---
>   libbb/compare_string_array.c | 15 +++++----------
>   1 file changed, 5 insertions(+), 10 deletions(-)
>
> diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
> index 3dbd3eb..2a46e62 100644
> --- a/libbb/compare_string_array.c
> +++ b/libbb/compare_string_array.c
> @@ -35,17 +35,12 @@ char* FAST_FUNC is_prefixed_with(const char *string, const char *key)
>    */
>   char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
>   {
> -	size_t key_len = strlen(key);
> -	ssize_t len_diff = strlen(string) - key_len;
> +	char *ret;
>
> -	if (len_diff >= 0) {
> -		string += len_diff;
> -		if (strcmp(string, key) == 0) {
> -			return (char*)string;
> -		}
> -	}
> -
> -	return NULL;
> +	ret = strstr(string, key);
> +	if (ret && strcmp(ret, key))
> +		ret = NULL;
> +	return ret;
>   }
>
>   /* returns the array index of the string */
>

Hi,
I think this will not work in case of:

haystack = provatesttest  needle = test

I suspect it will return NULL instead of last "test"

unless you traverse in a loop the whole string like:

char *my_ends_with(const char *str, const char *key)
{
	char *p;
	const char *s = str;
	
	while((p = strstr(s, key)) != NULL && *key != 0) {
	//	printf("p = %s s = %s\n", p, s);
		if (strcmp(p, key) == 0)
			return p;
		s++;
	}
	return NULL;
}

Ciao,
Tito



More information about the busybox mailing list