Content-type: text/html; charset=UTF-8 Man page of GRAPHEME_ENCODE_UTF8

GRAPHEME_ENCODE_UTF8

Section: C Library Functions (3)
Index Return to Main Contents

BSD mandoc
suckless.org  

NAME

grapheme_encode_utf8 - encode codepoint into UTF-8 string  

SYNOPSIS

In grapheme.h Ft size_t Fn grapheme_encode_utf8 uint_least32_t cp char *str size_t len  

DESCRIPTION

The Fn grapheme_encode_utf8 function encodes the codepoint cp into a UTF-8-string. If str is not NULL and len is large enough it writes the UTF-8-string to the memory pointed to by str Otherwise no data is written.  

RETURN VALUES

The Fn grapheme_encode_utf8 function returns the length (in bytes) of the UTF-8-string resulting from encoding cp even if len is not large enough or str is NULL  

EXAMPLES

/* cc (-static) -o example example.c -lgrapheme */
#include <grapheme.h>
#include <stddef.h>
#include <stdlib.h>

size_t
cps_to_utf8(const uint_least32_t *cp, size_t cplen, char *str, size_t len)
{
        size_t i, off, ret;

        for (i = 0, off = 0; i < cplen; i++, off += ret) {
                if ((ret = grapheme_encode_utf8(cp[i], str + off,
                                                len - off)) > (len - off)) {
                        /* buffer too small */
                        break;
                }
        }

        return off;
}

size_t
cps_bytelen(const uint_least32_t *cp, size_t cplen)
{
        size_t i, len;

        for (i = 0, len = 0; i < cplen; i++) {
                len += grapheme_encode_utf8(cp[i], NULL, 0);
        }

        return len;
}

char *
cps_to_utf8_alloc(const uint_least32_t *cp, size_t cplen)
{
        char *str;
        size_t len, i, ret, off;

        len = cps_bytelen(cp, cplen);

        if (!(str = malloc(len))) {
                return NULL;
        }

        for (i = 0, off = 0; i < cplen; i++, off += ret) {
                if ((ret = grapheme_encode_utf8(cp[i], str + off,
                                                len - off)) > (len - off)) {
                        /* buffer too small */
                        break;
                }
        }
        str[off] = '\0';

        return str;
}
 

SEE ALSO

grapheme_decode_utf83, libgrapheme(7)  

AUTHORS

An Laslo Hunhold Aq Mt dev@frign.de


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUES
EXAMPLES
SEE ALSO
AUTHORS

This document was created by man2html, using the manual pages.
Time: 06:27:21 GMT, December 21, 2024