[PATCH 2/6] strbuf: add string-chomping functions
- From
Jeff King <peff@peff.net>
- Date
- Jan 29, 2013, 09:15 UTC
- Message-ID
- <20130129091540.GB9999@sigill.intra.peff.net>
- In-Reply-To
- <20130129091434.GA6975@sigill.intra.peff.net>
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 *); /*
-- 1.8.0.2.16.g72e2fc9