jim.c: Simplify Jim_GetIndex() for int conversion
[jimtcl.git] / utf8.h
blob728811329214265e94bc02d6d03361f8145ea261
1 #ifndef UTF8_UTIL_H
2 #define UTF8_UTIL_H
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
8 /**
9 * UTF-8 utility functions
11 * (c) 2010 Steve Bennett <steveb@workware.net.au>
13 * See LICENCE for licence details.
15 #include <jim-config.h>
17 /* Currently we support unicode points up to 2^22-1 */
18 #define MAX_UTF8_LEN 4
20 /**
21 * Converts the given unicode codepoint (0 - 0x1fffff) to utf-8
22 * and stores the result at 'p'.
24 * Returns the number of utf-8 characters (up to MAX_UTF8_LEN).
26 int utf8_fromunicode(char *p, unsigned uc);
28 #ifndef JIM_UTF8
29 #include <ctype.h>
31 /* No utf-8 support. 1 byte = 1 char */
32 #define utf8_strlen(S, B) ((B) < 0 ? strlen(S) : (B))
33 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
34 #define utf8_getchars(CP, C) (*(CP) = (C), 1)
35 #define utf8_upper(C) toupper(C)
36 #define utf8_title(C) toupper(C)
37 #define utf8_lower(C) tolower(C)
38 #define utf8_index(C, I) (I)
39 #define utf8_charlen(C) 1
40 #define utf8_prev_len(S, L) 1
42 #else
43 #if !defined(JIM_BOOTSTRAP)
45 #define utf8_getchars utf8_fromunicode
47 /**
48 * Returns the length of the utf-8 sequence starting with 'c'.
50 * Returns 1-4, or -1 if this is not a valid start byte.
52 * Note that charlen=4 is not supported by the rest of the API.
54 int utf8_charlen(int c);
56 /**
57 * Returns the number of characters in the utf-8
58 * string of the given byte length.
60 * Any bytes which are not part of an valid utf-8
61 * sequence are treated as individual characters.
63 * The string *must* be null terminated.
65 * Does not support unicode code points > \u1fffff
67 int utf8_strlen(const char *str, int bytelen);
69 /**
70 * Returns the byte index of the given character in the utf-8 string.
72 * The string *must* be null terminated.
74 * This will return the byte length of a utf-8 string
75 * if given the char length.
77 int utf8_index(const char *str, int charindex);
79 /**
80 * Returns the unicode codepoint corresponding to the
81 * utf-8 sequence 'str'.
83 * Stores the result in *uc and returns the number of bytes
84 * consumed.
86 * If 'str' is null terminated, then an invalid utf-8 sequence
87 * at the end of the string will be returned as individual bytes.
89 * If it is not null terminated, the length *must* be checked first.
91 * Does not support unicode code points > \u1fffff
93 int utf8_tounicode(const char *str, int *uc);
95 /**
96 * Returns the number of bytes before 'str' that the previous
97 * utf-8 character sequence starts (which may be the middle of a sequence).
99 * Looks back at most 'len' bytes backwards, which must be > 0.
100 * If no start char is found, returns -len
102 int utf8_prev_len(const char *str, int len);
105 * Returns the upper-case variant of the given unicode codepoint.
107 * Unicode code points > \uffff are returned unchanged.
109 int utf8_upper(int uc);
112 * Returns the title-case variant of the given unicode codepoint.
114 * If none, returns utf8_upper().
116 * Unicode code points > \uffff are returned unchanged.
118 int utf8_title(int uc);
121 * Returns the lower-case variant of the given unicode codepoint.
123 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
125 * Unicode code points > \uffff are returned unchanged.
127 int utf8_lower(int uc);
128 #endif /* JIM_BOOTSTRAP */
130 #endif
132 #ifdef __cplusplus
134 #endif
136 #endif