fmt_utf8(3)

πŸ“¦
version 0.34 Β· 2 versions
  • 0.34 Β· alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.30 Β· fedora 43, fedora 44, ubuntu jammy
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ fmt_utf8 β€” encode 31-bit unsigned integer using UTF-8 rules ✨

okay bestie so this one's from the Library Functions Manual (section 3, library calls, you know the vibe) and it's genuinely so cute, let me put you on πŸ’–


🌸 NAME 🌸

fmt_utf8 β€” encode 31-bit unsigned integer using UTF-8 rules


πŸŽ€ SYNTAX πŸŽ€

#include <libowfat/fmt.h>

size_t fmt_utf8(char *dest,uint32_t source);

πŸ’… DESCRIPTION πŸ’…

so fmt_utf8 takes a 31-bit unsigned integer and encodes it using the UTF-8 rules. that's the whole job. iconic behaviour, no notes 🫢

the output can be anywhere from 1 byte (for values 0-0x7f) all the way up to 5 bytes (for values 0x4000000-0x7fffffff). yes bestie, FIVE. it's giving stretchy πŸ’€

heads up: values larger than 0x7fffffff cannot be represented in this encoding. like, at all. there is no space for them, that's the ceiling, periodt.

now the fun part: if dest equals FMT_LEN (i.e. it's NULL), fmt_utf8 doesn't write anything β€” it just returns the number of bytes it would have written. so it's a little preview, a little "here's how much room you'd need, bestie" ✨

and for convenience, fmt.h defines the integer FMT_UTF8 to be big enough to contain every possible fmt_utf8 output. so like, that's your buffer size girlie, use it 🌈


πŸ‘‘ NOTE πŸ‘‘

okay so this is the part where it gets a lil niche and I need you to pay attention 🧚

fmt_utf8 and scan_utf8 implement the encoding from UTF-8, but they're meant to be able to store integers, not just Unicode code points. it's a whole different use case, iykyk.

which means: values larger than 0x10ffff are not valid UTF-8 (see RFC 3629) β€” but they can be represented in this encoding. so fmt_utf8 will allow them. it's not gatekeeping, it's just built different πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹

  • scan_utf8(3) β€” the other half of this duo, go read it fr fr

and that's the page bestie!! πŸ’– this one lives in the Library Functions Manual, fmt_utf8(3), section 3, and it didn't come with a date stamped on it β€” so just vibe with it as-is. slay, go encode some integers, you got this πŸŒΈβœ¨πŸ’œ