#ifndef UTF8_UTIL_H1#define UTF8_UTIL_H23#ifdef __cplusplus4extern "C" {5#endif67/**8* UTF-8 utility functions9*10* (c) 2010-2019 Steve Bennett <[email protected]>11*12* See utf8.c for licence details.13*/1415#ifndef USE_UTF816#include <ctype.h>1718#define MAX_UTF8_LEN 11920/* No utf-8 support. 1 byte = 1 char */21#define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))22#define utf8_strwidth(S, B) utf8_strlen((S), (B))23#define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)24#define utf8_index(C, I) (I)25#define utf8_charlen(C) 126#define utf8_width(C) 12728#else2930#define MAX_UTF8_LEN 43132/**33* Converts the given unicode codepoint (0 - 0x1fffff) to utf-834* and stores the result at 'p'.35*36* Returns the number of utf-8 characters37*/38int utf8_fromunicode(char *p, unsigned uc);3940/**41* Returns the length of the utf-8 sequence starting with 'c'.42*43* Returns 1-4, or -1 if this is not a valid start byte.44*45* Note that charlen=4 is not supported by the rest of the API.46*/47int utf8_charlen(int c);4849/**50* Returns the number of characters in the utf-851* string of the given byte length.52*53* Any bytes which are not part of an valid utf-854* sequence are treated as individual characters.55*56* The string *must* be null terminated.57*58* Does not support unicode code points > \u1fffff59*/60int utf8_strlen(const char *str, int bytelen);6162/**63* Calculates the display width of the first 'charlen' characters in 'str'.64* See utf8_width()65*/66int utf8_strwidth(const char *str, int charlen);6768/**69* Returns the byte index of the given character in the utf-8 string.70*71* The string *must* be null terminated.72*73* This will return the byte length of a utf-8 string74* if given the char length.75*/76int utf8_index(const char *str, int charindex);7778/**79* Returns the unicode codepoint corresponding to the80* utf-8 sequence 'str'.81*82* Stores the result in *uc and returns the number of bytes83* consumed.84*85* If 'str' is null terminated, then an invalid utf-8 sequence86* at the end of the string will be returned as individual bytes.87*88* If it is not null terminated, the length *must* be checked first.89*90* Does not support unicode code points > \u1fffff91*/92int utf8_tounicode(const char *str, int *uc);9394/**95* Returns the width (in characters) of the given unicode codepoint.96* This is 1 for normal letters and 0 for combining characters and 2 for wide characters.97*/98int utf8_width(int ch);99100#endif101102#ifdef __cplusplus103}104#endif105106#endif107108109