[PATCH 8/9] strbuf: add strbuf_reencode helper
- From
Jeff King <peff@peff.net>
- Date
- May 21, 2014, 10:33 UTC
- Message-ID
- <20140521103324.GH30464@sigill.intra.peff.net>
- In-Reply-To
- <20140521102524.GA30301@sigill.intra.peff.net>
This is a convenience wrapper around `reencode_string_len` and `strbuf_attach`.
Signed-off-by: Jeff King <peff@peff.net> --- Documentation/technical/api-strbuf.txt | 5 +++++ strbuf.c | 17 +++++++++++++++++ strbuf.h | 1 + 3 files changed, 23 insertions(+)
diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt index 8480f89..a468816 100644 --- a/Documentation/technical/api-strbuf.txt +++ b/Documentation/technical/api-strbuf.txt @@ -129,6 +129,11 @@ Functions Lowercase each character in the buffer using `tolower`. +`strbuf_reencode`:: + + Replace the contents of the strbuf with a reencoded form. Returns -1 + on error, 0 on success. + `strbuf_cmp`:: Compare two buffers. Returns an integer less than, equal to, or greater diff --git a/strbuf.c b/strbuf.c index d289d1a..a717d9b 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,5 +1,6 @@ #include "cache.h" #include "refs.h" +#include "utf8.h" int starts_with(const char *str, const char *prefix) { @@ -113,6 +114,22 @@ void strbuf_tolower(struct strbuf *sb) sb->buf[i] = tolower(sb->buf[i]); } +int strbuf_reencode(struct strbuf *sb, const char *from, const char *to) +{ + char *out; + int len; + + if (same_encoding(from, to)) + return 0; + + out = reencode_string_len(sb->buf, sb->len, to, from, &len); + if (!out) + return -1; + + strbuf_attach(sb, out, len, len); + return 0; +} + struct strbuf **strbuf_split_buf(const char *str, size_t slen, int terminator, int max) { diff --git a/strbuf.h b/strbuf.h index 25328b9..a068dd6 100644 --- a/strbuf.h +++ b/strbuf.h @@ -46,6 +46,7 @@ extern void strbuf_trim(struct strbuf *); extern void strbuf_rtrim(struct strbuf *); extern void strbuf_ltrim(struct strbuf *); extern void strbuf_tolower(struct strbuf *sb); +extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to); extern int strbuf_cmp(const struct strbuf *, const struct strbuf *); /*
-- 2.0.0.rc1.436.g03cb729