summaryrefslogtreecommitdiff
path: root/git-compat-util.h
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-06-30 16:57:51 (GMT)
committerJunio C Hamano <gitster@pobox.com>2014-06-30 20:43:16 (GMT)
commit35480f0b23f2c1824109ddae24392a70d19c6b9c (patch)
treeee2080bb4c9813223cacd5ef0287d437eb00058f /git-compat-util.h
parent880fb8de67af3aeeae5a59ba5ce84ad3d0cf1dda (diff)
downloadgit-35480f0b23f2c1824109ddae24392a70d19c6b9c.zip
git-35480f0b23f2c1824109ddae24392a70d19c6b9c.tar.gz
git-35480f0b23f2c1824109ddae24392a70d19c6b9c.tar.bz2
add strip_suffix function
Many callers of ends_with want to not only find out whether a string has a suffix, but want to also strip it off. Doing that separately has two minor problems: 1. We often run over the string twice (once to find the suffix, and then once more to find its length to subtract the suffix length). 2. We have to specify the suffix length again, which means either a magic number, or repeating ourselves with strlen("suffix"). Just as we have skip_prefix to avoid these cases with starts_with, we can add a strip_suffix to avoid them with ends_with. Note that we add two forms of strip_suffix here: one that takes a string, with the resulting length as an out-parameter; and one that takes a pointer/length pair, and reuses the length as an out-parameter. The latter is more efficient when the caller already has the length (e.g., when using strbufs), but it can be easy to confuse the two, as they take the same number and types of parameters. For that reason, the "mem" form puts its length parameter next to the buffer (since they are a pair), and the string form puts it at the end (since it is an out-parameter). The compiler can notice when you get the order wrong, which should help prevent writing one when you meant the other. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r--git-compat-util.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h
index e6a4159..4c451b5 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -350,6 +350,33 @@ static inline const char *skip_prefix(const char *str, const char *prefix)
return NULL;
}
+/*
+ * If buf ends with suffix, return 1 and subtract the length of the suffix
+ * from *len. Otherwise, return 0 and leave *len untouched.
+ */
+static inline int strip_suffix_mem(const char *buf, size_t *len,
+ const char *suffix)
+{
+ size_t suflen = strlen(suffix);
+ if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
+ return 0;
+ *len -= suflen;
+ return 1;
+}
+
+/*
+ * If str ends with suffix, return 1 and set *len to the size of the string
+ * without the suffix. Otherwise, return 0 and set *len to the size of the
+ * string.
+ *
+ * Note that we do _not_ NUL-terminate str to the new length.
+ */
+static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
+{
+ *len = strlen(str);
+ return strip_suffix_mem(str, len, suffix);
+}
+
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ