git-compat-util: convert skip_{prefix,suffix}{,_mem} to bool

Use the data type bool and its values true and false to document the binary return value of skip_prefix() and friends more explicitly. This first use of stdbool.h, introduced with C99, is meant to check whether there are platforms that claim support for C99, as tested by 7bc341e21b (git-compat-util: add a test balloon for C99 support, 2021-12-01), but still lack that header for some reason. A fallback based on a wider type, e.g. int, would have to deal with comparisons somehow to emulate that any non-zero value is true: bool b1 = 1; bool b2 = 2; if (b1 == b2) puts("This is true."); int i1 = 1; int i2 = 2; if (i1 == i2) puts("Not printed."); #define BOOLEQ(a, b) (!(a) == !(b)) if (BOOLEQ(i1, i2)) puts("This is true."); So we'd be better off using bool everywhere without a fallback, if possible. That's why this patch doesn't include any. Signed-off-by: René Scharfe <l.s.r@web.de> Acked-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Dec 16, 2023 at 11:47 UTC 8277dbe9872205be1588ddfbf01d5439847db1d9
1 file changed +22 -20
git-compat-util.h
+22 -20
@@ -225,6 +225,7 @@ struct strbuf;
225 #include <stddef.h>
226 #include <stdlib.h>
227 #include <stdarg.h>
228 +#include <stdbool.h>
229 #include <string.h>
230 #ifdef HAVE_STRINGS_H
231 #include <strings.h> /* for strcasecmp() */
@@ -684,11 +685,11 @@ report_fn get_warn_routine(void);
685 void set_die_is_recursing_routine(int (*routine)(void));
686
687 /*
687 - * If the string "str" begins with the string found in "prefix", return 1.
688 + * If the string "str" begins with the string found in "prefix", return true.
689 * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
690 * the string right after the prefix).
691 *
691 - * Otherwise, return 0 and leave "out" untouched.
692 + * Otherwise, return false and leave "out" untouched.
693 *
694 * Examples:
695 *
@@ -699,57 +700,58 @@ void set_die_is_recursing_routine(int (*routine)(void));
700 * [skip prefix if present, otherwise use whole string]
701 * skip_prefix(name, "refs/heads/", &name);
702 */
702 -static inline int skip_prefix(const char *str, const char *prefix,
703 - const char **out)
703 +static inline bool skip_prefix(const char *str, const char *prefix,
704 + const char **out)
705 {
706 do {
707 if (!*prefix) {
708 *out = str;
708 - return 1;
709 + return true;
710 }
711 } while (*str++ == *prefix++);
711 - return 0;
712 + return false;
713 }
714
715 /*
716 * Like skip_prefix, but promises never to read past "len" bytes of the input
717 * buffer, and returns the remaining number of bytes in "out" via "outlen".
718 */
718 -static inline int skip_prefix_mem(const char *buf, size_t len,
719 - const char *prefix,
720 - const char **out, size_t *outlen)
719 +static inline bool skip_prefix_mem(const char *buf, size_t len,
720 + const char *prefix,
721 + const char **out, size_t *outlen)
722 {
723 size_t prefix_len = strlen(prefix);
724 if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
725 *out = buf + prefix_len;
726 *outlen = len - prefix_len;
726 - return 1;
727 + return true;
728 }
728 - return 0;
729 + return false;
730 }
731
732 /*
732 - * If buf ends with suffix, return 1 and subtract the length of the suffix
733 - * from *len. Otherwise, return 0 and leave *len untouched.
733 + * If buf ends with suffix, return true and subtract the length of the suffix
734 + * from *len. Otherwise, return false and leave *len untouched.
735 */
735 -static inline int strip_suffix_mem(const char *buf, size_t *len,
736 - const char *suffix)
736 +static inline bool strip_suffix_mem(const char *buf, size_t *len,
737 + const char *suffix)
738 {
739 size_t suflen = strlen(suffix);
740 if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
740 - return 0;
741 + return false;
742 *len -= suflen;
742 - return 1;
743 + return true;
744 }
745
746 /*
746 - * If str ends with suffix, return 1 and set *len to the size of the string
747 - * without the suffix. Otherwise, return 0 and set *len to the size of the
747 + * If str ends with suffix, return true and set *len to the size of the string
748 + * without the suffix. Otherwise, return false and set *len to the size of the
749 * string.
750 *
751 * Note that we do _not_ NUL-terminate str to the new length.
752 */
752 -static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
753 +static inline bool strip_suffix(const char *str, const char *suffix,
754 + size_t *len)
755 {
756 *len = strlen(str);
757 return strip_suffix_mem(str, len, suffix);