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

Jeff Johnston <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <CAOox84sw=FaVHqW9K=yoZnjiaDfoo4-BpOOncDiNWU0akT6yGg@mail.gmail.com>
Unless I am missing something, I think the code could be made even simpler
to return the pointer when found and return NULL otherwise.

index 774db1e..d984745 100644
--- a/newlib/libc/string/strpbrk.c
+++ b/newlib/libc/string/strpbrk.c
@@ -29,23 +29,16 @@ strpbrk (const char *s1,
        const char *s2)
 {
   const char *c = s2;
-  if (!*s1)
-    return (char *) NULL;

   while (*s1)
     {
       for (c = s2; *c; c++)
        {
          if (*s1 == *c)
-           break;
+           return (char *) s1;
        }
-      if (*c)
-       break;
       s1++;
     }

-  if (*c == '\0')
-    s1 = NULL;
-
-  return (char *) s1;
+  return (char *) NULL;
 }

On Wed, Dec 20, 2023 at 1:12 AM Xiao Zeng <[email protected]>
wrote:

> Signed-off-by: Xiao Zeng <[email protected]>
> ---
>  newlib/libc/string/strpbrk.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c
> index 774db1e6d..95e89c20c 100644
> --- a/newlib/libc/string/strpbrk.c
> +++ b/newlib/libc/string/strpbrk.c
> @@ -37,15 +37,14 @@ strpbrk (const char *s1,
>        for (c = s2; *c; c++)
>         {
>           if (*s1 == *c)
> -           break;
> +           goto end;
>         }
> -      if (*c)
> -       break;
>        s1++;
>      }
>
>    if (*c == '\0')
>      s1 = NULL;
>
> +end:
>    return (char *) s1;
>  }
> --
> 2.17.1
>
>
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.