Skip to content

Add strisdigit(), and use it instead of its pattern #1153

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

Merged
merged 2 commits into from
Feb 16, 2025

Conversation

alejandro-colomar
Copy link
Collaborator

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


Revisions:

v2
  • Add strisdigit() and use it instead of open-coding it.
  • Add note about empty strings in commit message.
$ git range-diff master gh/alldigits alldigits 
-:  -------- > 1:  40c9035b lib/string/ctype/: strisdigit(): Add function
1:  a349dfd3 ! 2:  24335081 lib/: Reimplement all_digits() in terms of streq(stpspn()), and open-code it
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/: Reimplement all_digits() in terms of streq(stpspn()), and open-code it
    +    lib/: Use strisdigit() instead of its pattern
    +
    +    Note that the old code in
    +
    +            (1)  lib/strtoday.c:strtoday()
    +            (2)  lib/subordinateio.c:append_uids()
    +
    +    was considering an empty string as if it were a number.
    +    strisdigit() does not consider an empty string to be numeric.
    +
    +    I think it will not affect the behavior in either case, as they should
    +    sooner or later result in an error somewhere.  And it seems (IMO)
    +    surprising to treat empty strings as numeric strings, so let's not do
    +    it.
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/chkname.c
      
      #include "defines.h"
      #include "chkname.h"
    -+#include "string/strchr/stpspn.h"
    ++#include "string/ctype/strisdigit.h"
      #include "string/strcmp/streq.h"
      
      
    @@ lib/chkname.c: is_valid_name(const char *name)
               */
     -  int numeric;
     +
    -+  if (streq(stpspn(name, "0123456789"), "")) {
    ++  if (strisdigit(name)) {
     +          errno = EINVAL;
     +          return false;
     +  }
    @@ lib/chkname.c: is_valid_name(const char *name)
        return true;
     
      ## lib/strtoday.c ##
    +@@
    + #include "atoi/str2i/str2s.h"
    + #include "getdate.h"
    + #include "prototypes.h"
    ++#include "string/ctype/strisdigit.h"
    + #include "string/strchr/stpspn.h"
    + #include "string/strcmp/streq.h"
    + 
     @@
      long strtoday (const char *str)
      {
    @@ lib/strtoday.c: long strtoday (const char *str)
     -          s++;
     -  }
     -  if (isnum) {
    -+  if (streq(stpspn(s, "0123456789"), "")) {
    ++  if (strisdigit(s)) {
                long retdate;
     +
                if (str2sl(&retdate, str) == -1)
    @@ lib/strtoday.c: long strtoday (const char *str)
     
      ## lib/subordinateio.c ##
     @@
    + #include "alloc/realloc.h"
      #include "alloc/reallocf.h"
      #include "atoi/str2i/str2u.h"
    ++#include "string/ctype/strisdigit.h"
      #include "string/sprintf/snprintf.h"
    -+#include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
      
    - 
     @@ lib/subordinateio.c: out:
        return count;
      }
    @@ lib/subordinateio.c: out:
        uid_t  owner_uid;
      
     -  if (all_digits(owner)) {
    -+  if (streq(stpspn(owner, "0123456789"), "")) {
    ++  if (strisdigit(owner)) {
                i = sscanf(owner, "%d", &owner_uid);
                if (i != 1) {
                        // should not happen
v3
  • Clarify that this API works with the C locale.
$ git range-diff master gh/alldigits alldigits 
1:  40c9035b ! 1:  6183a653 lib/string/ctype/: strisdigit(): Add function
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/ctype/: strisdigit(): Add function
    +    lib/string/ctype/: strisdigit_c(): Add function
    +
    +    The "_c" clarifies that is considers the digits from the C locale.
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        spawn.c \
        sssd.c \
        sssd.h \
    -+  string/ctype/strisdigit.c \
    -+  string/ctype/strisdigit.h \
    ++  string/ctype/strisdigit_c.c \
    ++  string/ctype/strisdigit_c.h \
        string/memset/memzero.c \
        string/memset/memzero.h \
        string/sprintf/snprintf.c \
     
    - ## lib/string/ctype/strisdigit.c (new) ##
    + ## lib/string/ctype/strisdigit_c.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
    @@ lib/string/ctype/strisdigit.c (new)
     +
     +#include <config.h>
     +
    -+#include "string/ctype/strisdigit.h"
    ++#include "string/ctype/strisdigit_c.h"
     +
     +#include <stdbool.h>
     +
     +
    -+extern inline bool strisdigit(const char *s);
    ++extern inline bool strisdigit_c(const char *s);
     
    - ## lib/string/ctype/strisdigit.h (new) ##
    + ## lib/string/ctype/strisdigit_c.h (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
     +
     +
    -+#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_H_
    -+#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_H_
    ++#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_C_H_
    ++#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_C_H_
     +
     +
     +#include <config.h>
    @@ lib/string/ctype/strisdigit.h (new)
     +#include "string/strcmp/streq.h"
     +
     +
    -+inline bool strisdigit(const char *s);
    ++inline bool strisdigit_c(const char *s);
     +
     +
    -+// Like isdigit(3), but check all characters in the string
    ++// Like isdigit(3),
    ++// but check all characters in the string, and use the C locale.
     +inline bool
    -+strisdigit(const char *s)
    ++strisdigit_c(const char *s)
     +{
     +  if (streq(s, ""))
     +          return false;
2:  24335081 ! 2:  b9da40df lib/: Use strisdigit() instead of its pattern
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/: Use strisdigit() instead of its pattern
    +    lib/: Use strisdigit_c() instead of its pattern
     
         Note that the old code in
     
    @@ Commit message
                 (2)  lib/subordinateio.c:append_uids()
     
         was considering an empty string as if it were a number.
    -    strisdigit() does not consider an empty string to be numeric.
    +    strisdigit_c() does not consider an empty string to be numeric.
     
         I think it will not affect the behavior in either case, as they should
         sooner or later result in an error somewhere.  And it seems (IMO)
    @@ lib/chkname.c
      
      #include "defines.h"
      #include "chkname.h"
    -+#include "string/ctype/strisdigit.h"
    ++#include "string/ctype/strisdigit_c.h"
      #include "string/strcmp/streq.h"
      
      
    @@ lib/chkname.c: is_valid_name(const char *name)
               */
     -  int numeric;
     +
    -+  if (strisdigit(name)) {
    ++  if (strisdigit_c(name)) {
     +          errno = EINVAL;
     +          return false;
     +  }
    @@ lib/strtoday.c
      #include "atoi/str2i/str2s.h"
      #include "getdate.h"
      #include "prototypes.h"
    -+#include "string/ctype/strisdigit.h"
    ++#include "string/ctype/strisdigit_c.h"
      #include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
      
    @@ lib/strtoday.c: long strtoday (const char *str)
     -          s++;
     -  }
     -  if (isnum) {
    -+  if (strisdigit(s)) {
    ++  if (strisdigit_c(s)) {
                long retdate;
     +
                if (str2sl(&retdate, str) == -1)
    @@ lib/subordinateio.c
      #include "alloc/realloc.h"
      #include "alloc/reallocf.h"
      #include "atoi/str2i/str2u.h"
    -+#include "string/ctype/strisdigit.h"
    ++#include "string/ctype/strisdigit_c.h"
      #include "string/sprintf/snprintf.h"
      #include "string/strcmp/streq.h"
      
    @@ lib/subordinateio.c: out:
        uid_t  owner_uid;
      
     -  if (all_digits(owner)) {
    -+  if (strisdigit(owner)) {
    ++  if (strisdigit_c(owner)) {
                i = sscanf(owner, "%d", &owner_uid);
                if (i != 1) {
                        // should not happen
v4
  • Move files into subdirectory.
$ git range-diff master gh/alldigits alldigits 
1:  6183a653 ! 1:  e25769be lib/string/ctype/: strisdigit_c(): Add function
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/ctype/: strisdigit_c(): Add function
    +    lib/string/ctype/strisascii_c/: strisdigit_c(): Add function
     
    -    The "_c" clarifies that is considers the digits from the C locale.
    +    The "_c" means that it considers the digits from the C locale.
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        spawn.c \
        sssd.c \
        sssd.h \
    -+  string/ctype/strisdigit_c.c \
    -+  string/ctype/strisdigit_c.h \
    ++  string/ctype/strisascii_c/strisdigit_c.c \
    ++  string/ctype/strisascii_c/strisdigit_c.h \
        string/memset/memzero.c \
        string/memset/memzero.h \
        string/sprintf/snprintf.c \
     
    - ## lib/string/ctype/strisdigit_c.c (new) ##
    + ## lib/string/ctype/strisascii_c/strisdigit_c.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
    @@ lib/string/ctype/strisdigit_c.c (new)
     +
     +extern inline bool strisdigit_c(const char *s);
     
    - ## lib/string/ctype/strisdigit_c.h (new) ##
    + ## lib/string/ctype/strisascii_c/strisdigit_c.h (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
2:  b9da40df ! 2:  418486c1 lib/: Use strisdigit_c() instead of its pattern
    @@ lib/chkname.c
      
      #include "defines.h"
      #include "chkname.h"
    -+#include "string/ctype/strisdigit_c.h"
    ++#include "string/ctype/strisascii_c/strisdigit_c.h"
      #include "string/strcmp/streq.h"
      
      
    @@ lib/strtoday.c
      #include "atoi/str2i/str2s.h"
      #include "getdate.h"
      #include "prototypes.h"
    -+#include "string/ctype/strisdigit_c.h"
    ++#include "string/ctype/strisascii_c/strisdigit_c.h"
      #include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
      
    @@ lib/subordinateio.c
      #include "alloc/realloc.h"
      #include "alloc/reallocf.h"
      #include "atoi/str2i/str2u.h"
    -+#include "string/ctype/strisdigit_c.h"
    ++#include "string/ctype/strisascii_c/strisdigit_c.h"
      #include "string/sprintf/snprintf.h"
      #include "string/strcmp/streq.h"
      
v4b
  • Fix include guard and include path.
$ git range-diff master gh/alldigits alldigits 
1:  e25769be ! 1:  d160bacc lib/string/ctype/strisascii_c/: strisdigit_c(): Add function
    @@ lib/string/ctype/strisascii_c/strisdigit_c.c (new)
     +
     +#include <config.h>
     +
    -+#include "string/ctype/strisdigit_c.h"
    ++#include "string/ctype/strisascii_c/strisdigit_c.h"
     +
     +#include <stdbool.h>
     +
    @@ lib/string/ctype/strisascii_c/strisdigit_c.h (new)
     +// SPDX-License-Identifier: BSD-3-Clause
     +
     +
    -+#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_C_H_
    -+#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISDIGIT_C_H_
    ++#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_C_STRISDIGIT_C_H_
    ++#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_C_STRISDIGIT_C_H_
     +
     +
     +#include <config.h>
2:  418486c1 = 2:  04c1c1b1 lib/: Use strisdigit_c() instead of its pattern
v5
  • Simplify name (character encodings are a mess, anyway).
$ git range-diff master gh/alldigits alldigits 
1:  d160bacc ! 1:  d5fb6c45 lib/string/ctype/strisascii_c/: strisdigit_c(): Add function
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/string/ctype/strisascii_c/: strisdigit_c(): Add function
    -
    -    The "_c" means that it considers the digits from the C locale.
    +    lib/string/ctype/strisascii/: strisdigit(): Add function
     
         Signed-off-by: Alejandro Colomar <[email protected]>
     
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        spawn.c \
        sssd.c \
        sssd.h \
    -+  string/ctype/strisascii_c/strisdigit_c.c \
    -+  string/ctype/strisascii_c/strisdigit_c.h \
    ++  string/ctype/strisascii/strisdigit.c \
    ++  string/ctype/strisascii/strisdigit.h \
        string/memset/memzero.c \
        string/memset/memzero.h \
        string/sprintf/snprintf.c \
     
    - ## lib/string/ctype/strisascii_c/strisdigit_c.c (new) ##
    + ## lib/string/ctype/strisascii/strisdigit.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
    @@ lib/string/ctype/strisascii_c/strisdigit_c.c (new)
     +
     +#include <config.h>
     +
    -+#include "string/ctype/strisascii_c/strisdigit_c.h"
    ++#include "string/ctype/strisascii/strisdigit.h"
     +
     +#include <stdbool.h>
     +
     +
    -+extern inline bool strisdigit_c(const char *s);
    ++extern inline bool strisdigit(const char *s);
     
    - ## lib/string/ctype/strisascii_c/strisdigit_c.h (new) ##
    + ## lib/string/ctype/strisascii/strisdigit.h (new) ##
     @@
     +// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
     +// SPDX-License-Identifier: BSD-3-Clause
     +
     +
    -+#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_C_STRISDIGIT_C_H_
    -+#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_C_STRISDIGIT_C_H_
    ++#ifndef SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_STRISDIGIT_H_
    ++#define SHADOW_INCLUDE_LIB_STRING_CTYPE_STRISASCII_STRISDIGIT_H_
     +
     +
     +#include <config.h>
    @@ lib/string/ctype/strisascii_c/strisdigit_c.h (new)
     +#include "string/strcmp/streq.h"
     +
     +
    -+inline bool strisdigit_c(const char *s);
    ++inline bool strisdigit(const char *s);
     +
     +
    -+// Like isdigit(3),
    -+// but check all characters in the string, and use the C locale.
    ++// Like isdigit(3), but check all characters in the string.
     +inline bool
    -+strisdigit_c(const char *s)
    ++strisdigit(const char *s)
     +{
     +  if (streq(s, ""))
     +          return false;
2:  04c1c1b1 ! 2:  e22bf379 lib/: Use strisdigit_c() instead of its pattern
    @@ Metadata
     Author: Alejandro Colomar <[email protected]>
     
      ## Commit message ##
    -    lib/: Use strisdigit_c() instead of its pattern
    +    lib/: Use strisdigit() instead of its pattern
     
         Note that the old code in
     
    @@ Commit message
                 (2)  lib/subordinateio.c:append_uids()
     
         was considering an empty string as if it were a number.
    -    strisdigit_c() does not consider an empty string to be numeric.
    +    strisdigit() does not consider an empty string to be numeric.
     
         I think it will not affect the behavior in either case, as they should
         sooner or later result in an error somewhere.  And it seems (IMO)
    @@ lib/chkname.c
      
      #include "defines.h"
      #include "chkname.h"
    -+#include "string/ctype/strisascii_c/strisdigit_c.h"
    ++#include "string/ctype/strisascii/strisdigit.h"
      #include "string/strcmp/streq.h"
      
      
    @@ lib/chkname.c: is_valid_name(const char *name)
               */
     -  int numeric;
     +
    -+  if (strisdigit_c(name)) {
    ++  if (strisdigit(name)) {
     +          errno = EINVAL;
     +          return false;
     +  }
    @@ lib/strtoday.c
      #include "atoi/str2i/str2s.h"
      #include "getdate.h"
      #include "prototypes.h"
    -+#include "string/ctype/strisascii_c/strisdigit_c.h"
    ++#include "string/ctype/strisascii/strisdigit.h"
      #include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
      
    @@ lib/strtoday.c: long strtoday (const char *str)
     -          s++;
     -  }
     -  if (isnum) {
    -+  if (strisdigit_c(s)) {
    ++  if (strisdigit(s)) {
                long retdate;
     +
                if (str2sl(&retdate, str) == -1)
    @@ lib/subordinateio.c
      #include "alloc/realloc.h"
      #include "alloc/reallocf.h"
      #include "atoi/str2i/str2u.h"
    -+#include "string/ctype/strisascii_c/strisdigit_c.h"
    ++#include "string/ctype/strisascii/strisdigit.h"
      #include "string/sprintf/snprintf.h"
      #include "string/strcmp/streq.h"
      
    @@ lib/subordinateio.c: out:
        uid_t  owner_uid;
      
     -  if (all_digits(owner)) {
    -+  if (strisdigit_c(owner)) {
    ++  if (strisdigit(owner)) {
                i = sscanf(owner, "%d", &owner_uid);
                if (i != 1) {
                        // should not happen
v5b
  • Rebase
$ git range-diff alx/master..gh/alldigits master..alldigits 
1:  d5fb6c45 = 1:  eb0bf9e2 lib/string/ctype/strisascii/: strisdigit(): Add function
2:  e22bf379 = 2:  d549476d lib/: Use strisdigit() instead of its pattern
v5c
  • Rebase
$ git range-diff alx/master..gh/alldigits master..alldigits 
1:  eb0bf9e2 = 1:  f67e5ea0 lib/string/ctype/strisascii/: strisdigit(): Add function
2:  d549476d = 2:  2e145013 lib/: Use strisdigit() instead of its pattern
v5d
  • Rebase
$ git range-diff master..gh/alldigits shadow/master..alldigits 
1:  f67e5ea0 = 1:  b3d890b8 lib/string/ctype/strisascii/: strisdigit(): Add function
2:  2e145013 = 2:  39e47ac9 lib/: Use strisdigit() instead of its pattern
v5e
  • Rebase
$ git range-diff master..gh/alldigits shadow/master..alldigits 
1:  b3d890b8 = 1:  005f71a1 lib/string/ctype/strisascii/: strisdigit(): Add function
2:  39e47ac9 ! 2:  c8a57136 lib/: Use strisdigit() instead of its pattern
    @@ lib/chkname.c: is_valid_name(const char *name)
     +          return false;
     +  }
      
    -   if ('\0' == *name ||
    -       ('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
    +   if (streq(name, "") ||
    +       streq(name, ".") ||
     @@ lib/chkname.c: is_valid_name(const char *name)
                return false;
        }
v5f
  • Rebase
$ git range-diff master..gh/alldigits shadow/master..alldigits 
1:  005f71a1 = 1:  a99f3d54 lib/string/ctype/strisascii/: strisdigit(): Add function
2:  c8a57136 = 2:  8b969321 lib/: Use strisdigit() instead of its pattern
v6
  • Add comment spelling out the meaning of the letter-soup API names. [@hallyn ]
$ git range-diff master gh/alldigits alldigits 
1:  a99f3d54 ! 1:  7a12a0fa lib/string/ctype/strisascii/: strisdigit(): Add function
    @@ lib/string/ctype/strisascii/strisdigit.h (new)
     +inline bool strisdigit(const char *s);
     +
     +
    ++// string is [:digit:]
     +// Like isdigit(3), but check all characters in the string.
     +inline bool
     +strisdigit(const char *s)
2:  8b969321 = 2:  edf0994c lib/: Use strisdigit() instead of its pattern
v6b
$ git range-diff master..gh/alldigits shadow/master..alldigits 
1:  7a12a0fa ! 1:  b54bb7e9 lib/string/ctype/strisascii/: strisdigit(): Add function
    @@ Metadata
      ## Commit message ##
         lib/string/ctype/strisascii/: strisdigit(): Add function
     
    +    Reviewed-by: Serge Hallyn <[email protected]>
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## lib/Makefile.am ##
2:  edf0994c ! 2:  7388c365 lib/: Use strisdigit() instead of its pattern
    @@ Commit message
         surprising to treat empty strings as numeric strings, so let's not do
         it.
     
    +    Reviewed-by: Serge Hallyn <[email protected]>
         Signed-off-by: Alejandro Colomar <[email protected]>
     
      ## lib/chkname.c ##
    @@ lib/strtoday.c
      #include "getdate.h"
      #include "prototypes.h"
     +#include "string/ctype/strisascii/strisdigit.h"
    - #include "string/strchr/stpspn.h"
      #include "string/strcmp/streq.h"
    + #include "string/strspn/stpspn.h"
      
     @@
      long strtoday (const char *str)
v6c
  • Fix include path after rebase
$ git range-diff shadow/master gh/alldigits alldigits 
1:  b54bb7e9 ! 1:  ceef9e1a lib/string/ctype/strisascii/: strisdigit(): Add function
    @@ lib/string/ctype/strisascii/strisdigit.h (new)
     +
     +#include <stdbool.h>
     +
    -+#include "string/strchr/stpspn.h"
     +#include "string/strcmp/streq.h"
    ++#include "string/strspn/stpspn.h"
     +
     +
     +inline bool strisdigit(const char *s);
2:  7388c365 = 2:  b9e6bf2c lib/: Use strisdigit() instead of its pattern

@alejandro-colomar alejandro-colomar force-pushed the alldigits branch 3 times, most recently from a349dfd to 2433508 Compare December 11, 2024 01:23
@alejandro-colomar alejandro-colomar changed the title lib/subordinateio.c: Reimplement all_digits() in terms of streq(stpspn()), and open-code it lib/string/ctype/, lib/: Add and use strisdigit() Dec 11, 2024
@alejandro-colomar alejandro-colomar changed the title lib/string/ctype/, lib/: Add and use strisdigit() Add strisdigit(), and use it instead of its pattern Dec 11, 2024
@alejandro-colomar alejandro-colomar force-pushed the alldigits branch 4 times, most recently from 04c1c1b to e22bf37 Compare December 14, 2024 19:04
@alejandro-colomar alejandro-colomar marked this pull request as ready for review December 22, 2024 11:56
@alejandro-colomar alejandro-colomar marked this pull request as draft December 24, 2024 17:40
@alejandro-colomar alejandro-colomar marked this pull request as ready for review December 24, 2024 17:43
@alejandro-colomar alejandro-colomar force-pushed the alldigits branch 2 times, most recently from 2e14501 to 39e47ac Compare January 24, 2025 14:51
@alejandro-colomar alejandro-colomar force-pushed the alldigits branch 2 times, most recently from 8b96932 to edf0994 Compare February 16, 2025 16:16
@alejandro-colomar
Copy link
Collaborator Author

@hallyn , it would be interesting to get this one merged soon-ish. I have a decent number of patches that depend on this one, and that would reduce the amount of rebasing. Thanks!

@alejandro-colomar alejandro-colomar added Simpler A good issue for a new beginner and removed Simpler A good issue for a new beginner labels Feb 16, 2025
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.

If there are no precedents, I would prefer 'isalldigits' as the function name. Otherwise, feel free to merge.

@alejandro-colomar
Copy link
Collaborator Author

If there are no precedents, I would prefer 'isalldigits' as the function name. Otherwise, feel free to merge.

There are precedents, indeed:

https://codesearch.debian.net/search?q=%5Cbstrisdigit+*%5C%28&literal=0

There are no entries for isalldigits().

@alejandro-colomar
Copy link
Collaborator Author

If there are no precedents, I would prefer 'isalldigits' as the function name. Otherwise, feel free to merge.

There are precedents, indeed:

https://codesearch.debian.net/search?q=%5Cbstrisdigit+*%5C%28&literal=0

There are no entries for isalldigits().

Oh well, a different search engine shows results for isAllDigits():

https://sourcegraph.com/search?q=context%3Aglobal+isalldigits%5Cs*%5C%28&patternType=regexp&sm=0&__cc=1&df=%5B%22lang%22%2C%22C%22%2C%22lang%3Ac%22%5D

https://sourcegraph.com/search?q=context%3Aglobal+strisdigit%5Cs*%5C%28&patternType=regexp&sm=0&df=%5B%22lang%22%2C%22C%22%2C%22lang%3Ac%22%5D&__cc=1

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Feb 16, 2025

If there are no precedents, I would prefer 'isalldigits' as the function name. Otherwise, feel free to merge.

If you're worried about the name not being clear on if it means any characters or all characters, I have some APIs where a different meaning is wanted. This is the rule I developed for consistency:

strisXXX() (string is XXX) means that all characters are of that class.

strchrisXXX() (string character is XXX) means that any character is of that class.

I've got these APIs in the oven at the moment:

  • strisdigit()
  • strisprint()
  • strchriscntrl()

Does this plan sound consistent and intuitive to you?

@hallyn
Copy link
Member

hallyn commented Feb 16, 2025

If there are no precedents, I would prefer 'isalldigits' as the function name. Otherwise, feel free to merge.

If you're worried about the name not being clear on if it means any characters or all characters, I have some APIs where a different meaning is wanted. This is the rule I developed for consistency:

Right, that's my concern. If you ask me "is '1xx' a digit", I would say yes, or ask for clarification. Because a digit is explicitly one [0-9].

strisXXX() (string is XXX) means that all characters are of that class.

strchrisXXX() (string character is XXX) means that any character is of that class.

I've got these APIs in the oven at the moment:

* strisdigit()

* strisprint()

* strchriscntrl()

Does this plan sound consistent and intuitive to you?

In general it makes sense, but I'd still say the first should be strisdigits, just because of the definition of 'digit'.

@alejandro-colomar
Copy link
Collaborator Author

alejandro-colomar commented Feb 16, 2025

Right, that's my concern. If you ask me "is '1xx' a digit", I would say yes, or ask for clarification. Because a digit is explicitly one [0-9].

I'd say that a string character is a digit, but the string is not made up of digit characters.

So, strchrisdigit() true, but strisdigit() false.

strisXXX() (string is XXX) means that all characters are of that class.
strchrisXXX() (string character is XXX) means that any character is of that class.
I've got these APIs in the oven at the moment:

* strisdigit()

* strisprint()

* strchriscntrl()

Does this plan sound consistent and intuitive to you?

In general it makes sense, but I'd still say the first should be strisdigits, just because of the definition of 'digit'.

Hmmmm, I had that debate internally for a long time. I still haven't found an ideal name I like.

I agree that strisdigits() seems better. But then I wanted something that can be consistent with strisprint() too. I'm not sure about calling it strisprints()...

I considered straredigit(), but it sounds very weird. I could live with strallisdigit().

Reviewed-by: Serge Hallyn <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
Note that the old code in

	(1)  lib/strtoday.c:strtoday()
	(2)  lib/subordinateio.c:append_uids()

was considering an empty string as if it were a number.
strisdigit() does not consider an empty string to be numeric.

I think it will not affect the behavior in either case, as they should
sooner or later result in an error somewhere.  And it seems (IMO)
surprising to treat empty strings as numeric strings, so let's not do
it.

Reviewed-by: Serge Hallyn <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
@alejandro-colomar
Copy link
Collaborator Author

Please let me know your opinion. I slightly prefer strisdigit(), but something in my head says it's not a perfect name.

@hallyn
Copy link
Member

hallyn commented Feb 16, 2025

Please let me know your opinion. I slightly prefer strisdigit(), but something in my head says it's not a perfect name.

Ok let's stick with what you have.

@hallyn hallyn merged commit 77eb67d into shadow-maint:master Feb 16, 2025
10 checks passed
@alejandro-colomar alejandro-colomar deleted the alldigits branch February 16, 2025 22:18
@alejandro-colomar
Copy link
Collaborator Author

Thanks! Another PR with several patches waiting after it is #1152, for when you can have a look at it. :)

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