sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

index.md (1976B)


      1 GRAPHEME\_ENCODE\_UTF8(3) - Library Functions Manual
      2 
      3 # NAME
      4 
      5 **grapheme\_encode\_utf8** - encode codepoint into UTF-8 string
      6 
      7 # SYNOPSIS
      8 
      9 **#include <grapheme.h>**
     10 
     11 *size\_t*  
     12 **grapheme\_encode\_utf8**(*uint\_least32\_t cp*, *char \*str*, *size\_t len*);
     13 
     14 # DESCRIPTION
     15 
     16 The
     17 **grapheme\_encode\_utf8**()
     18 function encodes the codepoint
     19 *cp*
     20 into a UTF-8-string.
     21 If
     22 *str*
     23 is not
     24 `NULL`
     25 and
     26 *len*
     27 is large enough it writes the UTF-8-string to the memory pointed to by
     28 *str*.
     29 Otherwise no data is written.
     30 
     31 # RETURN VALUES
     32 
     33 The
     34 **grapheme\_encode\_utf8**()
     35 function returns the length (in bytes) of the UTF-8-string resulting
     36 from encoding
     37 *cp*,
     38 even if
     39 *len*
     40 is not large enough or
     41 *str*
     42 is
     43 `NULL`.
     44 
     45 # EXAMPLES
     46 
     47 	/* cc (-static) -o example example.c -lgrapheme */
     48 	#include <grapheme.h>
     49 	#include <stddef.h>
     50 	#include <stdlib.h>
     51 	
     52 	size_t
     53 	cps_to_utf8(const uint_least32_t *cp, size_t cplen, char *str, size_t len)
     54 	{
     55 		size_t i, off, ret;
     56 	
     57 		for (i = 0, off = 0; i < cplen; i++, off += ret) {
     58 			if ((ret = grapheme_encode_utf8(cp[i], str + off,
     59 			                                len - off)) > (len - off)) {
     60 				/* buffer too small */
     61 				break;
     62 			}
     63 		}
     64 	
     65 		return off;
     66 	}
     67 	
     68 	size_t
     69 	cps_bytelen(const uint_least32_t *cp, size_t cplen)
     70 	{
     71 		size_t i, len;
     72 	
     73 		for (i = 0, len = 0; i < cplen; i++) {
     74 			len += grapheme_encode_utf8(cp[i], NULL, 0);
     75 		}
     76 	
     77 		return len;
     78 	}
     79 	
     80 	char *
     81 	cps_to_utf8_alloc(const uint_least32_t *cp, size_t cplen)
     82 	{
     83 		char *str;
     84 		size_t len, i, ret, off;
     85 	
     86 		len = cps_bytelen(cp, cplen);
     87 	
     88 		if (!(str = malloc(len))) {
     89 			return NULL;
     90 		}
     91 	
     92 		for (i = 0, off = 0; i < cplen; i++, off += ret) {
     93 			if ((ret = grapheme_encode_utf8(cp[i], str + off,
     94 			                                len - off)) > (len - off)) {
     95 				/* buffer too small */
     96 				break;
     97 			}
     98 		}
     99 		str[off] = '\0';
    100 	
    101 		return str;
    102 	}
    103 
    104 # SEE ALSO
    105 
    106 grapheme\_decode\_utf8(3),
    107 libgrapheme(7)
    108 
    109 # AUTHORS
    110 
    111 Laslo Hunhold ([dev@frign.de](mailto:dev@frign.de))
    112 
    113 suckless.org - 2022-10-06