2 3448adb0 2022-11-02 op .Dd ${MAN_DATE}
3 3448adb0 2022-11-02 op .Dt GRAPHEME_ENCODE_UTF8 3
4 3448adb0 2022-11-02 op .Os suckless.org
6 3448adb0 2022-11-02 op .Nm grapheme_encode_utf8
7 3448adb0 2022-11-02 op .Nd encode codepoint into UTF-8 string
11 3448adb0 2022-11-02 op .Fn grapheme_encode_utf8 "uint_least32_t cp" "char *str" "size_t len"
12 3448adb0 2022-11-02 op .Sh DESCRIPTION
14 3448adb0 2022-11-02 op .Fn grapheme_encode_utf8
15 3448adb0 2022-11-02 op function encodes the codepoint
17 3448adb0 2022-11-02 op into a UTF-8-string.
24 3448adb0 2022-11-02 op is large enough it writes the UTF-8-string to the memory pointed to by
26 3448adb0 2022-11-02 op Otherwise no data is written.
27 3448adb0 2022-11-02 op .Sh RETURN VALUES
29 3448adb0 2022-11-02 op .Fn grapheme_encode_utf8
30 3448adb0 2022-11-02 op function returns the length (in bytes) of the UTF-8-string resulting
35 3448adb0 2022-11-02 op is not large enough or
41 3448adb0 2022-11-02 op /* cc (-static) -o example example.c -lgrapheme */
42 3448adb0 2022-11-02 op #include <grapheme.h>
43 3448adb0 2022-11-02 op #include <stddef.h>
44 3448adb0 2022-11-02 op #include <stdlib.h>
47 3448adb0 2022-11-02 op cps_to_utf8(const uint_least32_t *cp, size_t cplen, char *str, size_t len)
49 3448adb0 2022-11-02 op size_t i, off, ret;
51 3448adb0 2022-11-02 op for (i = 0, off = 0; i < cplen; i++, off += ret) {
52 3448adb0 2022-11-02 op if ((ret = grapheme_encode_utf8(cp[i], str + off,
53 3448adb0 2022-11-02 op len - off)) > (len - off)) {
54 3448adb0 2022-11-02 op /* buffer too small */
63 3448adb0 2022-11-02 op cps_bytelen(const uint_least32_t *cp, size_t cplen)
65 3448adb0 2022-11-02 op size_t i, len;
67 3448adb0 2022-11-02 op for (i = 0, len = 0; i < cplen; i++) {
68 3448adb0 2022-11-02 op len += grapheme_encode_utf8(cp[i], NULL, 0);
75 3448adb0 2022-11-02 op cps_to_utf8_alloc(const uint_least32_t *cp, size_t cplen)
78 3448adb0 2022-11-02 op size_t len, i, ret, off;
80 3448adb0 2022-11-02 op len = cps_bytelen(cp, cplen);
82 3448adb0 2022-11-02 op if (!(str = malloc(len))) {
86 3448adb0 2022-11-02 op for (i = 0, off = 0; i < cplen; i++, off += ret) {
87 3448adb0 2022-11-02 op if ((ret = grapheme_encode_utf8(cp[i], str + off,
88 3448adb0 2022-11-02 op len - off)) > (len - off)) {
89 3448adb0 2022-11-02 op /* buffer too small */
93 3448adb0 2022-11-02 op str[off] = '\\\\0';
99 3448adb0 2022-11-02 op .Xr grapheme_decode_utf8 3 ,
100 3448adb0 2022-11-02 op .Xr libgrapheme 7
102 3448adb0 2022-11-02 op .An Laslo Hunhold Aq Mt dev@frign.de