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

Simplify string handling with a new API: stpsep() #1038

Merged
merged 8 commits into from
Jul 9, 2024

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Jul 2, 2024


Revisions:

v1b
  • Don't discard restrict.
$ git range-diff port gh/stpsep stpsep 
1:  42e52881 ! 1:  006032d2 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
    @@ lib/string/strtok/stpsep.c (new)
     +#include "string/strtok/stpsep.h"
     +
     +
    -+extern inline char *stpsep(char *restrict s, const char *restrict delim);
    ++extern inline char *stpsep(char *s, const char *restrict delim);
     
      ## lib/string/strtok/stpsep.h (new) ##
     @@
    @@ lib/string/strtok/stpsep.h (new)
     +
     +
     +ATTR_STRING(1, 2)
    -+inline char *stpsep(char *restrict s, const char *restrict delim);
    ++inline char *stpsep(char *s, const char *restrict delim);
     +
     +
     +// Similar to strsep(3),
    @@ lib/string/strtok/stpsep.h (new)
     +// Similar to strtok(3),
     +// but don't store a state, and don't skip empty fields.
     +inline char *
    -+stpsep(char *restrict s, const char * restrict delim)
    ++stpsep(char *s, const char * restrict delim)
     +{
     +  strsep(&s, delim);
     +
2:  5ae3bb15 = 2:  8317e0e8 lib/, src/: Use stpsep() instead of its pattern
v1c
  • Fix ATTR_STRING()
$ git range-diff port gh/stpsep stpsep 
1:  006032d2 ! 1:  dcef1912 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
    @@ lib/string/strtok/stpsep.h (new)
     +#include <string.h>
     +
     +
    -+ATTR_STRING(1, 2)
    ++ATTR_STRING(1) ATTR_STRING(2)
     +inline char *stpsep(char *s, const char *restrict delim);
     +
     +
2:  8317e0e8 = 2:  4c9239ac lib/, src/: Use stpsep() instead of its pattern
-:  -------- > 3:  d4edd20f lib/attr.h: ATTR_STRING(): It only accepts one argument
v1d
  • Add missing include
$ git range-diff port d4edd20f fcecb348
1:  dcef1912 ! 1:  a673b932 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
    @@ lib/string/strtok/stpsep.h (new)
     +
     +#include <string.h>
     +
    ++#include "attr.h"
    ++
     +
     +ATTR_STRING(1) ATTR_STRING(2)
     +inline char *stpsep(char *s, const char *restrict delim);
2:  4c9239ac = 2:  206d4c4e lib/, src/: Use stpsep() instead of its pattern
3:  d4edd20f = 3:  fcecb348 lib/attr.h: ATTR_STRING(): It only accepts one argument
v2
  • Remove stpcspn(), which is now unused.
$ git range-diff port gh/stpsep stpsep 
1:  a673b932 = 1:  a673b932 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
2:  206d4c4e = 2:  206d4c4e lib/, src/: Use stpsep() instead of its pattern
3:  fcecb348 = 3:  fcecb348 lib/attr.h: ATTR_STRING(): It only accepts one argument
-:  -------- > 4:  7227070f lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
v2b
  • Rebase
$ git range-diff gh/port..gh/stpsep port..stpsep 
1:  a673b932 = 1:  75cb36e5 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
2:  206d4c4e ! 2:  9ba6ef0a lib/, src/: Use stpsep() instead of its pattern
    @@ lib/sgetgrent.c: struct group *sgetgrent (const char *buf)
     
      ## lib/sgetspent.c ##
     @@
    - #include <string.h>
      
    + #include "atoi/a2i.h"
      #include "atoi/str2i.h"
     +#include "defines.h"
      #include "prototypes.h"
3:  fcecb348 = 3:  83a082e8 lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  7227070f = 4:  510bbdc3 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
v2c
  • Fix commit message
  • wsfix
$ git range-diff port gh/stpsep stpsep 
1:  75cb36e5 ! 1:  3cb334c4 lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
    @@ Metadata
     Author: Alejandro Colomar <alx@kernel.org>
     
      ## Commit message ##
    -    lib/string/strtok/stpsep.[ch]: stpsep(): Add macro
    +    lib/string/strtok/stpsep.[ch]: stpsep(): Add function
     
    -    This macro is somewhat simpler to use than strsep(3) in some cases.
    +    This function is somewhat simpler to use than strsep(3) in some cases.
     
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
    @@ lib/string/strtok/stpsep.h (new)
     +// Similar to strtok(3),
     +// but don't store a state, and don't skip empty fields.
     +inline char *
    -+stpsep(char *s, const char * restrict delim)
    ++stpsep(char *s, const char *restrict delim)
     +{
     +  strsep(&s, delim);
     +
2:  9ba6ef0a = 2:  32e48a0f lib/, src/: Use stpsep() instead of its pattern
3:  83a082e8 = 3:  e4c0f525 lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  510bbdc3 = 4:  f3555448 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
v2d
  • Rebase
$ git range-diff gh/port..gh/stpsep port..stpsep 
1:  3cb334c4 = 1:  76b2acf4 lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  32e48a0f = 2:  4bc8555d lib/, src/: Use stpsep() instead of its pattern
3:  e4c0f525 = 3:  715defa1 lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  f3555448 ! 4:  e76ed026 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
    @@ lib/string/strchr/stpcspn.h (deleted)
     -
     -
     -// Similar to strcspn(3), but return a pointer.
    +-// Similar to strchrnul(3), but search for several delimiters.
    +-// Similar to strpbrk(3), but return 's + strlen(s)' if not found.
     -#define stpcspn(s, reject)                                                    \
     -({                                                                            \
     -  __auto_type  s_ = s;                                                  \
v2e
  • Rebase
$ git range-diff gh/port..gh/stpsep port..stpsep 
1:  76b2acf4 = 1:  4eeeeac6 lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  4bc8555d ! 2:  83d3760d lib/, src/: Use stpsep() instead of its pattern
    @@ lib/fields.c
     @@
      
      #include "prototypes.h"
    - #include "string/strchr/stprspn.h"
    + #include "string/strchr/strrspn.h"
     +#include "string/strtok/stpsep.h"
      
      
    @@ lib/getdef.c
      #include "string/sprintf/xasprintf.h"
     -#include "string/strchr/stpcspn.h"
      #include "string/strchr/stpspn.h"
    - #include "string/strchr/stprspn.h"
    + #include "string/strchr/strrspn.h"
     +#include "string/strtok/stpsep.h"
      
      
    @@ src/login_nopam.c
     @@
      
      #include "sizeof.h"
    - #include "string/strchr/stprspn.h"
    + #include "string/strchr/strrspn.h"
     +#include "string/strtok/stpsep.h"
     +
      
3:  715defa1 = 3:  66963c48 lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  e76ed026 ! 4:  f21b9c7f lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        string/sprintf/xasprintf.h \
     -  string/strchr/stpcspn.c \
     -  string/strchr/stpcspn.h \
    -   string/strchr/stprspn.c \
    -   string/strchr/stprspn.h \
        string/strchr/stpspn.c \
    +   string/strchr/stpspn.h \
    +   string/strchr/strnul.c \
     
      ## lib/string/strchr/stpcspn.c (deleted) ##
     @@
    @@ lib/string/strchr/stpcspn.h (deleted)
     -#include "attr.h"
     -
     -
    --// Similar to strcspn(3), but return a pointer.
    +-// Similar to strcspn(3), but return a pointer instead of an offset.
     -// Similar to strchrnul(3), but search for several delimiters.
     -// Similar to strpbrk(3), but return 's + strlen(s)' if not found.
     -#define stpcspn(s, reject)                                                    \
v2f
  • Rebase
$ git range-diff 4eeeeac6^..gh/stpsep port..stpsep 
1:  4eeeeac6 = 1:  a000f6e4 lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  83d3760d = 2:  8ed31c5b lib/, src/: Use stpsep() instead of its pattern
3:  66963c48 = 3:  99a19749 lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  f21b9c7f = 4:  c857e971 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
v2g
  • Rebase
$ git range-diff port..gh/stpsep shadow/master..stpsep 
1:  a000f6e4 = 1:  66bd1dfd lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  8ed31c5b = 2:  6a68dddb lib/, src/: Use stpsep() instead of its pattern
3:  99a19749 = 3:  da0c4fcc lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  c857e971 = 4:  11af18d3 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
v3
  • Use stpsep() in more places.
$ git range-diff shadow/master gh/stpsep stpsep 
1:  66bd1dfd = 1:  66bd1dfd lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  6a68dddb = 2:  6a68dddb lib/, src/: Use stpsep() instead of its pattern
3:  da0c4fcc = 3:  da0c4fcc lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  11af18d3 = 4:  11af18d3 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
-:  -------- > 5:  6449ea67 src/suauth.c: check_su_auth(): Use stpsep() to simplify
-:  -------- > 6:  f5150d8e lib/nss.c: nss_init(): Use stpsep() instead of strtok_r(3)
-:  -------- > 7:  d2b17255 src/useradd.c: Use stpsep() to simplify
-:  -------- > 8:  40e4a958 src/login_nopam.c: login_access(): Use stpsep() to simplify
v3b
  • tfix
$ git range-diff shadow/master gh/stpsep stpsep 
1:  66bd1dfd = 1:  66bd1dfd lib/string/strtok/stpsep.[ch]: stpsep(): Add function
2:  6a68dddb = 2:  6a68dddb lib/, src/: Use stpsep() instead of its pattern
3:  da0c4fcc = 3:  da0c4fcc lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  11af18d3 = 4:  11af18d3 lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
5:  6449ea67 = 5:  6449ea67 src/suauth.c: check_su_auth(): Use stpsep() to simplify
6:  f5150d8e = 6:  f5150d8e lib/nss.c: nss_init(): Use stpsep() instead of strtok_r(3)
7:  d2b17255 ! 7:  d02eeddc src/useradd.c: Use stpsep() to simplify
    @@ src/useradd.c: static void get_defaults (void)
      
     -          cp = strchr (buf, '=');
     -          if (NULL == cp) {
    -+          cp = stpsep(buf, '=');
    ++          cp = stpsep(buf, "=");
     +          if (NULL == cp)
                        continue;
     -          }
8:  40e4a958 = 8:  a53ebe80 src/login_nopam.c: login_access(): Use stpsep() to simplify
v3c
$ git range-diff shadow/master gh/stpsep stpsep 
1:  66bd1dfd ! 1:  7139f094 lib/string/strtok/stpsep.[ch]: stpsep(): Add function
    @@ lib/string/strtok/stpsep.c (new)
     +#include "string/strtok/stpsep.h"
     +
     +
    -+extern inline char *stpsep(char *s, const char *restrict delim);
    ++extern inline char *stpsep(char *s, const char *delim);
     
      ## lib/string/strtok/stpsep.h (new) ##
     @@
    @@ lib/string/strtok/stpsep.h (new)
     +
     +
     +ATTR_STRING(1) ATTR_STRING(2)
    -+inline char *stpsep(char *s, const char *restrict delim);
    ++inline char *stpsep(char *s, const char *delim);
     +
     +
     +// Similar to strsep(3),
    @@ lib/string/strtok/stpsep.h (new)
     +// Similar to strtok(3),
     +// but don't store a state, and don't skip empty fields.
     +inline char *
    -+stpsep(char *s, const char *restrict delim)
    ++stpsep(char *s, const char *delim)
     +{
     +  strsep(&s, delim);
     +
2:  6a68dddb = 2:  7bf4ba14 lib/, src/: Use stpsep() instead of its pattern
3:  da0c4fcc = 3:  e0ccb05c lib/attr.h: ATTR_STRING(): It only accepts one argument
4:  11af18d3 = 4:  e7cf1c0b lib/string/strchr/stpcspn.[ch]: stpcspn(): Remove unused function
5:  6449ea67 = 5:  79f7da31 src/suauth.c: check_su_auth(): Use stpsep() to simplify
6:  f5150d8e = 6:  d67629ce lib/nss.c: nss_init(): Use stpsep() instead of strtok_r(3)
7:  d02eeddc = 7:  9cbc404f src/useradd.c: Use stpsep() to simplify
8:  a53ebe80 = 8:  643f9980 src/login_nopam.c: login_access(): Use stpsep() to simplify

This function is somewhat simpler to use than strsep(3) in some cases.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This allows using plain strcmp(3) instead of MATCH().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
hallyn
hallyn previously approved these changes Jul 7, 2024
Copy link
Member

@hallyn hallyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@hallyn hallyn dismissed their stale review July 7, 2024 02:31

Sorry - just realized there are 4 functions added since I last downloaded the patchset

@hallyn hallyn merged commit 568d26d into shadow-maint:master Jul 9, 2024
9 checks passed
@alejandro-colomar alejandro-colomar deleted the stpsep branch July 11, 2024 08:41
@thesamesam thesamesam mentioned this pull request Jul 18, 2024
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.

2 participants