Fix hardcoded install paths
[jimtcl.git] / utf8.h
blob71fd6ff2b83799110424ece77eaa127e6591831e
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 /* Currently we support unicode points up to 2^22-1 */
13 #define MAX_UTF8_LEN 4
15 /**
16 * Converts the given unicode codepoint (0 - 0x1fffff) to utf-8
17 * and stores the result at 'p'.
19 * Returns the number of utf-8 characters (up to MAX_UTF8_LEN).
21 int utf8_fromunicode(char *p, unsigned uc);
23 #ifndef JIM_UTF8
24 #include <ctype.h>
26 /* No utf-8 support. 1 byte = 1 char */
27 #define utf8_strlen(S, B) ((B) < 0 ? strlen(S) : (B))
28 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
29 #define utf8_upper(C) toupper(C)
30 #define utf8_title(C) toupper(C)
31 #define utf8_lower(C) tolower(C)
32 #define utf8_index(C, I) (I)
33 #define utf8_charlen(C) 1
34 #define utf8_prev_len(S, L) 1
36 #else
37 #if !defined(JIM_BOOTSTRAP)
38 /**
39 * Returns the length of the utf-8 sequence starting with 'c'.
41 * Returns 1-4, or -1 if this is not a valid start byte.
43 * Note that charlen=4 is not supported by the rest of the API.
45 int utf8_charlen(int c);
47 /**
48 * Returns the number of characters in the utf-8
49 * string of the given byte length.
51 * Any bytes which are not part of an valid utf-8
52 * sequence are treated as individual characters.
54 * The string *must* be null terminated.
56 * Does not support unicode code points > \u1fffff
58 int utf8_strlen(const char *str, int bytelen);
60 /**
61 * Returns the byte index of the given character in the utf-8 string.
63 * The string *must* be null terminated.
65 * This will return the byte length of a utf-8 string
66 * if given the char length.
68 int utf8_index(const char *str, int charindex);
70 /**
71 * Returns the unicode codepoint corresponding to the
72 * utf-8 sequence 'str'.
74 * Stores the result in *uc and returns the number of bytes
75 * consumed.
77 * If 'str' is null terminated, then an invalid utf-8 sequence
78 * at the end of the string will be returned as individual bytes.
80 * If it is not null terminated, the length *must* be checked first.
82 * Does not support unicode code points > \u1fffff
84 int utf8_tounicode(const char *str, int *uc);
86 /**
87 * Returns the number of bytes before 'str' that the previous
88 * utf-8 character sequence starts (which may be the middle of a sequence).
90 * Looks back at most 'len' bytes backwards, which must be > 0.
91 * If no start char is found, returns -len
93 int utf8_prev_len(const char *str, int len);
95 /**
96 * Returns the upper-case variant of the given unicode codepoint.
98 * Unicode code points > \uffff are returned unchanged.
100 int utf8_upper(int uc);
103 * Returns the title-case variant of the given unicode codepoint.
105 * If none, returns utf8_upper().
107 * Unicode code points > \uffff are returned unchanged.
109 int utf8_title(int uc);
112 * Returns the lower-case variant of the given unicode codepoint.
114 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
116 * Unicode code points > \uffff are returned unchanged.
118 int utf8_lower(int uc);
119 #endif /* JIM_BOOTSTRAP */
121 #endif
123 #endif