1 | #ifndef NU_UDB_H |
2 | #define NU_UDB_H |
3 | |
4 | #include <stdint.h> |
5 | #include <sys/types.h> |
6 | |
7 | #include <libnu/config.h> |
8 | #include <libnu/defines.h> |
9 | #include <libnu/mph.h> |
10 | #include <libnu/strings.h> |
11 | #include <libnu/utf8.h> |
12 | |
13 | /** @defgroup udb Unicode database |
14 | * |
15 | * Note: never use it directly, it is subject to change in next releases |
16 | */ |
17 | |
18 | #if defined (__cplusplus) || defined (c_plusplus) |
19 | extern "C" { |
20 | #endif |
21 | |
22 | #ifdef NU_WITH_UDB |
23 | |
24 | #define NU_UDB_DECODING_FUNCTION (nu_utf8_read) |
25 | #define nu_udb_read (nu_utf8_read) |
26 | |
27 | /** Lookup value in UDB |
28 | * |
29 | * Similar to nu_udb_lookup(), but doesn't look into COMBINED |
30 | * |
31 | * @ingroup udb |
32 | * @see nu_udb_lookup |
33 | * @return raw value from VALUES_I or 0 if value wasn't found |
34 | */ |
35 | static inline |
36 | uint32_t nu_udb_lookup_value(uint32_t codepoint, |
37 | const int16_t *G, size_t G_SIZE, |
38 | const uint32_t *VALUES_C, const uint16_t *VALUES_I) { |
39 | |
40 | uint32_t hash = nu_mph_hash(G, G_SIZE, codepoint); |
41 | uint32_t value = nu_mph_lookup(V_C: VALUES_C, V_I: VALUES_I, codepoint, hash); |
42 | |
43 | return value; |
44 | } |
45 | |
46 | /** Lookup data in UDB |
47 | * |
48 | * Returned data is encoded, therefore you need to use p = it(p, &u) to |
49 | * fetch it. Returned string might contain more than 1 codepoint. |
50 | * |
51 | * @ingroup udb |
52 | * @param codepoint unicode codepoint |
53 | * @param G first MPH table |
54 | * @param G_SIZE first table number of elements (original MPH set size) |
55 | * @param VALUES_C codepoints array |
56 | * @param VALUES_I offsets array |
57 | * @param COMBINED joined values addressed by index stored in VALUES |
58 | * @return looked up data or 0 |
59 | */ |
60 | static inline |
61 | const char* nu_udb_lookup(uint32_t codepoint, |
62 | const int16_t *G, size_t G_SIZE, |
63 | const uint32_t *VALUES_C, const uint16_t *VALUES_I, const uint8_t *COMBINED) { |
64 | |
65 | uint32_t combined_offset = nu_udb_lookup_value(codepoint, |
66 | G, G_SIZE, VALUES_C, VALUES_I); |
67 | |
68 | if (combined_offset == 0) { |
69 | return 0; |
70 | } |
71 | |
72 | return (const char *)(COMBINED + combined_offset); |
73 | } |
74 | |
75 | #endif /* NU_WITH_UDB */ |
76 | |
77 | #if defined (__cplusplus) || defined (c_plusplus) |
78 | } |
79 | #endif |
80 | |
81 | #endif /* NU_UDB_H */ |
82 | |