BSON_UTF8(3)
π 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 isNULL-terminated, you can pass-1for 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
π COPYRIGHT π
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
- ubuntu noble: libbson-doc 1.26.0-1.1ubuntu2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms