Skip to content

Commit 5ce9702

Browse files
committed
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
2 parents 97e14d9 + f006e03 commit 5ce9702

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

Documentation/CodingGuidelines

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ For C programs:
298298
. since late 2021 with 44ba10d6, we have had variables declared in
299299
the for loop "for (int i = 0; i < 10; i++)".
300300

301+
. since late 2023 with 8277dbe987 we have been using the bool type
302+
from <stdbool.h>.
303+
301304
New C99 features that we cannot use yet:
302305

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

git-compat-util.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,35 +897,35 @@ static inline size_t xsize_t(off_t len)
897897
* is done via tolower(), so it is strictly ASCII (no multi-byte characters or
898898
* locale-specific conversions).
899899
*/
900-
static inline int skip_iprefix(const char *str, const char *prefix,
900+
static inline bool skip_iprefix(const char *str, const char *prefix,
901901
const char **out)
902902
{
903903
do {
904904
if (!*prefix) {
905905
*out = str;
906-
return 1;
906+
return true;
907907
}
908908
} while (tolower(*str++) == tolower(*prefix++));
909-
return 0;
909+
return false;
910910
}
911911

912912
/*
913913
* Like skip_prefix_mem, but compare case-insensitively. Note that the
914914
* comparison is done via tolower(), so it is strictly ASCII (no multi-byte
915915
* characters or locale-specific conversions).
916916
*/
917-
static inline int skip_iprefix_mem(const char *buf, size_t len,
917+
static inline bool skip_iprefix_mem(const char *buf, size_t len,
918918
const char *prefix,
919919
const char **out, size_t *outlen)
920920
{
921921
do {
922922
if (!*prefix) {
923923
*out = buf;
924924
*outlen = len;
925-
return 1;
925+
return true;
926926
}
927927
} while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
928-
return 0;
928+
return false;
929929
}
930930

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

strbuf.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,55 @@
88
#include "utf8.h"
99
#include "date.h"
1010

11-
int starts_with(const char *str, const char *prefix)
11+
bool starts_with(const char *str, const char *prefix)
1212
{
1313
for (; ; str++, prefix++)
1414
if (!*prefix)
15-
return 1;
15+
return true;
1616
else if (*str != *prefix)
17-
return 0;
17+
return false;
1818
}
1919

20-
int istarts_with(const char *str, const char *prefix)
20+
bool istarts_with(const char *str, const char *prefix)
2121
{
2222
for (; ; str++, prefix++)
2323
if (!*prefix)
24-
return 1;
24+
return true;
2525
else if (tolower(*str) != tolower(*prefix))
26-
return 0;
26+
return false;
2727
}
2828

29-
int starts_with_mem(const char *str, size_t len, const char *prefix)
29+
bool starts_with_mem(const char *str, size_t len, const char *prefix)
3030
{
3131
const char *end = str + len;
3232
for (; ; str++, prefix++) {
3333
if (!*prefix)
34-
return 1;
34+
return true;
3535
else if (str == end || *str != *prefix)
36-
return 0;
36+
return false;
3737
}
3838
}
3939

40-
int skip_to_optional_arg_default(const char *str, const char *prefix,
40+
bool skip_to_optional_arg_default(const char *str, const char *prefix,
4141
const char **arg, const char *def)
4242
{
4343
const char *p;
4444

4545
if (!skip_prefix(str, prefix, &p))
46-
return 0;
46+
return false;
4747

4848
if (!*p) {
4949
if (arg)
5050
*arg = def;
51-
return 1;
51+
return true;
5252
}
5353

5454
if (*p != '=')
55-
return 0;
55+
return false;
5656

5757
if (arg)
5858
*arg = p + 1;
59-
return 1;
59+
return true;
6060
}
6161

6262
/*

strbuf.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ char *xstrvfmt(const char *fmt, va_list ap);
660660
__attribute__((format (printf, 1, 2)))
661661
char *xstrfmt(const char *fmt, ...);
662662

663-
int starts_with(const char *str, const char *prefix);
664-
int istarts_with(const char *str, const char *prefix);
665-
int starts_with_mem(const char *str, size_t len, const char *prefix);
663+
bool starts_with(const char *str, const char *prefix);
664+
bool istarts_with(const char *str, const char *prefix);
665+
bool starts_with_mem(const char *str, size_t len, const char *prefix);
666666

667667
/*
668668
* 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);
678678
* can be used instead of !strcmp(arg, "--key") and then
679679
* skip_prefix(arg, "--key=", &arg) to parse such an option.
680680
*/
681-
int skip_to_optional_arg_default(const char *str, const char *prefix,
681+
bool skip_to_optional_arg_default(const char *str, const char *prefix,
682682
const char **arg, const char *def);
683683

684-
static inline int skip_to_optional_arg(const char *str, const char *prefix,
684+
static inline bool skip_to_optional_arg(const char *str, const char *prefix,
685685
const char **arg)
686686
{
687687
return skip_to_optional_arg_default(str, prefix, arg, "");
688688
}
689689

690-
static inline int ends_with(const char *str, const char *suffix)
690+
static inline bool ends_with(const char *str, const char *suffix)
691691
{
692692
size_t len;
693693
return strip_suffix(str, suffix, &len);

0 commit comments

Comments
 (0)