BSON_UTF8(3)

πŸ“¦
version 1.26.0 Β· 3 versions
, in ubuntu noble

πŸ’– BSON_UTF8(3) πŸ’–

okay bestie, welcome to the libbson manual, section 3, page BSON_UTF8. yes it's a whole page about text encoding and yes it is lowkey the most important thing you'll read all day, because if you get this wrong everything downstream gets cursed. let's go ✨


🌸 ENCODING 🌸

Libbson is fully, unapologetically expecting you to always be working with UTF-8 encoded text. that's the vibe. that's the whole vibe. Anything else is invalid API use. like, not "oops my bad" invalid, but invalid API use invalid. do not test it. do not try to be cute with Latin-1. she will not accept it. πŸ’€

But if you do need to walk through UTF-8 sequences yourself β€” maybe you're feeling adventurous, maybe you're building something iconic β€” you've got a whole squad of UTF-8 helper functions distributed with Libbson. they're right there for you, go off πŸ¦‹


πŸŽ€ VALIDATING A UTF-8 SEQUENCE πŸŽ€

so you've got a string called my_string and you need a vibe check. here's the move:

good news: if you know for a fact that the string is NULL-terminated, you can pass -1 for the string length and just let her figure it out herself πŸ’…
if (!bson_utf8_validate (my_string, -1, false)) {
   printf ("Validation failed.\n");
}

but WAIT. plot twist πŸ™ˆ if my_string has NULL bytes within the string (not just at the end, bestie, we mean in the middle), then -1 is not gonna cut it. you must provide the string length. and notice the true at the end β€” that's the flag saying \0 is allowed in there. very important detail, don't skip it:

if (!bson_utf8_validate (my_string, my_string_len, true)) {
   printf ("Validation failed.\n");
}

for more info, go read the API reference for bson_utf8_validate(). it's giving documentation. it's giving answers. πŸ“šβœ¨


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc

2017-present, MongoDB, Inc


and that's the whole page, bestie!! you just read libbson version 1.26.0, dated Mar 31, 2024, and honestly? iconic behavior. go validate some strings, stay UTF-8, and remember: no cap, anything else is invalid API use πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from