Merge branch 'pw/adopt-c99-bool-officially'

Declare weather-balloon we raised for "bool" type 18 months ago a
success and officially allow using the type in our codebase.

* pw/adopt-c99-bool-officially:
  strbuf: convert predicates to return bool
  git-compat-util: convert string predicates to return bool
  CodingGuidelines: allow the use of bool
maint
Junio C Hamano 2025-07-24 16:03:55 -07:00
commit 5ce97021dd
4 changed files with 29 additions and 26 deletions

View File

@ -298,6 +298,9 @@ For C programs:
. since late 2021 with 44ba10d6, we have had variables declared in . since late 2021 with 44ba10d6, we have had variables declared in
the for loop "for (int i = 0; i < 10; i++)". the for loop "for (int i = 0; i < 10; i++)".


. since late 2023 with 8277dbe987 we have been using the bool type
from <stdbool.h>.

New C99 features that we cannot use yet: New C99 features that we cannot use yet:


. %z and %zu as a printf() argument for a size_t (the %z being for . %z and %zu as a printf() argument for a size_t (the %z being for

View File

@ -897,16 +897,16 @@ static inline size_t xsize_t(off_t len)
* is done via tolower(), so it is strictly ASCII (no multi-byte characters or * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
* locale-specific conversions). * locale-specific conversions).
*/ */
static inline int skip_iprefix(const char *str, const char *prefix, static inline bool skip_iprefix(const char *str, const char *prefix,
const char **out) const char **out)
{ {
do { do {
if (!*prefix) { if (!*prefix) {
*out = str; *out = str;
return 1; return true;
} }
} while (tolower(*str++) == tolower(*prefix++)); } while (tolower(*str++) == tolower(*prefix++));
return 0; return false;
} }


/* /*
@ -914,7 +914,7 @@ static inline int skip_iprefix(const char *str, const char *prefix,
* comparison is done via tolower(), so it is strictly ASCII (no multi-byte * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
* characters or locale-specific conversions). * characters or locale-specific conversions).
*/ */
static inline int skip_iprefix_mem(const char *buf, size_t len, static inline bool skip_iprefix_mem(const char *buf, size_t len,
const char *prefix, const char *prefix,
const char **out, size_t *outlen) const char **out, size_t *outlen)
{ {
@ -922,10 +922,10 @@ static inline int skip_iprefix_mem(const char *buf, size_t len,
if (!*prefix) { if (!*prefix) {
*out = buf; *out = buf;
*outlen = len; *outlen = len;
return 1; return true;
} }
} while (len-- > 0 && tolower(*buf++) == tolower(*prefix++)); } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
return 0; return false;
} }


static inline int strtoul_ui(char const *s, int base, unsigned int *result) static inline int strtoul_ui(char const *s, int base, unsigned int *result)

View File

@ -8,55 +8,55 @@
#include "utf8.h" #include "utf8.h"
#include "date.h" #include "date.h"


int starts_with(const char *str, const char *prefix) bool starts_with(const char *str, const char *prefix)
{ {
for (; ; str++, prefix++) for (; ; str++, prefix++)
if (!*prefix) if (!*prefix)
return 1; return true;
else if (*str != *prefix) else if (*str != *prefix)
return 0; return false;
} }


int istarts_with(const char *str, const char *prefix) bool istarts_with(const char *str, const char *prefix)
{ {
for (; ; str++, prefix++) for (; ; str++, prefix++)
if (!*prefix) if (!*prefix)
return 1; return true;
else if (tolower(*str) != tolower(*prefix)) else if (tolower(*str) != tolower(*prefix))
return 0; return false;
} }


int starts_with_mem(const char *str, size_t len, const char *prefix) bool starts_with_mem(const char *str, size_t len, const char *prefix)
{ {
const char *end = str + len; const char *end = str + len;
for (; ; str++, prefix++) { for (; ; str++, prefix++) {
if (!*prefix) if (!*prefix)
return 1; return true;
else if (str == end || *str != *prefix) else if (str == end || *str != *prefix)
return 0; return false;
} }
} }


int skip_to_optional_arg_default(const char *str, const char *prefix, bool skip_to_optional_arg_default(const char *str, const char *prefix,
const char **arg, const char *def) const char **arg, const char *def)
{ {
const char *p; const char *p;


if (!skip_prefix(str, prefix, &p)) if (!skip_prefix(str, prefix, &p))
return 0; return false;


if (!*p) { if (!*p) {
if (arg) if (arg)
*arg = def; *arg = def;
return 1; return true;
} }


if (*p != '=') if (*p != '=')
return 0; return false;


if (arg) if (arg)
*arg = p + 1; *arg = p + 1;
return 1; return true;
} }


/* /*

View File

@ -660,9 +660,9 @@ char *xstrvfmt(const char *fmt, va_list ap);
__attribute__((format (printf, 1, 2))) __attribute__((format (printf, 1, 2)))
char *xstrfmt(const char *fmt, ...); char *xstrfmt(const char *fmt, ...);


int starts_with(const char *str, const char *prefix); bool starts_with(const char *str, const char *prefix);
int istarts_with(const char *str, const char *prefix); bool istarts_with(const char *str, const char *prefix);
int starts_with_mem(const char *str, size_t len, const char *prefix); bool starts_with_mem(const char *str, size_t len, const char *prefix);


/* /*
* If the string "str" is the same as the string in "prefix", then the "arg" * If the string "str" is the same as the string in "prefix", then the "arg"
@ -678,16 +678,16 @@ int starts_with_mem(const char *str, size_t len, const char *prefix);
* can be used instead of !strcmp(arg, "--key") and then * can be used instead of !strcmp(arg, "--key") and then
* skip_prefix(arg, "--key=", &arg) to parse such an option. * skip_prefix(arg, "--key=", &arg) to parse such an option.
*/ */
int skip_to_optional_arg_default(const char *str, const char *prefix, bool skip_to_optional_arg_default(const char *str, const char *prefix,
const char **arg, const char *def); const char **arg, const char *def);


static inline int skip_to_optional_arg(const char *str, const char *prefix, static inline bool skip_to_optional_arg(const char *str, const char *prefix,
const char **arg) const char **arg)
{ {
return skip_to_optional_arg_default(str, prefix, arg, ""); return skip_to_optional_arg_default(str, prefix, arg, "");
} }


static inline int ends_with(const char *str, const char *suffix) static inline bool ends_with(const char *str, const char *suffix)
{ {
size_t len; size_t len;
return strip_suffix(str, suffix, &len); return strip_suffix(str, suffix, &len);