Skip to content

Commit d4241f5

Browse files
peffgitster
authored andcommitted
strbuf: add strbuf_reencode helper
This is a convenience wrapper around `reencode_string_len` and `strbuf_attach`. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e313162 commit d4241f5

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Documentation/technical/api-strbuf.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ Functions
125125

126126
Strip whitespace from the end of a string.
127127

128+
`strbuf_reencode`::
129+
130+
Replace the contents of the strbuf with a reencoded form. Returns -1
131+
on error, 0 on success.
132+
128133
`strbuf_cmp`::
129134

130135
Compare two buffers. Returns an integer less than, equal to, or greater

strbuf.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "refs.h"
3+
#include "utf8.h"
34

45
int starts_with(const char *str, const char *prefix)
56
{
@@ -106,6 +107,22 @@ void strbuf_ltrim(struct strbuf *sb)
106107
sb->buf[sb->len] = '\0';
107108
}
108109

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+
109126
struct strbuf **strbuf_split_buf(const char *str, size_t slen,
110127
int terminator, int max)
111128
{

strbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
4545
extern void strbuf_trim(struct strbuf *);
4646
extern void strbuf_rtrim(struct strbuf *);
4747
extern void strbuf_ltrim(struct strbuf *);
48+
extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
4849
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
4950

5051
/*

0 commit comments

Comments
 (0)