]> git.lizzy.rs Git - linenoise.git/blob - utf8.h
Make linenoiseClearScreen() public
[linenoise.git] / utf8.h
1 #ifndef UTF8_UTIL_H
2 #define UTF8_UTIL_H
3 /**
4  * UTF-8 utility functions
5  *
6  * (c) 2010 Steve Bennett <steveb@workware.net.au>
7  *
8  * See LICENCE for licence details.
9  */
10
11 #ifndef USE_UTF8
12 #include <ctype.h>
13
14 /* No utf-8 support. 1 byte = 1 char */
15 #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
16 #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
17 #define utf8_index(C, I) (I)
18 #define utf8_charlen(C) 1
19
20 #else
21 /**
22  * Converts the given unicode codepoint (0 - 0xffff) to utf-8
23  * and stores the result at 'p'.
24  * 
25  * Returns the number of utf-8 characters (1-3).
26  */
27 int utf8_fromunicode(char *p, unsigned short uc);
28
29 /**
30  * Returns the length of the utf-8 sequence starting with 'c'.
31  * 
32  * Returns 1-4, or -1 if this is not a valid start byte.
33  *
34  * Note that charlen=4 is not supported by the rest of the API.
35  */
36 int utf8_charlen(int c);
37
38 /**
39  * Returns the number of characters in the utf-8 
40  * string of the given byte length.
41  *
42  * Any bytes which are not part of an valid utf-8
43  * sequence are treated as individual characters.
44  *
45  * The string *must* be null terminated.
46  *
47  * Does not support unicode code points > \uffff
48  */
49 int utf8_strlen(const char *str, int bytelen);
50
51 /**
52  * Returns the byte index of the given character in the utf-8 string.
53  * 
54  * The string *must* be null terminated.
55  *
56  * This will return the byte length of a utf-8 string
57  * if given the char length.
58  */
59 int utf8_index(const char *str, int charindex);
60
61 /**
62  * Returns the unicode codepoint corresponding to the
63  * utf-8 sequence 'str'.
64  * 
65  * Stores the result in *uc and returns the number of bytes
66  * consumed.
67  *
68  * If 'str' is null terminated, then an invalid utf-8 sequence
69  * at the end of the string will be returned as individual bytes.
70  *
71  * If it is not null terminated, the length *must* be checked first.
72  *
73  * Does not support unicode code points > \uffff
74  */
75 int utf8_tounicode(const char *str, int *uc);
76
77 #endif
78
79 #endif