[PATCH 7/9] strbuf: implement strbuf_strip_suffix
From: Jeff King <hidden>
Date: 2016-06-15 23:01:46
Subsystem:
the rest · Maintainer:
Linus Torvalds
You can almost get away with just calling "strip_suffix_mem" on a strbuf's buf and len fields. But we also need to move the NUL-terminator to satisfy strbuf's invariants. Let's provide a convenience wrapper that handles this. Signed-off-by: Jeff King <redacted> --- I called strbuf_setlen here because it seemed sensible to use that as an opaque building block, but we are violating the invariant here for a moment by setting sb->len for a moment. I think that is fine, as this is a strbuf function, and can violate the invariant for a moment if it wants to. But if we care, we can keep a separate "size_t len" variable, and strbuf_setlen to that. Or we can make it less opaque, and replace the setlen with "sb->buf[sb->len] = 0". strbuf.h | 9 +++++++++ 1 file changed, 9 insertions(+)
diff --git a/strbuf.h b/strbuf.h
index e9ad03e..ec11742 100644
--- a/strbuf.h
+++ b/strbuf.h@@ -49,6 +49,15 @@ extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to); extern void strbuf_tolower(struct strbuf *sb); extern int strbuf_cmp(const struct strbuf *, const struct strbuf *); +static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) +{ + if (strip_suffix_mem(sb->buf, &sb->len, suffix)) { + strbuf_setlen(sb, sb->len); + return 1; + } else + return 0; +} + /* * Split str (of length slen) at the specified terminator character. * Return a null-terminated array of pointers to strbuf objects
--
2.0.0.566.gfe3e6b2