9 * UTF-8 utility functions
11 * (c) 2010-2016 Steve Bennett <steveb@workware.net.au>
13 * See LICENCE for licence details.
19 /* No utf-8 support. 1 byte = 1 char */
20 #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
21 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
22 #define utf8_index(C, I) (I)
23 #define utf8_charlen(C) 1
27 * Converts the given unicode codepoint (0 - 0x1fffff) to utf-8
28 * and stores the result at 'p'.
30 * Returns the number of utf-8 characters
32 int utf8_fromunicode(char *p, unsigned uc);
35 * Returns the length of the utf-8 sequence starting with 'c'.
37 * Returns 1-4, or -1 if this is not a valid start byte.
39 * Note that charlen=4 is not supported by the rest of the API.
41 int utf8_charlen(int c);
44 * Returns the number of characters in the utf-8
45 * string of the given byte length.
47 * Any bytes which are not part of an valid utf-8
48 * sequence are treated as individual characters.
50 * The string *must* be null terminated.
52 * Does not support unicode code points > \u1fffff
54 int utf8_strlen(const char *str, int bytelen);
57 * Returns the byte index of the given character in the utf-8 string.
59 * The string *must* be null terminated.
61 * This will return the byte length of a utf-8 string
62 * if given the char length.
64 int utf8_index(const char *str, int charindex);
67 * Returns the unicode codepoint corresponding to the
68 * utf-8 sequence 'str'.
70 * Stores the result in *uc and returns the number of bytes
73 * If 'str' is null terminated, then an invalid utf-8 sequence
74 * at the end of the string will be returned as individual bytes.
76 * If it is not null terminated, the length *must* be checked first.
78 * Does not support unicode code points > \u1fffff
80 int utf8_tounicode(const char *str, int *uc);