NFC and NFKC support
[disorder] / lib / unicode.h
1 /*
2 * This file is part of DisOrde
3 * Copyright (C) 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20 /** @file lib/unicode.h
21 * @brief Unicode support functions
22 */
23
24 #ifndef UNICODE_H
25 #define UNICODE_H
26
27 /** @brief Smart pointer into a string
28 *
29 * Iterators can be efficiently moved either forwards or back to the start of
30 * the string. They cannot (currently) efficiently be moved backwards. Their
31 * advantage is that they remember internal state to speed up boundary
32 * detection.
33 *
34 * Iterators can point to any code point of the string, or to a hypothetical
35 * post-final code point of value 0, but not outside the string.
36 */
37 typedef struct utf32_iterator_data *utf32_iterator;
38
39 char *utf32_to_utf8(const uint32_t *s, size_t ns, size_t *nd);
40 uint32_t *utf8_to_utf32(const char *s, size_t ns, size_t *nd);
41 int utf8_valid(const char *s, size_t ns);
42
43 size_t utf32_len(const uint32_t *s);
44 int utf32_cmp(const uint32_t *a, const uint32_t *b);
45
46 uint32_t *utf32_decompose_canon(const uint32_t *s, size_t ns, size_t *ndp);
47 char *utf8_decompose_canon(const char *s, size_t ns, size_t *ndp);
48
49 uint32_t *utf32_decompose_compat(const uint32_t *s, size_t ns, size_t *ndp);
50 char *utf8_decompose_compat(const char *s, size_t ns, size_t *ndp);
51
52 uint32_t *utf32_compose_canon(const uint32_t *s, size_t ns, size_t *ndp);
53 char *utf8_compose_canon(const char *s, size_t ns, size_t *ndp);
54
55 uint32_t *utf32_compose_compat(const uint32_t *s, size_t ns, size_t *ndp);
56 char *utf8_compose_compat(const char *s, size_t ns, size_t *ndp);
57
58 uint32_t *utf32_casefold_canon(const uint32_t *s, size_t ns, size_t *ndp);
59 char *utf8_casefold_canon(const char *s, size_t ns, size_t *ndp);
60
61 uint32_t *utf32_casefold_compat(const uint32_t *s, size_t ns, size_t *ndp);
62 char *utf8_casefold_compat(const char *s, size_t ns, size_t *ndp);
63
64 int utf32_is_grapheme_boundary(const uint32_t *s, size_t ns, size_t n);
65 int utf32_is_word_boundary(const uint32_t *s, size_t ns, size_t n);
66
67 utf32_iterator utf32_iterator_new(const uint32_t *s, size_t ns);
68 void utf32_iterator_destroy(utf32_iterator it);
69
70 size_t utf32_iterator_where(utf32_iterator it);
71 int utf32_iterator_set(utf32_iterator it, size_t n);
72 int utf32_iterator_advance(utf32_iterator it, size_t n);
73 uint32_t utf32_iterator_code(utf32_iterator it);
74 int utf32_iterator_grapheme_boundary(utf32_iterator it);
75 int utf32_iterator_word_boundary(utf32_iterator it);
76
77 /** @brief Convert 0-terminated UTF-32 to UTF-8
78 * @param s 0-terminated UTF-32 string
79 * @return 0-terminated UTF-8 string or 0 on error
80 *
81 * See utf32_to_utf8() for possible causes of errors.
82 */
83 static inline char *utf32nt_to_utf8(const uint32_t *s) {
84 return utf32_to_utf8(s, utf32_len(s), 0);
85 }
86
87 /** @brief Convert 0-terminated UTF-8 to UTF-32
88 * @param s 0-terminated UTF-8 string
89 * @return 0-terminated UTF-32 string or 0 on error
90 *
91 * See utf8_to_utf32() for possible causes of errors.
92 */
93 static inline uint32_t *utf8nt_to_utf32(const char *s) {
94 return utf8_to_utf32(s, strlen(s), 0);
95 }
96
97 #endif /* UNICODE_H */
98
99 /*
100 Local Variables:
101 c-basic-offset:2
102 comment-column:40
103 fill-column:79
104 indent-tabs-mode:nil
105 End:
106 */