GRAPHEME_NEXT_WORD_BREAK_UTF8(3)

πŸ“¦ version 3.0.0, in nixos 26.05, nixpkgs unstable

🌸 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