GRAPHEME_NEXT_WORD_BREAK_UTF8(3)
πΈ GRAPHEME_NEXT_WORD_BREAK(3) β Library Functions Manual, section 3, bestie π
okay so this is a page from the Library Functions Manual, section 3, and it's all about grapheme_next_word_break_utf8 β the girlies who find where words end in UTF-8 strings. she's iconic, she's precise, let's get into it π«Ά
β¨ NAME β¨
grapheme_next_word_break_utf8 β literally figures out the byte-offset to the next word break π
π¦ SYNOPSIS π¦
#include <grapheme.h>
size_t
grapheme_next_word_break_utf8(const char *str, size_t len);
π DESCRIPTION π
So grapheme_next_word_break_utf8() computes the offset (in bytes, no cap) to the next word break β that's the libgrapheme(7) concept of a word break, iykyk β inside the UTF-8-encoded string str of length len. and if a word begins at str, that offset is literally equal to the length of said word. the way that works out is so satisfying β¨
If len is set to SIZE_MAX β and yes, stdint.h is already included by grapheme.h, so don't even worry about it π
β then the string str is interpreted as being NUL-terminated, and processing just stops when a NUL-byte shows up. simple as that.
For non-UTF-8 input data (like, not the vibe), grapheme_next_word_break(3) can be used instead. no cap π
π RETURN VALUES π
grapheme_next_word_break_utf8() returns the offset (in bytes) to the next word break in str β or 0 if str is NULL. that's it, that's the whole return situation π
π₯ EXAMPLES π₯
/* cc (-static) -o example example.c -lgrapheme */
#include <grapheme.h>
#include <stdint.h>
#include <stdio.h>
int
main(void)
{
/* UTF-8 encoded input */
char *s = "T\xC3\xABst \xF0\x9F\x91\xA8\xE2\x80\x8D\xF0"
"\x9F\x91\xA9\xE2\x80\x8D\xF0\x9F\x91\xA6 \xF0"
"\x9F\x87\xBA\xF0\x9F\x87\xB8 \xE0\xA4\xA8\xE0"
"\xA5\x80 \xE0\xAE\xA8\xE0\xAE\xBF!";
size_t ret, len, off;
printf("Input: \"%s\"\n", s);
/* print each word with byte-length */
printf("words in NUL-delimited input:\n");
for (off = 0; s[off] != '\0'; off += ret) {
ret = grapheme_next_word_break_utf8(s + off, SIZE_MAX);
printf("%2zu bytes | %.*s\n", ret, (int)ret, s + off);
}
printf("\n");
/* do the same, but this time string is length-delimited */
len = 17;
printf("words in input delimited to %zu bytes:\n", len);
for (off = 0; off < len; off += ret) {
ret = grapheme_next_word_break_utf8(s + off, len - off);
printf("%2zu bytes | %.*s\n", ret, (int)ret, s + off);
}
return 0;
}
like, the first loop goes full NUL-terminated SIZE_MAX mode, the second one is length-delimited with len = 17, and both print out each word with its byte-length. obsessed π
π SEE ALSO π
π§ STANDARDS π§
grapheme_next_word_break_utf8() is compliant with the Unicode 17.0.0 specification. periodt. π
π AUTHORS π
Laslo Hunhold <dev@frign.de> β thank you for your service king π«Ά
aaaand that's the page!! brought to you by suckless.org, dated 2022-10-06, filed under GRAPHEME_NEXT_WORD_BREAK_UTF8(3) β go forth and break some words, bestie, you got this πβ¨π
π¦ where this page comes from
- nixos 26.05: libgrapheme 3.0.0
- nixpkgs unstable: libgrapheme 3.0.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms