diff options
author | Jeff King <peff@peff.net> | 2014-06-30 12:57:51 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-06-30 13:43:16 -0700 |
commit | 35480f0b23f2c1824109ddae24392a70d19c6b9c (patch) | |
tree | ee2080bb4c9813223cacd5ef0287d437eb00058f /t/t4151-am-abort.sh | |
parent | sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one() (diff) | |
download | tgif-35480f0b23f2c1824109ddae24392a70d19c6b9c.tar.xz |
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 't/t4151-am-abort.sh')
0 files changed, 0 insertions, 0 deletions