RUNE(3)

๐Ÿ“ฆ version 0_git20260208, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

๐Ÿ’– RUNE(3) โ€” the rune/UTF glow-up page โœจ

okay so bestie, gather round, because this is the RUNE(3) page from the Library Functions Manual and it's literally all about converting between UTF byte streams and runes. it's giving "i speak both languages" main character energy ๐Ÿฆ‹ and honestly? you're gonna slay this.


๐ŸŒธ NAME ๐ŸŒธ

runetochar, chartorune, runelen, runenlen, fullrune, utfecpy, utflen, utfnlen, utfrune, utfrrune, utfutf โ€” the whole rune/UTF conversion friend group ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <u.h>
#include <libc.h>

int    runetochar(char *s, Rune *r)

int    chartorune(Rune *r, char *s)

int    runelen(long r)

int    runenlen(Rune *r, int n)

int    fullrune(char *s, int n)

char*  utfecpy(char *s1, char *es1, char *s2)

int    utflen(char *s)

int    utfnlen(char *s, long n)

char*  utfrune(char *s, long c)

char*  utfrrune(char *s, long c)

char*  utfutf(char *s1, char *s2)

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

so these routines convert to and from a UTF byte stream and runes. that's the whole vibe. two directions, no drama ๐Ÿ’–

runetochar copies one rune at r to at most UTFmax bytes starting at s and returns the number of bytes copied. hi bestie, quick PSA: UTFmax, defined as 3 in <libc.h>, is the maximum number of bytes required to represent a rune. so 3. that's the ceiling. don't come for me. โœจ

chartorune copies at most UTFmax bytes starting at s to one rune at r and returns the number of bytes copied. but listen โ€” if the input is not exactly in UTF format, chartorune will convert to Runeerror (0xFFFD) and return 1. not me matching that error energy ๐Ÿ˜ญ it's literally the replacement character, iconic behaviour.

runelen returns the number of bytes required to convert r into UTF. simple. clean. obsessed. ๐Ÿซถ

runenlen returns the number of bytes required to convert the n runes pointed to by r into UTF. same deal but for a whole squad of n runes fr fr.

fullrune returns 1 if the string s of length n is long enough to be decoded by chartorune and 0 otherwise. โš ๏ธ but lowkey important: this does not guarantee that the string contains a legal UTF encoding. it's just a length vibe check, not a legality check, iykyk. this routine is used by programs that obtain input a byte at a time and need to know when a full rune has arrived. patient king behaviour ๐Ÿ‘‘


๐ŸŒˆ the string-lookalike squad ๐ŸŒˆ

the following routines are analogous to the corresponding string routines with utf substituted for str and rune substituted for chr. so like, if you know the str/chr versions, you already know the plot twist here ๐Ÿ’…

  • utfecpy โ€” copies UTF sequences until a null sequence has been copied, but writes no sequences beyond es1. if any sequences are copied, s1 is terminated by a null sequence, and a pointer to that sequence is returned. otherwise, the original s1 is returned. respectful of your boundaries, we stan ๐ŸŽ€
  • utflen โ€” returns the number of runes that are represented by the UTF string s. counting queen ๐Ÿงฎ
  • utfnlen โ€” returns the number of complete runes that are represented by the first n bytes of UTF string s. if the last few bytes of the string contain an incompletely coded rune, utfnlen will not count them; in this way, it differs from utflen, which includes every byte of the string. so like, utfnlen says "no half-finished crumbs ๐Ÿ™…" and utflen says "every byte counts, babes ๐Ÿ’€". know the difference or perish (emotionally).
  • utfrune / utfrrune โ€” utfrune (utfrrune) returns a pointer to the first (last) occurrence of rune c in the UTF string s, or 0 if c does not occur in the string. first one, last one, both valid. and heads up: the NUL byte terminating a string is considered to be part of the string s. that little NUL is included, no cap. ๐Ÿคฏ
  • utfutf โ€” returns a pointer to the first occurrence of the UTF string s2 as a UTF substring of s1, or 0 if there is none. and if s2 is the null string, utfutf returns s1. edge case handled, periodt. ๐Ÿ’…

๐ŸŒŸ SOURCE ๐ŸŒŸ

/src/lib9/utf/rune.c
/src/lib9/utf/utfrune.c

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


bestie if you're still here, you ate that. go forth and convert runes responsibly ๐Ÿ’–

okay and for the cute sign-off โ€” this whole thing is from the RUNE(3) Library Functions Manual page, footer and all. mwah, luv u, bye ๐ŸŽ€โœจ๐Ÿฆ‹