grapheme_encode_utf8.sh - libgrapheme - unicode string library
 (HTM) git clone git://git.suckless.org/libgrapheme
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       grapheme_encode_utf8.sh (1915B)
       ---
            1 cat << 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
           13 The
           14 .Fn grapheme_encode_utf8
           15 function encodes the codepoint
           16 .Va cp
           17 into a UTF-8-string.
           18 If
           19 .Va str
           20 is not
           21 .Dv NULL
           22 and
           23 .Va len
           24 is large enough it writes the UTF-8-string to the memory pointed to by
           25 .Va str .
           26 Otherwise no data is written.
           27 .Sh RETURN VALUES
           28 The
           29 .Fn grapheme_encode_utf8
           30 function returns the length (in bytes) of the UTF-8-string resulting
           31 from encoding
           32 .Va cp ,
           33 even if
           34 .Va len
           35 is not large enough or
           36 .Va str
           37 is
           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 
           46 size_t
           47 cps_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 
           62 size_t
           63 cps_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 
           74 char *
           75 cps_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
          103 EOF