package: simplification/code cleanup
[jimtcl.git] / utf8.h
blob4dc5a020a5f1049a1bc785a22791fbebfa277958
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_getchars(CP, C) (*(CP) = (C), 1)
30 #define utf8_upper(C) toupper(C)
31 #define utf8_title(C) toupper(C)
32 #define utf8_lower(C) tolower(C)
33 #define utf8_index(C, I) (I)
34 #define utf8_charlen(C) 1
35 #define utf8_prev_len(S, L) 1
37 #else
38 #if !defined(JIM_BOOTSTRAP)
40 #define utf8_getchars utf8_fromunicode
42 /**
43 * Returns the length of the utf-8 sequence starting with 'c'.
45 * Returns 1-4, or -1 if this is not a valid start byte.
47 * Note that charlen=4 is not supported by the rest of the API.
49 int utf8_charlen(int c);
51 /**
52 * Returns the number of characters in the utf-8
53 * string of the given byte length.
55 * Any bytes which are not part of an valid utf-8
56 * sequence are treated as individual characters.
58 * The string *must* be null terminated.
60 * Does not support unicode code points > \u1fffff
62 int utf8_strlen(const char *str, int bytelen);
64 /**
65 * Returns the byte index of the given character in the utf-8 string.
67 * The string *must* be null terminated.
69 * This will return the byte length of a utf-8 string
70 * if given the char length.
72 int utf8_index(const char *str, int charindex);
74 /**
75 * Returns the unicode codepoint corresponding to the
76 * utf-8 sequence 'str'.
78 * Stores the result in *uc and returns the number of bytes
79 * consumed.
81 * If 'str' is null terminated, then an invalid utf-8 sequence
82 * at the end of the string will be returned as individual bytes.
84 * If it is not null terminated, the length *must* be checked first.
86 * Does not support unicode code points > \u1fffff
88 int utf8_tounicode(const char *str, int *uc);
90 /**
91 * Returns the number of bytes before 'str' that the previous
92 * utf-8 character sequence starts (which may be the middle of a sequence).
94 * Looks back at most 'len' bytes backwards, which must be > 0.
95 * If no start char is found, returns -len
97 int utf8_prev_len(const char *str, int len);
99 /**
100 * Returns the upper-case variant of the given unicode codepoint.
102 * Unicode code points > \uffff are returned unchanged.
104 int utf8_upper(int uc);
107 * Returns the title-case variant of the given unicode codepoint.
109 * If none, returns utf8_upper().
111 * Unicode code points > \uffff are returned unchanged.
113 int utf8_title(int uc);
116 * Returns the lower-case variant of the given unicode codepoint.
118 * NOTE: Use utf8_upper() in preference for case-insensitive matching.
120 * Unicode code points > \uffff are returned unchanged.
122 int utf8_lower(int uc);
123 #endif /* JIM_BOOTSTRAP */
125 #endif
127 #endif