string map and string compare now support embedded nulls
[jimtcl.git] / utf8.h
blob99706839ad66b1a68b937c3505227046ffbd7006
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.
53 * If 'c' is not a valid start byte, returns 1.
55 int utf8_charlen(int c);
57 /**
58 * Returns the number of characters in the utf-8
59 * string of the given byte length.
61 * Any bytes which are not part of an valid utf-8
62 * sequence are treated as individual characters.
64 * The string *must* be null terminated.
66 * Does not support unicode code points > \u1fffff
68 int utf8_strlen(const char *str, int bytelen);
70 /**
71 * Calculates the display width of the first 'charlen' characters in 'str'.
72 * See utf8_width()
74 int utf8_strwidth(const char *str, int charlen);
76 /**
77 * Returns the byte index of the given character in the utf-8 string.
79 * The string *must* be null terminated.
81 * This will return the byte length of a utf-8 string
82 * if given the char length.
84 int utf8_index(const char *str, int charindex);
86 /**
87 * Returns the unicode codepoint corresponding to the
88 * utf-8 sequence 'str'.
90 * Stores the result in *uc and returns the number of bytes
91 * consumed.
93 * If 'str' is null terminated, then an invalid utf-8 sequence
94 * at the end of the string will be returned as individual bytes.
96 * If it is not null terminated, the length *must* be checked first.
98 * Does not support unicode code points > \u1fffff
100 int utf8_tounicode(const char *str, int *uc);
103 * Returns the number of bytes before 'str' that the previous
104 * utf-8 character sequence starts (which may be the middle of a sequence).
106 * Looks back at most 'len' bytes backwards, which must be > 0.
107 * If no start char is found, returns -len
109 int utf8_prev_len(const char *str, int len);
112 * Returns the upper-case variant of the given unicode codepoint.
114 * Unicode code points > \uffff are returned unchanged.
116 int utf8_upper(int uc);
119 * Returns the title-case variant of the given unicode codepoint.
121 * If none, returns utf8_upper().
123 * Unicode code points > \uffff are returned unchanged.
125 int utf8_title(int uc);
128 * Returns the lower-case variant of the given unicode codepoint.
130 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
132 * Unicode code points > \uffff are returned unchanged.
134 int utf8_lower(int uc);
137 * Returns the width (in characters) of the given unicode codepoint.
138 * This is 1 for normal letters and 0 for combining characters and 2 for wide characters.
140 int utf8_width(int ch);
142 #endif /* JIM_BOOTSTRAP */
144 #endif
146 #ifdef __cplusplus
148 #endif
150 #endif