Blob


1 /* See LICENSE file for copyright and license details. */
2 #ifndef UTIL_H
3 #define UTIL_H
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
9 #include "../gen/types.h"
10 #include "../grapheme.h"
12 #undef MIN
13 #define MIN(x,y) ((x) < (y) ? (x) : (y))
14 #undef LEN
15 #define LEN(x) (sizeof(x) / sizeof(*(x)))
17 #undef likely
18 #undef unlikely
19 #ifdef __has_builtin
20 #if __has_builtin(__builtin_expect)
21 #define likely(expr) __builtin_expect(!!(expr), 1)
22 #define unlikely(expr) __builtin_expect(!!(expr), 0)
23 #else
24 #define likely(expr) (expr)
25 #define unlikely(expr) (expr)
26 #endif
27 #else
28 #define likely(expr) (expr)
29 #define unlikely(expr) (expr)
30 #endif
32 /*
33 * Herodotus, the ancient greek historian and geographer,
34 * was criticized for including legends and other fantastic
35 * accounts into his works, among others by his contemporary
36 * Thucydides.
37 *
38 * The Herodotus readers and writers are tailored towards the needs
39 * of the library interface, doing all the dirty work behind the
40 * scenes. While the reader is relatively faithful in his accounts,
41 * the Herodotus writer will never fail and always claim to write the
42 * data. Internally, it only writes as much as it can, and will simply
43 * keep account of the rest. This way, we can properly signal truncation.
44 *
45 * In this sense, explaining the naming, the writer is always a bit
46 * inaccurate in his accounts.
47 *
48 */
49 enum herodotus_status {
50 HERODOTUS_STATUS_SUCCESS,
51 HERODOTUS_STATUS_END_OF_BUFFER,
52 HERODOTUS_STATUS_SOFT_LIMIT_REACHED,
53 };
55 enum herodotus_type {
56 HERODOTUS_TYPE_CODEPOINT,
57 HERODOTUS_TYPE_UTF8,
58 };
60 typedef struct herodotus_reader {
61 enum herodotus_type type;
62 const void *src;
63 size_t srclen;
64 size_t off;
65 bool terminated_by_null;
66 size_t soft_limit[10];
67 } HERODOTUS_READER;
69 typedef struct herodotus_writer {
70 enum herodotus_type type;
71 void *dest;
72 size_t destlen;
73 size_t off;
74 size_t first_unwritable_offset;
75 } HERODOTUS_WRITER;
77 struct proper {
78 /*
79 * prev_prop[1] prev_prop[0] | next_prop[0] next_prop[1]
80 */
81 struct {
82 uint_least8_t prev_prop[2];
83 uint_least8_t next_prop[2];
84 } raw, skip;
85 HERODOTUS_READER mid_reader, raw_reader, skip_reader;
86 void *state;
87 uint_least8_t no_prop;
88 uint_least8_t (*get_break_prop)(uint_least32_t);
89 bool (*is_skippable_prop)(uint_least8_t);
90 void (*skip_shift_callback)(uint_least8_t, void *);
91 };
93 void herodotus_reader_init(HERODOTUS_READER *, enum herodotus_type,
94 const void *, size_t);
95 void herodotus_reader_copy(const HERODOTUS_READER *, HERODOTUS_READER *);
96 void herodotus_reader_push_advance_limit(HERODOTUS_READER *, size_t);
97 void herodotus_reader_pop_limit(HERODOTUS_READER *);
98 size_t herodotus_reader_number_read(const HERODOTUS_READER *);
99 size_t herodotus_reader_next_word_break(const HERODOTUS_READER *);
100 size_t herodotus_reader_next_codepoint_break(const HERODOTUS_READER *);
101 enum herodotus_status herodotus_read_codepoint(HERODOTUS_READER *, bool, uint_least32_t *);
103 void herodotus_writer_init(HERODOTUS_WRITER *, enum herodotus_type, void *,
104 size_t);
105 void herodotus_writer_nul_terminate(HERODOTUS_WRITER *);
106 size_t herodotus_writer_number_written(const HERODOTUS_WRITER *);
107 void herodotus_write_codepoint(HERODOTUS_WRITER *, uint_least32_t);
109 void proper_init(const HERODOTUS_READER *, void *, uint_least8_t,
110 uint_least8_t (*get_break_prop)(uint_least32_t),
111 bool (*is_skippable_prop)(uint_least8_t),
112 void (*skip_shift_callback)(uint_least8_t, void *),
113 struct proper *);
114 int proper_advance(struct proper *);
116 #endif /* UTIL_H */