diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-12-28 23:08:46 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-12-28 23:08:46 +0100 |
commit | f427b94985f7f8cde46df20f644760adc0ca6735 (patch) | |
tree | 8b36ce0f3145357c9ae9fef1a82f035a0494247e /strbuf.c | |
parent | Merge branch 'ra/prompt-eread-fix' (diff) | |
parent | t4045: reindent to make helpers readable (diff) | |
download | git-f427b94985f7f8cde46df20f644760adc0ca6735.tar.xz git-f427b94985f7f8cde46df20f644760adc0ca6735.zip |
Merge branch 'cc/skip-to-optional-val'
Introduce a helper to simplify code to parse a common pattern that
expects either "--key" or "--key=<something>".
* cc/skip-to-optional-val:
t4045: reindent to make helpers readable
diff: add tests for --relative without optional prefix value
diff: use skip_to_optional_arg_default() in parsing --relative
diff: use skip_to_optional_arg_default()
diff: use skip_to_optional_arg()
index-pack: use skip_to_optional_arg()
git-compat-util: introduce skip_to_optional_arg()
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -11,6 +11,28 @@ int starts_with(const char *str, const char *prefix) return 0; } +int skip_to_optional_arg_default(const char *str, const char *prefix, + const char **arg, const char *def) +{ + const char *p; + + if (!skip_prefix(str, prefix, &p)) + return 0; + + if (!*p) { + if (arg) + *arg = def; + return 1; + } + + if (*p != '=') + return 0; + + if (arg) + *arg = p + 1; + return 1; +} + /* * Used as the default ->buf value, so that people can always assume * buf is non NULL and ->buf is NUL terminated even for a freshly |