grapheme_encode_utf8.sh (1915B)
1cat << EOF 2.Dd ${MAN_DATE} 3.Dt GRAPHEME_ENCODE_UTF8 3 4.Os suckless.org 5.Sh NAME 6.Nm grapheme_encode_utf8 7.Nd encode codepoint into UTF-8 string 8.Sh SYNOPSIS 9.In grapheme.h 10.Ft size_t 11.Fn grapheme_encode_utf8 "uint_least32_t cp" "char *str" "size_t len" 12.Sh DESCRIPTION 13The 14.Fn grapheme_encode_utf8 15function encodes the codepoint 16.Va cp 17into a UTF-8-string. 18If 19.Va str 20is not 21.Dv NULL 22and 23.Va len 24is large enough it writes the UTF-8-string to the memory pointed to by 25.Va str . 26Otherwise no data is written. 27.Sh RETURN VALUES 28The 29.Fn grapheme_encode_utf8 30function returns the length (in bytes) of the UTF-8-string resulting 31from encoding 32.Va cp , 33even if 34.Va len 35is not large enough or 36.Va str 37is 38.Dv NULL . 39.Sh EXAMPLES 40.Bd -literal 41/* cc (-static) -o example example.c -lgrapheme */ 42#include <grapheme.h> 43#include <stddef.h> 44#include <stdlib.h> 45 46size_t 47cps_to_utf8(const uint_least32_t *cp, size_t cplen, char *str, size_t len) 48{ 49 size_t i, off, ret; 50 51 for (i = 0, off = 0; i < cplen; i++, off += ret) { 52 if ((ret = grapheme_encode_utf8(cp[i], str + off, 53 len - off)) > (len - off)) { 54 /* buffer too small */ 55 break; 56 } 57 } 58 59 return off; 60} 61 62size_t 63cps_bytelen(const uint_least32_t *cp, size_t cplen) 64{ 65 size_t i, len; 66 67 for (i = 0, len = 0; i < cplen; i++) { 68 len += grapheme_encode_utf8(cp[i], NULL, 0); 69 } 70 71 return len; 72} 73 74char * 75cps_to_utf8_alloc(const uint_least32_t *cp, size_t cplen) 76{ 77 char *str; 78 size_t len, i, ret, off; 79 80 len = cps_bytelen(cp, cplen); 81 82 if (!(str = malloc(len))) { 83 return NULL; 84 } 85 86 for (i = 0, off = 0; i < cplen; i++, off += ret) { 87 if ((ret = grapheme_encode_utf8(cp[i], str + off, 88 len - off)) > (len - off)) { 89 /* buffer too small */ 90 break; 91 } 92 } 93 str[off] = '\\\\0'; 94 95 return str; 96} 97.Ed 98.Sh SEE ALSO 99.Xr grapheme_decode_utf8 3 , 100.Xr libgrapheme 7 101.Sh AUTHORS 102.An Laslo Hunhold Aq Mt dev@frign.de 103EOF