From fda5d9595d5172fcbba34742e92d6c7ed4cbe5ef Mon Sep 17 00:00:00 2001 From: Calvin Wan Date: Wed, 5 Jul 2023 17:09:19 +0000 Subject: git-compat-util: move strbuf.c funcs to its header While functions like starts_with() probably should not belong in the boundaries of the strbuf library, this commit focuses on first splitting out headers from git-compat-util.h. Signed-off-by: Calvin Wan Signed-off-by: Junio C Hamano --- strbuf.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h index 507670ce3c..a2544484f8 100644 --- a/strbuf.h +++ b/strbuf.h @@ -735,4 +735,36 @@ char *xstrvfmt(const char *fmt, va_list ap); __attribute__((format (printf, 1, 2))) char *xstrfmt(const char *fmt, ...); +int starts_with(const char *str, const char *prefix); +int istarts_with(const char *str, const char *prefix); + +/* + * If the string "str" is the same as the string in "prefix", then the "arg" + * parameter is set to the "def" parameter and 1 is returned. + * If the string "str" begins with the string found in "prefix" and then a + * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1" + * (i.e., to the point in the string right after the prefix and the "=" sign), + * and 1 is returned. + * + * Otherwise, return 0 and leave "arg" untouched. + * + * When we accept both a "--key" and a "--key=" option, this function + * can be used instead of !strcmp(arg, "--key") and then + * skip_prefix(arg, "--key=", &arg) to parse such an option. + */ +int skip_to_optional_arg_default(const char *str, const char *prefix, + const char **arg, const char *def); + +static inline int skip_to_optional_arg(const char *str, const char *prefix, + const char **arg) +{ + return skip_to_optional_arg_default(str, prefix, arg, ""); +} + +static inline int ends_with(const char *str, const char *suffix) +{ + size_t len; + return strip_suffix(str, suffix, &len); +} + #endif /* STRBUF_H */ -- cgit v1.2.3