tcltest: do a better job of cleanup up after tests
[jimtcl.git] / utf8.h
blob40fc95f6bcdcdaeacd3a8ff4638b48dd038e6ad4
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-2016 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 ? (int)strlen(S) : (B))
33 #define utf8_strwidth(S, B) utf8_strlen((S), (B))
34 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
35 #define utf8_getchars(CP, C) (*(CP) = (C), 1)
36 #define utf8_upper(C) toupper(C)
37 #define utf8_title(C) toupper(C)
38 #define utf8_lower(C) tolower(C)
39 #define utf8_index(C, I) (I)
40 #define utf8_charlen(C) 1
41 #define utf8_prev_len(S, L) 1
42 #define utf8_width(C) 1
44 #else
45 #if !defined(JIM_BOOTSTRAP)
47 #define utf8_getchars utf8_fromunicode
49 /**
50 * Returns the length of the utf-8 sequence starting with 'c'.
52 * Returns 1-4, or -1 if this is not a valid start byte.
54 * Note that charlen=4 is not supported by the rest of the API.
56 int utf8_charlen(int c);
58 /**
59 * Returns the number of characters in the utf-8
60 * string of the given byte length.
62 * Any bytes which are not part of an valid utf-8
63 * sequence are treated as individual characters.
65 * The string *must* be null terminated.
67 * Does not support unicode code points > \u1fffff
69 int utf8_strlen(const char *str, int bytelen);
71 /**
72 * Calculates the display width of the first 'charlen' characters in 'str'.
73 * See utf8_width()
75 int utf8_strwidth(const char *str, int charlen);
77 /**
78 * Returns the byte index of the given character in the utf-8 string.
80 * The string *must* be null terminated.
82 * This will return the byte length of a utf-8 string
83 * if given the char length.
85 int utf8_index(const char *str, int charindex);
87 /**
88 * Returns the unicode codepoint corresponding to the
89 * utf-8 sequence 'str'.
91 * Stores the result in *uc and returns the number of bytes
92 * consumed.
94 * If 'str' is null terminated, then an invalid utf-8 sequence
95 * at the end of the string will be returned as individual bytes.
97 * If it is not null terminated, the length *must* be checked first.
99 * Does not support unicode code points > \u1fffff
101 int utf8_tounicode(const char *str, int *uc);
104 * Returns the number of bytes before 'str' that the previous
105 * utf-8 character sequence starts (which may be the middle of a sequence).
107 * Looks back at most 'len' bytes backwards, which must be > 0.
108 * If no start char is found, returns -len
110 int utf8_prev_len(const char *str, int len);
113 * Returns the upper-case variant of the given unicode codepoint.
115 * Unicode code points > \uffff are returned unchanged.
117 int utf8_upper(int uc);
120 * Returns the title-case variant of the given unicode codepoint.
122 * If none, returns utf8_upper().
124 * Unicode code points > \uffff are returned unchanged.
126 int utf8_title(int uc);
129 * Returns the lower-case variant of the given unicode codepoint.
131 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
133 * Unicode code points > \uffff are returned unchanged.
135 int utf8_lower(int uc);
138 * Returns the width (in characters) of the given unicode codepoint.
139 * This is 1 for normal letters and 0 for combining characters and 2 for wide characters.
141 int utf8_width(int ch);
143 #endif /* JIM_BOOTSTRAP */
145 #endif
147 #ifdef __cplusplus
149 #endif
151 #endif