Skip to content

Conversation

alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Aug 16, 2025

These APIs are like streq() and strprefix(), but for nonstrings (thus, the usual strn*() name).

The first parameter is a nonstring, and the second is a string.


Revisions:

v2
  • Drop STRNPFX() from this patch set. I'll keep that for later.
$ git range-diff master gh/strneq strneq 
1:  cc5fcce1 = 1:  cc5fcce1 lib/, src/: Use strncmp(3) instead of explicit byte comparisons
2:  c6f4546b = 2:  c6f4546b lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
3:  c7bc9f1d = 3:  c7bc9f1d lib/, src/: Use STRNEQ() instead of their pattern
4:  b1f734cf < -:  -------- lib/string/strcmp/: strnpfx(), STRNPFX(): Add APIs
5:  fbdade07 < -:  -------- src/logoutd.c: Use STRNPFX() instead of its pattern
v2b
  • Rebase
$ git rd
1:  cc5fcce1 = 1:  f41a6069 lib/, src/: Use strncmp(3) instead of explicit byte comparisons
2:  c6f4546b = 2:  23b111ae lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
3:  c7bc9f1d = 3:  fc42cc1b lib/, src/: Use STRNEQ() instead of their pattern
v3c
  • Update lib/string/README.
$ git range-diff shadow/master gh/strneq strneq 
1:  f41a6069 = 1:  f41a6069 lib/, src/: Use strncmp(3) instead of explicit byte comparisons
2:  23b111ae ! 2:  30c4e70d lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
    @@ lib/Makefile.am: libshadow_la_SOURCES = \
        string/strcmp/strprefix.h \
        string/strcpy/stpecpy.c \
     
    + ## lib/string/README ##
    +@@ lib/string/README: Don't use some libc functions without Really Good Reasons:
    +   The return value of strcmp(3) is confusing.
    +   strcmp(3) would be legitimate for sorting strings.
    + 
    ++    strncmp(3)
    ++  Use STRNEQ(), or strpfx(), or else, depending on what you want.
    ++  The return value of strncmp(3) is confusing,
    ++  and it is unclear the purpose of its use when you read it.
    ++
    +     strcasecmp(3)
    +   Use strcaseeq() instead.
    + 
    +@@ lib/string/README: strcmp/ - String comparison
    +   Like strsfx(), but ignore upper-/lower-case.
    + 
    +   n/
    +-    strneq()  // Unimplemented
    ++    strneq()
    +   Return true if a [[gnu::nonstring]] is equal to a string.
    +-    STRNEQ()  // Unimplemented
    ++    STRNEQ()
    +   Like strneq(), but takes an array.
    + 
    +     strnpfx()  // Unimplemented
    +
      ## lib/string/strcmp/strneq.c (new) ##
     @@
     +// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
3:  fc42cc1b = 3:  49a51b7e lib/, src/: Use STRNEQ() instead of their pattern
v4
  • Use [[gnu::nonstring]].
$ git range-diff shadow/master gh/strneq strneq 
1:  f41a6069 = 1:  f41a6069 lib/, src/: Use strncmp(3) instead of explicit byte comparisons
-:  -------- > 2:  cb760ff7 lib/attr.h: __has_c_attribute(): Define fallback
-:  -------- > 3:  44ff1464 lib/attr.h: ATTR_NONSTRING: Add attribute [[gnu::nonstring]]
2:  30c4e70d ! 4:  a302e99e lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
    @@ lib/string/strcmp/strneq.h (new)
     +
     +
     +ATTR_STRING(2)
    -+inline bool strneq(const char *strn, const char *s, size_t size);
    ++inline bool strneq(ATTR_NONSTRING const char *strn, const char *s, size_t size);
     +
     +
     +// nonstring equal
3:  49a51b7e = 5:  39c64afc lib/, src/: Use STRNEQ() instead of their pattern
v4b
  • Rebase
$ git rd 
1:  f41a6069 = 1:  a9f960b9 lib/, src/: Use strncmp(3) instead of explicit byte comparisons
2:  cb760ff7 = 2:  419505c3 lib/attr.h: __has_c_attribute(): Define fallback
3:  44ff1464 = 3:  40602dd4 lib/attr.h: ATTR_NONSTRING: Add attribute [[gnu::nonstring]]
4:  a302e99e = 4:  db0d2614 lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
5:  39c64afc = 5:  cb9791cc lib/, src/: Use STRNEQ() instead of their pattern

@alejandro-colomar alejandro-colomar changed the title Add STRNEQ(), and use it instead of its pattern Add STRNEQ(), STRNPFX(), and use them instead of its pattern Aug 16, 2025
@alejandro-colomar alejandro-colomar force-pushed the strneq branch 4 times, most recently from 1612a74 to fbdade0 Compare August 16, 2025 22:52
@alejandro-colomar alejandro-colomar marked this pull request as ready for review August 16, 2025 23:05
@alejandro-colomar alejandro-colomar changed the title Add STRNEQ(), STRNPFX(), and use them instead of its pattern Add STRNEQ(), STRNPFX(), and use them instead of their patterns Aug 16, 2025
@alejandro-colomar alejandro-colomar changed the title Add STRNEQ(), STRNPFX(), and use them instead of their patterns Add STRNEQ(), and use it instead of its pattern Sep 30, 2025
@alejandro-colomar alejandro-colomar force-pushed the strneq branch 2 times, most recently from fc42cc1 to 49a51b7 Compare September 30, 2025 08:13
This is simpler to read, IMO.

Signed-off-by: Alejandro Colomar <[email protected]>
This allows using __has_c_attribute() in compilers that don't have it.

Signed-off-by: Alejandro Colomar <[email protected]>
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