summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2014-07-09 11:33:27 -0700
committerJunio C Hamano <gitster@pobox.com>2014-07-09 11:33:28 -0700
commite91ae32a01ffe294b8510c1d8cd7138493a0712f (patch)
tree461b9dacf6c1e8adf5f59251af093dc7c407091f /git-compat-util.h
parent72c779457cd72928e36f2bc43c3ff7f3ae7b77c3 (diff)
parent67a31f612830e79fe768ac886ed9ef7eadd8fb10 (diff)
downloadgit-e91ae32a01ffe294b8510c1d8cd7138493a0712f.tar.gz
Merge branch 'jk/skip-prefix'
* jk/skip-prefix: http-push: refactor parsing of remote object names imap-send: use skip_prefix instead of using magic numbers use skip_prefix to avoid repeated calculations git: avoid magic number with skip_prefix fetch-pack: refactor parsing in get_ack fast-import: refactor parsing of spaces stat_opt: check extra strlen call daemon: use skip_prefix to avoid magic numbers fast-import: use skip_prefix for parsing input use skip_prefix to avoid repeating strings use skip_prefix to avoid magic numbers transport-helper: avoid reading past end-of-string fast-import: fix read of uninitialized argv memory apply: use skip_prefix instead of raw addition refactor skip_prefix to return a boolean avoid using skip_prefix as a boolean daemon: mark some strings as const parse_diff_color_slot: drop ofs parameter
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h27
1 files changed, 23 insertions, 4 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index 96f55547a3..9de3180710 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -349,13 +349,32 @@ extern void set_die_is_recursing_routine(int (*routine)(void));
extern int starts_with(const char *str, const char *prefix);
extern int ends_with(const char *str, const char *suffix);
-static inline const char *skip_prefix(const char *str, const char *prefix)
+/*
+ * If the string "str" begins with the string found in "prefix", return 1.
+ * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
+ * the string right after the prefix).
+ *
+ * Otherwise, return 0 and leave "out" untouched.
+ *
+ * Examples:
+ *
+ * [extract branch name, fail if not a branch]
+ * if (!skip_prefix(ref, "refs/heads/", &branch)
+ * return -1;
+ *
+ * [skip prefix if present, otherwise use whole string]
+ * skip_prefix(name, "refs/heads/", &name);
+ */
+static inline int skip_prefix(const char *str, const char *prefix,
+ const char **out)
{
do {
- if (!*prefix)
- return str;
+ if (!*prefix) {
+ *out = str;
+ return 1;
+ }
} while (*str++ == *prefix++);
- return NULL;
+ return 0;
}
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)