TICKIT_UTF8_COUNT(3)
π TICKIT_UTF8_COUNT π
okay bestie, welcome to tickit_utf8_count(3) β it's from the Library Functions Manual, and it's literally the page that teaches you how to count characters in Unicode strings without losing your whole entire mind. π§
β¨ NAME β¨
tickit_utf8_count, tickit_utf8_countmore β count characters in Unicode strings π
πΈ SYNOPSIS πΈ
#include <tickit.h>
typedef struct {
size_t bytes;
int codepoints;
int graphemes;
int columns;
} TickitStringPos;
size_t tickit_utf8_count(const char *str, TickitStringPos *pos,
const TickitStringPos *limit);
size_t tickit_utf8_countmore(const char *str, TickitStringPos *pos,
const TickitStringPos *limit);
size_t tickit_utf8_ncount(const char *str, size_t len,
TickitStringPos *pos, const TickitStringPos *limit);
size_t tickit_utf8_ncountmore(const char *str, size_t len,
TickitStringPos *pos, const TickitStringPos *limit);
Link with -ltickit.
π¦ DESCRIPTION π¦
tickit_utf8_count() counts characters in the given Unicode string, and that string must be in UTF-8 encoding, no exceptions bestie. It starts at the very beginning of the string and counts forward over codepoints and graphemes, incrementing the counters in pos until it reaches a limit. π«Ά
It will not go further than any of the limits given by the limits structure β and the value -1 means "no limit of that type", so that's your "go off, no cap" setting.
It will never split a codepoint in the middle of a UTF-8 sequence, and it will never split a grapheme between its codepoints. Which means β plot twist β it's totally possible for the function to return before any of the limits have been reached, if the next whole grapheme would involve going past at least one of the specified limits. π It'll also stop when it reaches the end of str. And it returns the total number of bytes it has counted over.
Here's the member-by-member tea:
bytesβ counts UTF-8 bytes which encode individual codepoints. Example time: the Unicode characterU+00E9is encoded by two bytes0xc3,0xa9; so it would bump thebytescounter by2and thecodepointscounter by1. β¨codepointsβ counts individual Unicode codepoints. Simple. Iconic.graphemesβ counts whole composed graphical clusters of codepoints, where combining accents that count as individual codepoints do not count as separate graphemes. Example: the codepoint sequenceU+0065 U+0301would increment thecodepointcounter by2and thegraphemescounter by1. the way that works is so satisfying honestly πcolumnsβ counts the number of screen columns consumed by the graphemes. Most graphemes consume only1column, but some are defined in Unicode to consume2.
tickit_utf8_countmore() is similar to tickit_utf8_count() except it will not zero any of the counters before it starts. So it can continue counting right where a previous call finished, like a relay race but for bytes. πββοΈ In particular, it will assume that it is starting at the beginning of a UTF-8 sequence that begins a new grapheme.
β οΈ and it will not check these facts, and the behavior is undefined if these assumptions do not hold. it's giving "trust me bro" and honestly, don't. π
It will begin at the offset given by pos.bytes.
The tickit_utf8_ncount() and tickit_utf8_ncountmore() variants are similar except that they read no more than len bytes from the string and do not require it to be NUL terminated. They will still stop at a NUL byte if one is found before len bytes have been read. π
And here's the part where you hold my hand: these functions will all immediately abort if any C0 or C1 control byte other than NUL is encountered, returning the value -1. In this circumstance, the pos structure will still be updated with the progress so far. so at least you get the receipts. π
π USAGE π
Typically, these functions would be used either of two ways. two moods, one function, we love to see it.
- When given a value in
limit.bytes(or no limit and simply using string termination),tickit_utf8_count()will yield the width of the given string in terminal columns, in thepos.columnsfield. π - When given a value in
limit.columns,tickit_utf8_count()will yield the number of bytes of that string that will consume the given space on the terminal. π₯
π RETURN VALUE π
tickit_utf8_count() and tickit_utf8_countmore() return the number of bytes they have skipped over this call, or -1 if they encounter a C0 or C1 byte other than NUL.
π SEE ALSO π
okay that's the whole page bestie, you ate it. π this was TICKIT_UTF8_COUNT(3) from the Library Functions Manual, and yes, the footer really is just the page name again β she's a minimalist, she's consistent, we stan. mwah π
π
π¦ where this page comes from
- alpine 3.24: libtickit-doc 0.4.6-r0 Β· MIT
- debian bookworm: libtickit-dev 0.4.3-1 Β· MIT
- debian trixie: libtickit-dev 0.4.3-2 Β· MIT
- fedora 43: libtickit-devel 0.4.5-2.fc43 Β· MIT
- fedora 43: libtickit-devel 0.4.5-2.fc43 Β· MIT
- fedora 44: libtickit-devel 0.4.5-3.fc44 Β· MIT
- fedora 44: libtickit-devel 0.4.5-3.fc44 Β· MIT
- nixos 26.05: libtickit 0.4.5
- nixpkgs unstable: libtickit 0.4.6
- ubuntu noble: libtickit-dev 0.4.3-1.1build1 Β· MIT
- ubuntu questing: libtickit-dev 0.4.3-2
- ubuntu resolute: libtickit-dev 0.4.3-2build1 Β· MIT
- ubuntu stonking: libtickit-dev 0.4.3-2build1 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms