Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/: Simplify, using strpbrk(3) #1167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Dec 31, 2024

Checking a boolean (actually, a boolean-like pointer) is more readable
than comparing against a length.

This removes the only uses of strcspn(3) in this project.

strpbrk(3) is a simpler call, even though it has a weird name.  It's
just like strchr(3) but searches for several characters.  I'd have named
it strchrs().

@hallyn Oh well, this is your fault for mentioning that. :D


Revisions:

v2
  • Since strpbrk(3) is like strchr(3), use it similarly. That is, treat it as a boolean, with the meaning "the characters were found". Act as if it was called strchrs().

  • Use spaces instead of tabs.

$ git range-diff shadow/master gh/strpbrk strpbrk 
1:  878792c7 ! 1:  72823449 src/: Simplify, using strpbrk(3)
    @@ Metadata
      ## Commit message ##
         src/: Simplify, using strpbrk(3)
     
    -    Comparing against NULL is more readable than comparing against a length.
    +    Checking a boolean (actually, a boolean-like pointer) is more readable
    +    than comparing against a length.
     
    -    This removes the only uses of strcspn(3) in this project.  strpbrk(3) is
    -    a simpler call, even though it has a weird name.  It's just like
    -    strchr(3) but searches for several characters.  I'd have named it
    -    strchrs().
    +    This removes the only uses of strcspn(3) in this project.
    +
    +    strpbrk(3) is a simpler call, even though it has a weird name.  It's
    +    just like strchr(3) but searches for several characters.  I'd have named
    +    it strchrs().
     
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
    @@ src/useradd.c: static const char *def_log_init = "yes";
      static const char *def_expire = "";
      
     -#define   VALID(s)        (strcspn (s, ":\n") == strlen (s))
    -+#define   VALID(s)        (strpbrk(s, ":\n") == NULL)
    ++#define VALID(s)  (!strpbrk(s, ":\n"))
      
      static const char *user_name = "";
      static const char *user_pass = "!";
    @@ src/usermod.c
      #endif                            /* ENABLE_SUBIDS */
      
     -#define   VALID(s)        (strcspn (s, ":\n") == strlen (s))
    -+#define   VALID(s)        (strpbrk(s, ":\n") == NULL)
    ++#define VALID(s)  (!strpbrk(s, ":\n"))
      
      /*
       * Global variables
v3
  • Treat strpbrk(3) as a boolean everywhere.
$ git range-diff shadow/master gh/strpbrk strpbrk 
1:  72823449 = 1:  72823449 src/: Simplify, using strpbrk(3)
-:  -------- > 2:  258e9e29 lib/: Treat strpbrk(3)'s return value as a boolean
v3b
  • Rebase
$ git range-diff 4.17.1..gh/strpbrk master..strpbrk 
1:  72823449 = 1:  44e9c6ac src/: Simplify, using strpbrk(3)
2:  258e9e29 = 2:  c534aa67 lib/: Treat strpbrk(3)'s return value as a boolean
v3c
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  f6b53605 = 1:  8a3145a2 src/: Simplify, using strpbrk(3)
2:  e7a56050 = 2:  412d4daf lib/: Treat strpbrk(3)'s return value as a boolean

@alejandro-colomar alejandro-colomar changed the title src/: Simplify using strpbrk(3) src/: Simplify, using strpbrk(3) Dec 31, 2024
@alejandro-colomar alejandro-colomar force-pushed the strpbrk branch 2 times, most recently from b1e7c98 to 878792c Compare December 31, 2024 22:49
@alejandro-colomar alejandro-colomar marked this pull request as ready for review December 31, 2024 22:50
@alejandro-colomar
Copy link
Collaborator Author

BTW, 9front actually calls this function strchrs() instead of strpbrk(3). :)

https://github.com/9front/9front/blob/71ca9a668a27dfd215c48091aa6290f6f0416a14/sys/src/cmd/ip/cifsd/util.c#L205

Checking a boolean (actually, a boolean-like pointer) is more readable
than comparing against a length.

This removes the only uses of strcspn(3) in this project.

strpbrk(3) is a simpler call, even though it has a weird name.  It's
just like strchr(3) but searches for several characters.  I'd have named
it strchrs().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
with the meaning "a character was found".

strpbrk(3) is just like strchr(3), but searches for multiple characters.
Both functions have a boolean-like return value, which evaluates to true
if a character was found.

A better name for strpbrk(3) would have been strchrs().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant