strbuf: add strbuf_reencode helper
authorJeff King <peff@peff.net>
Thu, 22 May 2014 09:30:14 +0000 (05:30 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 27 May 2014 16:59:21 +0000 (09:59 -0700)
This is a convenience wrapper around `reencode_string_len`
and `strbuf_attach`.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/technical/api-strbuf.txt
strbuf.c
strbuf.h
index 3350d97dda2408f92dc44c1e3216facc50257a50..9d28b034ad9a35108f8069557e194992f8e36b0f 100644 (file)
@@ -125,6 +125,11 @@ Functions
 
        Strip whitespace from the end of a string.
 
+`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
index ee96dcfb816625436582833d812a7156513d5d39..fc7290f57a07353ab7bbf86b7c743ec913ee396c 100644 (file)
--- 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)
 {
index 39c14cfa384c5154abd96899a7c02417295f8555..4e9a2f88686e211b8610d72e18c02251c2dc8a92 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -45,6 +45,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
 extern void strbuf_trim(struct strbuf *);
 extern void strbuf_rtrim(struct strbuf *);
 extern void strbuf_ltrim(struct strbuf *);
+extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
 extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
 
 /*