4 * UTF-8 utility functions
6 * (c) 2010 Steve Bennett <steveb@workware.net.au>
8 * See LICENCE for licence details.
14 /* No utf-8 support. 1 byte = 1 char */
15 #define utf8_strlen(S, B) (B) < 0 ? (int)strlen(S) : (B)
16 #define utf8_tounicode(S, CP) (*(CP) = *(S), 1)
17 #define utf8_index(C, I) (I)
18 #define utf8_charlen(C) 1
22 * Converts the given unicode codepoint (0 - 0xffff) to utf-8
23 * and stores the result at 'p'.
25 * Returns the number of utf-8 characters (1-3).
27 int utf8_fromunicode(char *p, unsigned short uc);
30 * Returns the length of the utf-8 sequence starting with 'c'.
32 * Returns 1-4, or -1 if this is not a valid start byte.
34 * Note that charlen=4 is not supported by the rest of the API.
36 int utf8_charlen(int c);
39 * Returns the number of characters in the utf-8
40 * string of the given byte length.
42 * Any bytes which are not part of an valid utf-8
43 * sequence are treated as individual characters.
45 * The string *must* be null terminated.
47 * Does not support unicode code points > \uffff
49 int utf8_strlen(const char *str, int bytelen);
52 * Returns the byte index of the given character in the utf-8 string.
54 * The string *must* be null terminated.
56 * This will return the byte length of a utf-8 string
57 * if given the char length.
59 int utf8_index(const char *str, int charindex);
62 * Returns the unicode codepoint corresponding to the
63 * utf-8 sequence 'str'.
65 * Stores the result in *uc and returns the number of bytes
68 * If 'str' is null terminated, then an invalid utf-8 sequence
69 * at the end of the string will be returned as individual bytes.
71 * If it is not null terminated, the length *must* be checked first.
73 * Does not support unicode code points > \uffff
75 int utf8_tounicode(const char *str, int *uc);