add strip_suffix function
commit35480f0b23f2c1824109ddae24392a70d19c6b9c
authorJeff King <peff@peff.net>
Mon, 30 Jun 2014 16:57:51 +0000 (30 12:57 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Jun 2014 20:43:16 +0000 (30 13:43 -0700)
treeee2080bb4c9813223cacd5ef0287d437eb00058f
parent880fb8de67af3aeeae5a59ba5ce84ad3d0cf1dda
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>
git-compat-util.h