Skip to content

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
v3d
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  8a3145a2 = 1:  e7591047 src/: Simplify, using strpbrk(3)
2:  412d4daf = 2:  77bc9e92 lib/: Treat strpbrk(3)'s return value as a boolean
v3e
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  e7591047 = 1:  7ee7ee10 src/: Simplify, using strpbrk(3)
2:  77bc9e92 = 2:  6d49f866 lib/: Treat strpbrk(3)'s return value as a boolean
v3f
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  7ee7ee10 = 1:  dba6cd9c src/: Simplify, using strpbrk(3)
2:  6d49f866 = 2:  97e9e54f lib/: Treat strpbrk(3)'s return value as a boolean
v3g
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  dba6cd9c = 1:  915f7fc0 src/: Simplify, using strpbrk(3)
2:  97e9e54f = 2:  37771da3 lib/: Treat strpbrk(3)'s return value as a boolean
v3h
  • Rebase
$ git range-diff master..gh/strpbrk shadow/master..strpbrk 
1:  915f7fc0 = 1:  514789e0 src/: Simplify, using strpbrk(3)
2:  37771da3 = 2:  23bd7cf0 lib/: Treat strpbrk(3)'s return value as a boolean
v3i
  • Rebase
$ git rd
1:  514789e0 = 1:  d38a7d78 src/: Simplify, using strpbrk(3)
2:  23bd7cf0 = 2:  5d1ed05f lib/: Treat strpbrk(3)'s return value as a boolean
v3j
  • Rebase
$ git rd 
1:  d38a7d78 = 1:  d03c69a6 src/: Simplify, using strpbrk(3)
2:  5d1ed05f = 2:  10051c02 lib/: Treat strpbrk(3)'s return value as a boolean
v3k
  • Rebase
$ git rd
1:  d03c69a6 = 1:  47d740d4 src/: Simplify, using strpbrk(3)
2:  10051c02 = 2:  5a7cfa9f 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>
@hallyn hallyn merged commit 38e2951 into shadow-maint:master May 27, 2025
10 checks passed
@alejandro-colomar alejandro-colomar deleted the strpbrk branch May 27, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Simpler A good issue for a new beginner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants