Skip to content

Commit 1b6c9a1

Browse files
committed
sds: use new utf8 decoder for sds_cat_utf8
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 13206cf commit 1b6c9a1

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/flb_sds.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <fluent-bit/flb_log.h>
2929
#include <fluent-bit/flb_sds.h>
3030
#include <fluent-bit/flb_utf8.h>
31+
#include <fluent-bit/flb_utils.h>
3132

3233
#include <stdarg.h>
3334
#include <ctype.h>
@@ -279,13 +280,15 @@ flb_sds_t flb_sds_copy(flb_sds_t s, const char *str, int len)
279280
return s;
280281
}
281282

282-
flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len)
283+
flb_sds_t flb_sds_cat_utf8(flb_sds_t *sds, const char *str, int str_len)
283284
{
284285
static const char int2hex[] = "0123456789abcdef";
285286
int i;
286287
int b;
287288
int ret;
288289
int hex_bytes;
290+
int offset;
291+
size_t size;
289292
uint32_t cp;
290293
uint32_t state = 0;
291294
unsigned char c;
@@ -297,6 +300,7 @@ flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len)
297300
s = *sds;
298301
head = FLB_SDS_HEADER(s);
299302

303+
/* make sure we have at least str_len extra bytes available */
300304
if (flb_sds_avail(s) <= str_len) {
301305
tmp = flb_sds_increase(s, str_len);
302306
if (tmp == NULL) {
@@ -306,6 +310,30 @@ flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len)
306310
head = FLB_SDS_HEADER(s);
307311
}
308312

313+
while (1) {
314+
offset = head->len;
315+
ret = flb_utils_write_str(s, &offset, flb_sds_alloc(s), str, str_len);
316+
if (ret == FLB_FALSE) {
317+
/* realloc */
318+
size = flb_sds_alloc(s) * 2;
319+
tmp = flb_sds_increase(s, size);
320+
if (tmp == NULL) {
321+
return NULL;
322+
}
323+
*sds = s = tmp;
324+
head = FLB_SDS_HEADER(s);
325+
}
326+
else {
327+
break;
328+
}
329+
}
330+
331+
flb_sds_len_set(s, offset);
332+
s[head->len] = '\0';
333+
return s;
334+
335+
336+
309337
for (i = 0; i < str_len; i++) {
310338
if (flb_sds_avail(s) < 8) {
311339
tmp = flb_sds_increase(s, 8);

0 commit comments

Comments
 (0)