Skip to content

Commit 68a4b10

Browse files
committed
bson_new_printf
1 parent 3c6650a commit 68a4b10

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/libbson/src/bson/bson-string.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ bson_string_append_printf (bson_string_t *string, const char *format, ...)
253253
bson_free (ret);
254254
}
255255

256+
bson_string_t *
257+
bson_string_new_printf (const char *format, ...)
258+
{
259+
va_list args;
260+
char *s;
261+
262+
BSON_ASSERT (format);
263+
264+
va_start (args, format);
265+
s = bson_strdupv_printf (format, args);
266+
va_end (args);
267+
268+
bson_string_t *ret = bson_string_new (s);
269+
bson_free (s);
270+
return ret;
271+
}
272+
256273

257274
/*
258275
*--------------------------------------------------------------------------

src/libbson/src/bson/bson-string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ bson_string_append_unichar (bson_string_t *string, bson_unichar_t unichar);
5050
BSON_EXPORT (void)
5151
bson_string_append_printf (bson_string_t *string, const char *format, ...)
5252
BSON_GNUC_PRINTF (2, 3);
53+
BSON_EXPORT (bson_string_t *)
54+
bson_string_new_printf (const char *format, ...) BSON_GNUC_PRINTF (1, 2);
5355
BSON_EXPORT (void)
5456
bson_string_truncate (bson_string_t *string, uint32_t len);
5557
BSON_EXPORT (char *)

src/libbson/tests/test-bcon-basic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ test_int32 (void)
293293
bson_init (&bcon);
294294
bson_init (&expected);
295295

296-
bson_append_int32 (&expected, "foo", -1, 100);
296+
bson_append_int32 (&expected, "foobar", 3, 100);
297297

298298
BCON_APPEND (&bcon, "foo", BCON_INT32 (100));
299299

src/libbson/tests/test-string.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ test_bson_string_append_printf (void)
107107
bson_string_free (str, true);
108108
}
109109

110+
static void
111+
test_bson_string_new_printf (void)
112+
{
113+
bson_string_t *str =
114+
bson_string_new_printf ("%s %d %d %d", "testing", 1, 2, 3);
115+
BSON_ASSERT (!strcmp (str->str, "testing 1 2 3"));
116+
bson_string_free (str, true);
117+
}
118+
110119

111120
static void
112121
test_bson_string_append_unichar (void)

0 commit comments

Comments
 (0)