Support and ignore fconfigure -translation ...
[jimtcl.git] / utf8.h
blobcbce8de88501ab3f2c4e18d8e8123019d531bffe
1 #ifndef UTF8_UTIL_H
2 #define UTF8_UTIL_H
3 /**
4 * UTF-8 utility functions
6 * (c) 2010 Steve Bennett <steveb@workware.net.au>
8 * See LICENCE for licence details.
9 */
10 #include <jim-config.h>
12 /**
13 * Converts the given unicode codepoint (0 - 0xffff) to utf-8
14 * and stores the result at 'p'.
16 * Returns the number of utf-8 characters (1-3).
18 int utf8_fromunicode(char *p, unsigned short uc);
20 #ifndef JIM_UTF8
21 #include <ctype.h>
23 /* No utf-8 support. 1 byte = 1 char */
24 #define utf8_strlen(S, B) (B) < 0 ? strlen(S) : (B)
25 #define utf8_tounicode(S, CP) (*(CP) = *(S), 1)
26 #define utf8_upper(C) toupper(C)
27 #define utf8_lower(C) tolower(C)
28 #define utf8_index(C, I) (I)
29 #define utf8_charlen(C) 1
30 #define utf8_prev_len(S, L) 1
32 #else
33 /**
34 * Returns the length of the utf-8 sequence starting with 'c'.
36 * Returns 1-4, or -1 if this is not a valid start byte.
38 * Note that charlen=4 is not supported by the rest of the API.
40 int utf8_charlen(int c);
42 /**
43 * Returns the number of characters in the utf-8
44 * string of the given byte length.
46 * Any bytes which are not part of an valid utf-8
47 * sequence are treated as individual characters.
49 * The string *must* be null terminated.
51 * Does not support unicode code points > \uffff
53 int utf8_strlen(const char *str, int bytelen);
55 /**
56 * Returns the byte index of the given character in the utf-8 string.
58 * The string *must* be null terminated.
60 * This will return the byte length of a utf-8 string
61 * if given the char length.
63 int utf8_index(const char *str, int charindex);
65 /**
66 * Returns the unicode codepoint corresponding to the
67 * utf-8 sequence 'str'.
69 * Stores the result in *uc and returns the number of bytes
70 * consumed.
72 * If 'str' is null terminated, then an invalid utf-8 sequence
73 * at the end of the string will be returned as individual bytes.
75 * If it is not null terminated, the length *must* be checked first.
77 * Does not support unicode code points > \uffff
79 int utf8_tounicode(const char *str, int *uc);
81 /**
82 * Returns the number of bytes before 'str' that the previous
83 * utf-8 character sequence starts (which may be the middle of a sequence).
85 * Looks back at most 'len' bytes backwards, which must be > 0.
86 * If no start char is found, returns -len
88 int utf8_prev_len(const char *str, int len);
90 /**
91 * Returns the upper-case variant of the given unicode codepoint.
93 * Does not support unicode code points > \uffff
95 int utf8_upper(int uc);
97 /**
98 * Returns the lower-case variant of the given unicode codepoint.
100 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
102 * Does not support unicode code points > \uffff
104 int utf8_lower(int uc);
106 #endif
108 #endif