BSON_STRING_T(3)

πŸ’– BSON_STRING_T πŸ’–

okay bestie, welcome to bson_string_t, straight outta the libbson manual, section 3. this one's the string-building girlie of the BSON world and she is literally so useful β€” just mind the warning below first, mwah πŸŽ€


⚠️ WARNING ⚠️

Deprecated since version 1.29.0. 😭 bestie she's been retired. still iconic, still documented, but like… she's in her legacy era. no cap.

🧡 String Building Abstraction


✨ SYNOPSIS ✨

#include <bson/bson.h>

typedef struct {
   char *str;
   uint32_t len;
   uint32_t alloc;
} bson_string_t;

🌸 DESCRIPTION 🌸

okay so bson_string_t is an abstraction for building strings β€” like, the whole point is you just keep slapping chunks onto it and it handles the growing for you. and the way it grows?? allocations are performed in powers of two ✨ 2, 4, 8, 16… she's doubling, she's thriving, she's not doing a million tiny reallocs like some clown.

and this API is the move if you need to build UTF-8 encoded strings. fr fr. that's the vibe. πŸ’…


πŸ¦‹ EXAMPLE πŸ¦‹

here's her in action, main character energy:

bson_string_t *str;

str = bson_string_new (NULL);
bson_string_append_printf (str, "%d %s %f\n", 0, "some string", 0.123);
printf ("%s\n", str->str);

bson_string_free (str, true);

πŸ“ TIP πŸ“

okay so here's the lil secret: you can call bson_string_free() with false if you would like to take ownership of str->str πŸ‘‘ some APIs that do this might legit just

return bson_string_free (str, false);

after building the string. so the string survives the free, you keep the goods, and nobody gets hurt. iykyk. 🧚


πŸ’œ AUTHOR πŸ’œ

MongoDB, Inc β€” yeah, the MongoDB girlies wrote her. legends only. 🫢


2009-present, MongoDB, Inc. β€” all the rights, all the years, all the respect. ✨


and that's a wrap bestie!! this page came to you from libbson, version 1.30.13, dated Oct 09, 2026 πŸ’– go build some strings (just maybe not with this one, she's deprecated, ok ily bye) πŸŽ€πŸ¦‹βœ¨

πŸ“¦ where this page comes from