Paul Eggert wrote: > > Should we break the "tradition" here to use 'int' for a Boolean value, > > and actually use 'bool' for the first time in <string.h>? > > I would prefer it, if it's a boolean function.
Done: 2025-01-04 Bruno Haible <br...@clisp.org> *_startswith, *_endswith: Change return type to 'bool'. Suggested by Paul Eggert. * lib/string.in.h (str_startswith, str_endswith, mbs_startswith, mbs_endswith): Change return type to 'bool'. * lib/str_startswith.c (str_startswith): Likewise. * lib/str_endswith.c (str_endswith): Likewise. * lib/mbs_endswith.c (mbs_endswith): Likewise. * modules/str_startswith (Depends-on): Add bool. * modules/str_endswith (Depends-on): Likewise. * modules/mbs_startswith (Depends-on): Likewise. * modules/mbs_endswith (Depends-on): Likewise. diff --git a/lib/mbs_endswith.c b/lib/mbs_endswith.c index 5007a445ec..8f163e1aa5 100644 --- a/lib/mbs_endswith.c +++ b/lib/mbs_endswith.c @@ -25,7 +25,7 @@ #include <stdlib.h> -int +bool mbs_endswith (const char *string, const char *suffix) { if ((unsigned char) suffix[0] < 0x30) diff --git a/lib/str_endswith.c b/lib/str_endswith.c index f33ac6997c..87728cf62c 100644 --- a/lib/str_endswith.c +++ b/lib/str_endswith.c @@ -22,7 +22,7 @@ #include <string.h> -int +bool str_endswith (const char *string, const char *suffix) { size_t len = strlen (string); diff --git a/lib/str_startswith.c b/lib/str_startswith.c index 381236ec2a..ff7ab59584 100644 --- a/lib/str_startswith.c +++ b/lib/str_startswith.c @@ -22,7 +22,7 @@ #include <string.h> -int +bool str_startswith (const char *string, const char *prefix) { return strncmp (string, prefix, strlen (prefix)) == 0; diff --git a/lib/string.in.h b/lib/string.in.h index 6b9e342a1a..ce48829900 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -1080,7 +1080,7 @@ _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - " #if @GNULIB_STR_STARTSWITH@ /* Returns true if STRING starts with PREFIX. Returns false otherwise. */ -_GL_EXTERN_C int str_startswith (const char *string, const char *prefix) +_GL_EXTERN_C bool str_startswith (const char *string, const char *prefix) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); #endif @@ -1088,7 +1088,7 @@ _GL_EXTERN_C int str_startswith (const char *string, const char *prefix) #if @GNULIB_STR_ENDSWITH@ /* Returns true if STRING ends with SUFFIX. Returns false otherwise. */ -_GL_EXTERN_C int str_endswith (const char *string, const char *prefix) +_GL_EXTERN_C bool str_endswith (const char *string, const char *prefix) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); #endif @@ -1320,7 +1320,7 @@ _GL_EXTERN_C char * mbstok_r (char *restrict string, const char *delim, #if @GNULIB_MBS_STARTSWITH@ /* Returns true if STRING starts with PREFIX. Returns false otherwise. */ -_GL_EXTERN_C int mbs_startswith (const char *string, const char *prefix) +_GL_EXTERN_C bool mbs_startswith (const char *string, const char *prefix) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); /* No extra code is needed for multibyte locales for this function. */ @@ -1332,7 +1332,7 @@ _GL_EXTERN_C int mbs_startswith (const char *string, const char *prefix) Returns false otherwise. Unlike str_endswith(), this function works correctly in multibyte locales. */ -_GL_EXTERN_C int mbs_endswith (const char *string, const char *suffix) +_GL_EXTERN_C bool mbs_endswith (const char *string, const char *suffix) _GL_ATTRIBUTE_PURE _GL_ARG_NONNULL ((1, 2)); #endif diff --git a/modules/mbs_endswith b/modules/mbs_endswith index 19ead99cd5..73ef26c1f5 100644 --- a/modules/mbs_endswith +++ b/modules/mbs_endswith @@ -6,6 +6,7 @@ lib/mbs_endswith.c Depends-on: string-h +bool str_endswith mbslen mbiter diff --git a/modules/mbs_startswith b/modules/mbs_startswith index b7145de034..2e909c67e6 100644 --- a/modules/mbs_startswith +++ b/modules/mbs_startswith @@ -5,6 +5,7 @@ Files: Depends-on: string-h +bool str_startswith configure.ac: diff --git a/modules/str_endswith b/modules/str_endswith index c04b2c8eb0..aceb295ca8 100644 --- a/modules/str_endswith +++ b/modules/str_endswith @@ -6,6 +6,7 @@ lib/str_endswith.c Depends-on: string-h +bool configure.ac: gl_STRING_MODULE_INDICATOR([str_endswith]) diff --git a/modules/str_startswith b/modules/str_startswith index 4b1754e341..535449c40a 100644 --- a/modules/str_startswith +++ b/modules/str_startswith @@ -6,6 +6,7 @@ lib/str_startswith.c Depends-on: string-h +bool configure.ac: gl_STRING_MODULE_INDICATOR([str_startswith])