From d4241f52d1a19bf464d63cbc4cd67fcc6a3af01d Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 22 May 2014 05:30:14 -0400 Subject: strbuf: add strbuf_reencode helper This is a convenience wrapper around `reencode_string_len` and `strbuf_attach`. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- strbuf.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index ee96dcfb81..fc7290f57a 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) { @@ -106,6 +107,22 @@ void strbuf_ltrim(struct strbuf *sb) sb->buf[sb->len] = '\0'; } +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) { -- cgit v1.2.3