strbuf: add strbuf_reencode helper
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>
Jeff King committed
May 22, 2014 at 05:30 UTC
d4241f52d1a19bf464d63cbc4cd67fcc6a3af01d
3 files changed
+23
Documentation/technical/api-strbuf.txt
+5
@@ -125,6 +125,11 @@ Functions
125
126
Strip whitespace from the end of a string.
127
128
+`strbuf_reencode`::
129
+
130
+ Replace the contents of the strbuf with a reencoded form. Returns -1
131
+ on error, 0 on success.
132
+
133
`strbuf_cmp`::
134
135
Compare two buffers. Returns an integer less than, equal to, or greater
strbuf.c
+17
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "refs.h"
3
+#include "utf8.h"
4
5
int starts_with(const char *str, const char *prefix)
6
{
@@ -106,6 +107,22 @@ void strbuf_ltrim(struct strbuf *sb)
107
sb->buf[sb->len] = '\0';
108
}
109
110
+int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
111
+{
112
+ char *out;
113
+ int len;
114
+
115
+ if (same_encoding(from, to))
116
+ return 0;
117
+
118
+ out = reencode_string_len(sb->buf, sb->len, to, from, &len);
119
+ if (!out)
120
+ return -1;
121
+
122
+ strbuf_attach(sb, out, len, len);
123
+ return 0;
124
+}
125
+
126
struct strbuf **strbuf_split_buf(const char *str, size_t slen,
127
int terminator, int max)
128
{
strbuf.h
+1
@@ -45,6 +45,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
45
extern void strbuf_trim(struct strbuf *);
46
extern void strbuf_rtrim(struct strbuf *);
47
extern void strbuf_ltrim(struct strbuf *);
48
+extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
49
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
50
51
/*