-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.h
230 lines (193 loc) · 5.9 KB
/
encoding.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef __ENCODING_H_
#define __ENCODING_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "ruby.h"
#include <stdbool.h>
#include "unicode/ustring.h"
#if __LITTLE_ENDIAN__
#define ENCODING_UTF16_NATIVE ENCODING_UTF16LE
#define ENCODING_UTF32_NATIVE ENCODING_UTF32LE
#define ENCODING_UTF16_NON_NATIVE ENCODING_UTF16BE
#define ENCODING_UTF32_NON_NATIVE ENCODING_UTF32BE
#else
#define ENCODING_UTF16_NATIVE ENCODING_UTF16BE
#define ENCODING_UTF32_NATIVE ENCODING_UTF32BE
#define ENCODING_UTF16_NON_NATIVE ENCODING_UTF16LE
#define ENCODING_UTF32_NON_NATIVE ENCODING_UTF32LE
#endif
#define NATIVE_UTF16_ENC(encoding) ((encoding) == encodings[ENCODING_UTF16_NATIVE])
#define NON_NATIVE_UTF16_ENC(encoding) ((encoding) == encodings[ENCODING_UTF16_NON_NATIVE])
#define UTF16_ENC(encoding) (NATIVE_UTF16_ENC(encoding) || NON_NATIVE_UTF16_ENC(encoding))
#define NATIVE_UTF32_ENC(encoding) ((encoding) == encodings[ENCODING_UTF32_NATIVE])
#define NON_NATIVE_UTF32_ENC(encoding) ((encoding) == encodings[ENCODING_UTF32_NON_NATIVE])
#define UTF32_ENC(encoding) (NATIVE_UTF32_ENC(encoding) || NON_NATIVE_UTF32_ENC(encoding))
#define BINARY_ENC(encoding) ((encoding) == encodings[ENCODING_BINARY])
typedef uint8_t str_flag_t;
typedef struct {
struct RBasic basic;
struct encoding_s *encoding;
long capacity_in_bytes;
long length_in_bytes;
union {
char *bytes;
UChar *uchars;
} data;
str_flag_t flags;
} string_t;
typedef struct {
long start_offset_in_bytes;
long end_offset_in_bytes;
} character_boundaries_t;
typedef struct {
void (*update_flags)(string_t *);
void (*make_data_binary)(string_t *);
bool (*try_making_data_uchars)(string_t *);
long (*length)(string_t *, bool);
long (*bytesize)(string_t *);
character_boundaries_t (*get_character_boundaries)(string_t *, long, bool);
long (*offset_in_bytes_to_index)(string_t *, long, bool);
} encoding_methods_t;
typedef struct encoding_s {
struct RBasic basic;
unsigned int index;
const char *public_name;
const char **aliases;
unsigned int aliases_count;
unsigned char min_char_size;
bool single_byte_encoding : 1;
bool ascii_compatible : 1;
encoding_methods_t methods;
void *private_data;
} encoding_t;
enum {
ENCODING_BINARY = 0,
ENCODING_ASCII,
ENCODING_UTF8,
ENCODING_UTF16BE,
ENCODING_UTF16LE,
ENCODING_UTF32BE,
ENCODING_UTF32LE,
ENCODING_ISO8859_1,
ENCODING_MACROMAN,
//ENCODING_EUCJP,
//ENCODING_SJIS,
//ENCODING_CP932,
ENCODINGS_COUNT
};
extern encoding_t *encodings[ENCODINGS_COUNT];
extern VALUE rb_cMREncoding;
#define STRING_HAS_SUPPLEMENTARY 0x020
#define STRING_HAS_SUPPLEMENTARY_SET 0x010
#define STRING_ASCII_ONLY 0x008
#define STRING_ASCII_ONLY_SET 0x010
#define STRING_ASCII_ONLY 0x008
#define STRING_VALID_ENCODING_SET 0x004
#define STRING_VALID_ENCODING 0x002
#define STRING_STORED_IN_UCHARS 0x001
#define STRING_REQUIRED_FLAGS STRING_STORED_IN_UCHARS
#define STR(x) ((string_t *)(x))
#define BYTES_TO_UCHARS(len) ((len) / sizeof(UChar))
#define UCHARS_TO_BYTES(len) ((len) * sizeof(UChar))
#define ODD_NUMBER(x) ((x) & 0x1)
static inline long
div_round_up(long a, long b)
{
return ((a) + (b - 1)) / b;
}
void
str_update_flags(string_t *self);
static inline void
str_unset_facultative_flags(string_t *self)
{
self->flags &= ~STRING_HAS_SUPPLEMENTARY_SET & ~STRING_ASCII_ONLY_SET & ~STRING_VALID_ENCODING_SET;
}
static inline bool
str_known_to_have_an_invalid_encoding(string_t *self)
{
return (self->flags & (STRING_VALID_ENCODING_SET | STRING_VALID_ENCODING)) == STRING_VALID_ENCODING_SET;
}
static inline bool
str_known_not_to_have_any_supplementary(string_t *self)
{
return (self->flags & (STRING_HAS_SUPPLEMENTARY_SET | STRING_HAS_SUPPLEMENTARY)) == STRING_HAS_SUPPLEMENTARY_SET;
}
static inline bool
str_check_flag_and_update_if_needed(string_t *self, str_flag_t flag_set, str_flag_t flag)
{
if (!(self->flags & flag_set)) {
str_update_flags(self);
assert(self->flags & flag_set);
}
return self->flags & flag;
}
static inline bool
str_is_valid_encoding(string_t *self)
{
return str_check_flag_and_update_if_needed(self, STRING_VALID_ENCODING_SET, STRING_VALID_ENCODING);
}
static inline bool
str_is_ascii_only(string_t *self)
{
return str_check_flag_and_update_if_needed(self, STRING_ASCII_ONLY_SET, STRING_ASCII_ONLY);
}
static inline bool
str_is_ruby_ascii_only(string_t *self)
{
// for MRI, a string in a non-ASCII-compatible encoding (like UTF-16)
// containing only ASCII characters is not "ASCII only" though for us it is internally
if (!self->encoding->ascii_compatible) {
return false;
}
return str_is_ascii_only(self);
}
static inline bool
str_is_stored_in_uchars(string_t *self)
{
return self->flags & STRING_STORED_IN_UCHARS;
}
static inline void
str_negate_stored_in_uchars(string_t *self)
{
self->flags ^= STRING_STORED_IN_UCHARS;
}
static inline void
str_set_stored_in_uchars(string_t *self, bool status)
{
if (status) {
self->flags |= STRING_STORED_IN_UCHARS;
}
else {
self->flags &= ~STRING_STORED_IN_UCHARS;
}
}
static inline void
str_set_facultative_flag(string_t *self, bool status, str_flag_t flag_set, str_flag_t flag)
{
if (status) {
self->flags = self->flags | flag_set | flag;
}
else {
self->flags = (self->flags | flag_set) & ~flag;
}
}
static inline void
str_set_has_supplementary(string_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_HAS_SUPPLEMENTARY_SET, STRING_HAS_SUPPLEMENTARY);
}
static inline void
str_set_ascii_only(string_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_ASCII_ONLY_SET, STRING_ASCII_ONLY);
}
static inline void
str_set_valid_encoding(string_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_VALID_ENCODING_SET, STRING_VALID_ENCODING);
}
#if defined(__cplusplus)
}
#endif
#endif /* __ENCODING_H_ */