Re: [PATCH 2/6] strbuf: add string-chomping functions
- From
Michael Haggerty <mhagger@alum.mit.edu>
- Date
- Jan 29, 2013, 10:15 UTC
- Message-ID
- <5107A146.4000309@alum.mit.edu>
- In-Reply-To
- <20130129091540.GB9999@sigill.intra.peff.net>
On 01/29/2013 10:15 AM, Jeff King wrote:
Show 46 quoted lines
> Sometimes it is handy to cut a trailing string off the end
> of a strbuf (e.g., a file extension). These helper functions
> make it a one-liner.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> strbuf.c | 11 +++++++++++
> strbuf.h | 2 ++
> 2 files changed, 13 insertions(+)
>
> diff --git a/strbuf.c b/strbuf.c
> index 9a373be..8199ced 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -106,6 +106,17 @@ void strbuf_ltrim(struct strbuf *sb)
> sb->buf[sb->len] = '\0';
> }
>
> +void strbuf_chompmem(struct strbuf *sb, const void *data, size_t len)
> +{
> + if (sb->len >= len && !memcmp(data, sb->buf + sb->len - len, len))
> + strbuf_setlen(sb, sb->len - len);
> +}
> +
> +void strbuf_chompstr(struct strbuf *sb, const char *str)
> +{
> + strbuf_chompmem(sb, str, strlen(str));
> +}
> +
> struct strbuf **strbuf_split_buf(const char *str, size_t slen,
> int terminator, int max)
> {
> diff --git a/strbuf.h b/strbuf.h
> index ecae4e2..3aeb815 100644
> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -42,6 +42,8 @@ extern void strbuf_ltrim(struct strbuf *);
> extern void strbuf_trim(struct strbuf *);
> extern void strbuf_rtrim(struct strbuf *);
> extern void strbuf_ltrim(struct strbuf *);
> +extern void strbuf_chompmem(struct strbuf *, const void *, size_t);
> +extern void strbuf_chompstr(struct strbuf *, const char *);
> extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
>
> /*
> It might be handy to have these functions return true/false based on whether the suffix was actually found.
Please document the new functions in Documentation/technical/api-strbuf.txt. Personally I would also advocate a "docstring" in the header file, but obviously that preference is the exception rather than the rule in the git project :-(
Michael
-- Michael Haggerty mhagger@alum.mit.edu http://softwareswirl.blogspot.com/