XenevaOS
Loading...
Searching...
No Matches
libunicode.h
Go to the documentation of this file.
1/*
2 * Unicode utilities
3 *
4 * Copyright (c) 2017-2018 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#ifndef LIBUNICODE_H
25#define LIBUNICODE_H
26
27#include <stdint.h>
28
29/* unicode standard version */
30#define LIBUNICODE_UNICODE_VERSION_MAJOR 17
31#define LIBUNICODE_UNICODE_VERSION_MINOR 0
32#define LIBUNICODE_UNICODE_VERSION_PATCH 0
33
34/* define it to include all the unicode tables (40KB larger) */
35#define CONFIG_ALL_UNICODE
36
37#define LRE_CC_RES_LEN_MAX 3
38
39/* char ranges */
40
41typedef struct {
42 int len; /* in points, always even */
43 int size;
44 uint32_t *points; /* points sorted by increasing value */
46 void *(*realloc_func)(void *opaque, void *ptr, size_t size);
47} CharRange;
48
55
56void cr_init(CharRange *cr, void *mem_opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size));
57void cr_free(CharRange *cr);
58int cr_realloc(CharRange *cr, int size);
59int cr_copy(CharRange *cr, const CharRange *cr1);
60
61static inline int cr_add_point(CharRange *cr, uint32_t v)
62{
63 if (cr->len >= cr->size) {
64 if (cr_realloc(cr, cr->len + 1))
65 return -1;
66 }
67 cr->points[cr->len++] = v;
68 return 0;
69}
70
71static inline int cr_add_interval(CharRange *cr, uint32_t c1, uint32_t c2)
72{
73 if ((cr->len + 2) > cr->size) {
74 if (cr_realloc(cr, cr->len + 2))
75 return -1;
76 }
77 cr->points[cr->len++] = c1;
78 cr->points[cr->len++] = c2;
79 return 0;
80}
81
82int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
83 const uint32_t *b_pt, int b_len, int op);
84int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op);
85
86static inline int cr_union_interval(CharRange *cr, uint32_t c1, uint32_t c2)
87{
88 uint32_t b_pt[2];
89 b_pt[0] = c1;
90 b_pt[1] = c2 + 1;
91 return cr_op1(cr, b_pt, 2, CR_OP_UNION);
92}
93
94int cr_invert(CharRange *cr);
95
96int cr_regexp_canonicalize(CharRange *cr, int is_unicode);
97
104
105int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
107 void *opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size));
108
109/* Unicode character range functions */
110
111int unicode_script(CharRange *cr, const char *script_name, int is_ext);
112int unicode_general_category(CharRange *cr, const char *gc_name);
113int unicode_prop(CharRange *cr, const char *prop_name);
114
115typedef void UnicodeSequencePropCB(void *opaque, const uint32_t *buf, int len);
116int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque,
117 CharRange *cr);
118
119int lre_case_conv(uint32_t *res, uint32_t c, int conv_type);
120int lre_canonicalize(uint32_t c, int is_unicode);
121
122/* Code point type categories */
123enum {
124 UNICODE_C_SPACE = (1 << 0),
125 UNICODE_C_DIGIT = (1 << 1),
126 UNICODE_C_UPPER = (1 << 2),
127 UNICODE_C_LOWER = (1 << 3),
128 UNICODE_C_UNDER = (1 << 4),
131};
132extern uint8_t const lre_ctype_bits[256];
133
134/* zero or non-zero return value */
139
140static inline int lre_is_space_byte(uint8_t c) {
142}
143
144static inline int lre_is_id_start_byte(uint8_t c) {
147}
148
149static inline int lre_is_id_continue_byte(uint8_t c) {
153}
154
155static inline int lre_is_word_byte(uint8_t c) {
158}
159
161
162static inline int lre_is_space(uint32_t c) {
163 if (c < 256)
164 return lre_is_space_byte(c);
165 else
166 return lre_is_space_non_ascii(c);
167}
168
169static inline int lre_js_is_ident_first(uint32_t c) {
170 if (c < 128) {
171 return lre_is_id_start_byte(c);
172 } else {
173#ifdef CONFIG_ALL_UNICODE
174 return lre_is_id_start(c);
175#else
176 return !lre_is_space_non_ascii(c);
177#endif
178 }
179}
180
181static inline int lre_js_is_ident_next(uint32_t c) {
182 if (c < 128) {
183 return lre_is_id_continue_byte(c);
184 } else {
185 /* ZWNJ and ZWJ are accepted in identifiers */
186 if (c >= 0x200C && c <= 0x200D)
187 return TRUE;
188#ifdef CONFIG_ALL_UNICODE
189 return lre_is_id_continue(c);
190#else
191 return !lre_is_space_non_ascii(c);
192#endif
193 }
194}
195
196#endif /* LIBUNICODE_H */
unsigned int uint32_t
Definition acefiex.h:163
unsigned char uint8_t
Definition acefiex.h:161
#define TRUE
Definition actypes.h:558
size_t size
Definition drm_bridge.h:1299
size_t len
Definition initial.c:23
int unicode_prop(CharRange *cr, const char *prop_name)
Definition libunicode.c:1627
int lre_canonicalize(uint32_t c, int is_unicode)
CharRangeOpEnum
Definition libunicode.h:49
@ CR_OP_SUB
Definition libunicode.h:53
@ CR_OP_UNION
Definition libunicode.h:50
@ CR_OP_XOR
Definition libunicode.h:52
@ CR_OP_INTER
Definition libunicode.h:51
int lre_is_cased(uint32_t c)
Definition libunicode.c:347
int cr_regexp_canonicalize(CharRange *cr, int is_unicode)
int unicode_sequence_prop(const char *prop_name, UnicodeSequencePropCB *cb, void *opaque, CharRange *cr)
Definition libunicode.c:2116
void cr_free(CharRange *cr)
Definition libunicode.c:401
int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len, const uint32_t *b_pt, int b_len, int op)
Definition libunicode.c:462
int lre_case_conv(uint32_t *res, uint32_t c, int conv_type)
Definition libunicode.c:156
int unicode_script(CharRange *cr, const char *script_name, int is_ext)
void cr_init(CharRange *cr, void *mem_opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size))
void UnicodeSequencePropCB(void *opaque, const uint32_t *buf, int len)
Definition libunicode.h:115
int lre_is_case_ignorable(uint32_t c)
Definition libunicode.c:372
UnicodeNormalizationEnum
Definition libunicode.h:98
@ UNICODE_NFKD
Definition libunicode.h:102
@ UNICODE_NFD
Definition libunicode.h:100
@ UNICODE_NFC
Definition libunicode.h:99
@ UNICODE_NFKC
Definition libunicode.h:101
int lre_is_space_non_ascii(uint32_t c)
Definition libunicode.c:1903
int cr_op1(CharRange *cr, const uint32_t *b_pt, int b_len, int op)
Definition libunicode.c:517
int cr_realloc(CharRange *cr, int size)
Definition libunicode.c:406
int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len, UnicodeNormalizationEnum n_type, void *opaque, void *(*realloc_func)(void *opaque, void *ptr, size_t size))
int lre_is_id_continue(uint32_t c)
Definition libunicode.c:753
int cr_copy(CharRange *cr, const CharRange *cr1)
Definition libunicode.c:423
int lre_is_id_start(uint32_t c)
Definition libunicode.c:746
@ UNICODE_C_UNDER
Definition libunicode.h:128
@ UNICODE_C_DIGIT
Definition libunicode.h:125
@ UNICODE_C_SPACE
Definition libunicode.h:124
@ UNICODE_C_XDIGIT
Definition libunicode.h:130
@ UNICODE_C_LOWER
Definition libunicode.h:127
@ UNICODE_C_UPPER
Definition libunicode.h:126
@ UNICODE_C_DOLLAR
Definition libunicode.h:129
int unicode_general_category(CharRange *cr, const char *gc_name)
Definition libunicode.c:1608
int cr_invert(CharRange *cr)
Definition libunicode.c:529
uint8_t const lre_ctype_bits[256]
Definition libunicode.c:1835
Definition libunicode.h:41
int len
Definition libunicode.h:42
void * mem_opaque
Definition libunicode.h:45
uint32_t * points
Definition libunicode.h:44
int size
Definition libunicode.h:43