Re: [PATCH] newlib: libc: Improved the readability of strcspn with minor optimization

Torbjorn SVENSSON <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hello Xiao,

On 2023-12-15 09:31, Xiao Zeng wrote:
> Signed-off-by: Xiao Zeng <[email protected]>
> ---
>   newlib/libc/string/strcspn.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/newlib/libc/string/strcspn.c b/newlib/libc/string/strcspn.c
> index abaa93ad6..8ac0bf10c 100644
> --- a/newlib/libc/string/strcspn.c
> +++ b/newlib/libc/string/strcspn.c
> @@ -37,12 +37,10 @@ strcspn (const char *s1,
>         for (c = s2; *c; c++)
>   	{
>   	  if (*s1 == *c)
> -	    break;
> +	    goto end;
>   	}
> -      if (*c)
> -	break;
>         s1++;
>       }
> -
> +end:
>     return s1 - s;
>   }

Just looking at this small snippet of code, I would say that the 
previous code and your suggestion won't do the same thing.

Do you have unit tests that confirm that the behavior is identical with 
the current implementation and your suggested change?

When I run your suggestion, I get return value 0, but with the current 
implementation it's 3 for this call: strspn("129th", "1234567890").

Kind regards,
Torbjörn
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.