2 * TSM - Unicode Handling
4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * This file provides small helpers to make working with Unicode/UTF8/UCS4 input
30 * and output much easier.
41 #define TSM_UCS4_MAX (0x7fffffffUL)
42 #define TSM_UCS4_INVALID (TSM_UCS4_MAX + 1)
43 #define TSM_UCS4_REPLACEMENT (0xfffdUL)
44 #define TSM_UCS4_MAXLEN 10
48 struct tsm_symbol_table;
49 typedef uint32_t tsm_symbol_t;
51 extern const tsm_symbol_t tsm_symbol_default;
53 int tsm_symbol_table_new(struct tsm_symbol_table **out);
54 void tsm_symbol_table_ref(struct tsm_symbol_table *tbl);
55 void tsm_symbol_table_unref(struct tsm_symbol_table *tbl);
57 tsm_symbol_t tsm_symbol_make(uint32_t ucs4);
58 tsm_symbol_t tsm_symbol_append(struct tsm_symbol_table *tbl,
59 tsm_symbol_t sym, uint32_t ucs4);
60 const uint32_t *tsm_symbol_get(struct tsm_symbol_table *tbl,
61 tsm_symbol_t *sym, size_t *size);
62 unsigned int tsm_symbol_get_width(struct tsm_symbol_table *tbl,
65 /* ucs4 to utf8 converter */
67 unsigned int tsm_ucs4_get_width(uint32_t ucs4);
68 size_t tsm_ucs4_to_utf8(uint32_t ucs4, char *out);
69 char *tsm_ucs4_to_utf8_alloc(const uint32_t *ucs4, size_t len, size_t *len_out);
71 /* utf8 state machine */
75 enum tsm_utf8_mach_state {
84 int tsm_utf8_mach_new(struct tsm_utf8_mach **out);
85 void tsm_utf8_mach_free(struct tsm_utf8_mach *mach);
87 int tsm_utf8_mach_feed(struct tsm_utf8_mach *mach, char c);
88 uint32_t tsm_utf8_mach_get(struct tsm_utf8_mach *mach);
89 void tsm_utf8_mach_reset(struct tsm_utf8_mach *mach);
91 #endif /* TSM_UNICODE_H */