]> git.ozlabs.org Git - ccan/blob - ccan/charset/charset.h
257d2860d70122efb407ae2d4d49b84015f72dcc
[ccan] / ccan / charset / charset.h
1 /*
2   Copyright (C) 2010 Joseph A. Adams (joeyadams3.14159@gmail.com)
3   All rights reserved.
4
5   Permission is hereby granted, free of charge, to any person obtaining a copy
6   of this software and associated documentation files (the "Software"), to deal
7   in the Software without restriction, including without limitation the rights
8   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9   copies of the Software, and to permit persons to whom the Software is
10   furnished to do so, subject to the following conditions:
11
12   The above copyright notice and this permission notice shall be included in
13   all copies or substantial portions of the Software.
14
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21   THE SOFTWARE.
22 */
23
24 #ifndef CCAN_CHARSET_H
25 #define CCAN_CHARSET_H
26
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30
31 #define REPLACEMENT_CHARACTER 0xFFFD
32
33 /*
34  * Type for Unicode codepoints.
35  * We need our own because wchar_t might be 16 bits.
36  */
37 typedef uint32_t uchar_t;
38
39 /*
40  * Validate the given UTF-8 string.
41  * If it contains '\0' characters, it is still valid.
42  */
43 bool utf8_validate(const char *str, size_t length);
44
45 /*
46  * Read a single UTF-8 character starting at @s,
47  * returning the length, in bytes, of the character read.
48  *
49  * This function assumes input is valid UTF-8,
50  * and that there are enough characters in front of @s.
51  */
52 int utf8_read_char(const char *s, uchar_t *out);
53
54 /*
55  * Write a single UTF-8 character to @s,
56  * returning the length, in bytes, of the character written.
57  *
58  * @unicode should be U+0000..U+10FFFF, but not U+D800..U+DFFF.
59  * If @unicode is invalid, REPLACEMENT_CHARACTER will be emitted instead.
60  *
61  * This function will write up to 4 bytes to @out.
62  */
63 int utf8_write_char(uchar_t unicode, char *out);
64
65 /*
66  * Compute the Unicode codepoint of a UTF-16 surrogate pair.
67  *
68  * @uc should be 0xD800..0xDBFF, and @lc should be 0xDC00..0xDFFF.
69  * If they aren't, this function returns REPLACEMENT_CHARACTER.
70  */
71 uchar_t from_surrogate_pair(unsigned int uc, unsigned int lc);
72
73 /*
74  * Construct a UTF-16 surrogate pair given a Unicode codepoint.
75  *
76  * @unicode should be U+10000..U+10FFFF.
77  * If it's not, this function returns false,
78  * and sets *uc and *lc to REPLACEMENT_CHARACTER.
79  */
80 bool to_surrogate_pair(uchar_t unicode, unsigned int *uc, unsigned int *lc);
81
82 #endif