GRAPHEME_NEXT_SENTENCE_BREAK_UTF8(3)

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

✨ GRAPHEME_NEXT_SENTENCE_BREAK_UTF8 ✨

okay bestie, gather round, because this one is from Library Functions Manual and it's literally about finding where sentences end in UTF-8 strings πŸ’…


🌸 NAME 🌸

grapheme_next_sentence_break_utf8 β€” the function that figures out the byte-offset to the next sentence break πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <grapheme.h>

size_t
grapheme_next_sentence_break_utf8(const char *str, size_t len);

πŸ’– DESCRIPTION πŸ’–

So like, grapheme_next_sentence_break_utf8() computes the offset β€” in bytes, bestie, bytes β€” to the next sentence break (peep libgrapheme(7) for the lore) in the UTF-8-encoded string str, which has length len. If a sentence begins at str, then this offset is literally equal to the length of said sentence. slay ✨

And if you set len to SIZE_MAX (stdint.h is already included by grapheme.h, don't worry babes), then str gets interpreted as NUL-terminated, and processing just stops when it hits a NUL-byte. iconic behavior tbh 🫢

Oh and heads up: for non-UTF-8 input data, you can use grapheme_next_sentence_break(3) instead. iykyk πŸ’œ


πŸŽ€ RETURN VALUES πŸŽ€

grapheme_next_sentence_break_utf8() returns the offset (in bytes, again, bytes 😭) to the next sentence break in str β€” or 0 if str is NULL. that's it, that's the whole vibe check πŸ‘‘


🌈 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 sentence with byte-length */
        printf("sentences in NUL-delimited input:\n");
        for (off = 0; s[off] != '\0'; off += ret) {
                ret = grapheme_next_sentence_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("sentences in input delimited to %zu bytes:\n", len);
        for (off = 0; off < len; off += ret) {
                ret = grapheme_next_sentence_break_utf8(s + off, len - off);
                printf("%2zu bytes | %.*s\n", ret, (int)ret, s + off);
        }

        return 0;
}

like look at that loop go β€” first pass walks the NUL-delimited string with SIZE_MAX, second pass goes length-delimited with len = 17. literally the same function, two moods πŸ’€βœ¨


πŸ”₯ SEE ALSO πŸ”₯


πŸ‘‘ STANDARDS πŸ‘‘

grapheme_next_sentence_break_utf8() is compliant with the Unicode 17.0.0 specification. no cap, certified 🧚


πŸ’œ AUTHORS πŸ’œ

Laslo Hunhold <dev@frign.de> β€” ty king πŸ™


ok that's the whole page bestie, go forth and break some sentences (the good, spec-compliant kind) πŸ’…

suckless.org Β· 2022-10-GRAPHEME_NEXT_SENTENCE_BREAK_UTF8(3) β€” mwah, that's the sign-off, see u in the next manpage πŸŽ€πŸ’–

πŸ“¦ where this page comes from