]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[libc] Add strchrnul()
authorMichael Brown <mcb30@ipxe.org>
Fri, 10 Jul 2026 10:50:35 +0000 (11:50 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 10 Jul 2026 11:46:05 +0000 (12:46 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/string.c
src/include/string.h
src/tests/string_test.c

index 2af19b7fe7d5f743c979471bb9cb2912bac71275..68f01105efaaa55ebf555af621c63a7a40477f62 100644 (file)
@@ -263,23 +263,37 @@ size_t strnlen ( const char *src, size_t max ) {
 }
 
 /**
- * Find character within a string
+ * Find character within a string, or NUL terminator
  *
  * @v src              String
  * @v character                Character to find
- * @ret found          Found character, or NULL if not found
+ * @ret found          Found character, or terminating NUL if not found
  */
-char * strchr ( const char *src, int character ) {
+char * strchrnul ( const char *src, int character ) {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );
 
        for ( ; ; src_bytes++ ) {
-               if ( *src_bytes == character )
+               if ( ( *src_bytes == character ) || ( ! *src_bytes ) )
                        return ( ( char * ) src_bytes );
-               if ( ! *src_bytes )
-                       return NULL;
        }
 }
 
+/**
+ * Find character within a string
+ *
+ * @v src              String
+ * @v character                Character to find
+ * @ret found          Found character, or NULL if not found
+ */
+char * strchr ( const char *src, int character ) {
+       char *found;
+
+       found = strchrnul ( src, character );
+       if ( character != *( ( uint8_t * ) found ) )
+               return NULL;
+       return found;
+}
+
 /**
  * Find rightmost character within a string
  *
index 3affe8e868c742be347972c92b3eb1338ae91fb9..6b43d8f4ed2347519299043630ff81468ade5689 100644 (file)
@@ -40,6 +40,7 @@ extern int __pure strncmp ( const char *first, const char *second,
                            size_t max ) __nonnull;
 extern size_t __pure strlen ( const char *src ) __nonnull;
 extern size_t __pure strnlen ( const char *src, size_t max ) __nonnull;
+extern char * __pure strchrnul ( const char *src, int character ) __nonnull;
 extern char * __pure strchr ( const char *src, int character ) __nonnull;
 extern char * __pure strrchr ( const char *src, int character ) __nonnull;
 extern char * __pure strstr ( const char *haystack,
index 6662848d396a5fbbb65456c0bbf6c6a799e4419a..6b659926e8cb741c9a4e761bf499a085415b0cd3 100644 (file)
@@ -70,6 +70,7 @@ static void string_test_exec ( void ) {
        ok ( *(strchr ( "Testing", 'e' )) == 'e' );
        ok ( *(strchr ( "Testing", 'g' )) == 'g' );
        ok ( strchr ( "Testing", 'x' ) == NULL );
+       ok ( *(strchr ( "Testing", '\0' )) == '\0' );
 
        /* Test strrchr() */
        ok ( strrchr ( "", 'a' ) == NULL );
@@ -77,6 +78,13 @@ static void string_test_exec ( void ) {
        ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
        ok ( strrchr ( "Haystack", 'x' ) == NULL );
 
+       /* Test strchrnul() */
+       ok ( *(strchrnul ( "", 'x' )) == '\0' );
+       ok ( *(strchrnul ( "Nonstandard", 's' )) == 's' );
+       ok ( *(strchrnul ( "Nonstandard", 't' )) == 't' );
+       ok ( *(strchrnul ( "Nonstandard", 'x' )) == '\0' );
+       ok ( *(strchrnul ( "Nonstandard", '\0' )) == '\0' );
+
        /* Test memchr() */
        ok ( memchr ( "", '\0', 0 ) == NULL );
        ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );