forked from MacRuby/MacRuby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.h
322 lines (278 loc) · 8.38 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* MacRuby implementation of Ruby 1.9 String.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2007-2010, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*/
#ifndef __ENCODING_H_
#define __ENCODING_H_
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(__cplusplus)
# include "unicode/unistr.h"
#else
# include "unicode/ustring.h"
#endif
#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) == rb_encodings[ENCODING_UTF16_NATIVE])
#define NON_NATIVE_UTF16_ENC(encoding) \
((encoding) == rb_encodings[ENCODING_UTF16_NON_NATIVE])
#define UTF16_ENC(encoding) \
(NATIVE_UTF16_ENC(encoding) || NON_NATIVE_UTF16_ENC(encoding))
#define NATIVE_UTF32_ENC(encoding) \
((encoding) == rb_encodings[ENCODING_UTF32_NATIVE])
#define NON_NATIVE_UTF32_ENC(encoding) \
((encoding) == rb_encodings[ENCODING_UTF32_NON_NATIVE])
#define UTF32_ENC(encoding) \
(NATIVE_UTF32_ENC(encoding) || NON_NATIVE_UTF32_ENC(encoding))
#define BINARY_ENC(encoding) ((encoding) == rb_encodings[ENCODING_BINARY])
typedef uint8_t str_flag_t;
typedef struct {
struct RBasic basic;
struct rb_encoding *encoding;
long capacity_in_bytes;
long length_in_bytes;
union {
char *bytes;
UChar *uchars;
} data;
str_flag_t flags;
} rb_str_t;
#define RSTR(x) ((rb_str_t *)x)
static inline bool
rb_klass_is_rstr(VALUE klass)
{
do {
if (klass == rb_cRubyString) {
return true;
}
if (klass == rb_cNSString) {
return false;
}
klass = RCLASS_SUPER(klass);
}
while (klass != 0);
return false;
}
#define IS_RSTR(x) (rb_klass_is_rstr(*(VALUE *)x))
static inline void
rstr_modify(VALUE str)
{
const long mask = RBASIC(str)->flags;
if ((mask & FL_FREEZE) == FL_FREEZE) {
rb_raise(rb_eRuntimeError, "can't modify frozen/immutable string");
}
if ((mask & FL_TAINT) == FL_TAINT && rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "Insecure: can't modify string");
}
}
static inline void
rstr_frozen_check(VALUE str)
{
const long mask = RBASIC(str)->flags;
if ((mask & FL_FREEZE) == FL_FREEZE) {
rb_raise(rb_eRuntimeError, "string frozen");
}
}
typedef struct {
long start_offset_in_bytes;
long end_offset_in_bytes;
} character_boundaries_t;
typedef struct {
void (*update_flags)(rb_str_t *);
void (*make_data_binary)(rb_str_t *);
bool (*try_making_data_uchars)(rb_str_t *);
long (*length)(rb_str_t *, bool);
long (*bytesize)(rb_str_t *);
character_boundaries_t (*get_character_boundaries)(rb_str_t *, long, bool);
long (*offset_in_bytes_to_index)(rb_str_t *, long, bool);
} encoding_methods_t;
typedef struct rb_encoding {
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;
} rb_encoding_t;
#define RENC(x) ((rb_encoding_t *)(x))
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 rb_encoding_t *rb_encodings[ENCODINGS_COUNT];
#define STRING_HAS_SUPPLEMENTARY 0x020
#define STRING_HAS_SUPPLEMENTARY_SET 0x010
#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 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(rb_str_t *self);
static inline void
str_unset_facultative_flags(rb_str_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(rb_str_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(rb_str_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(rb_str_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(rb_str_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(rb_str_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(rb_str_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(rb_str_t *self)
{
return self->flags & STRING_STORED_IN_UCHARS;
}
static inline void
str_negate_stored_in_uchars(rb_str_t *self)
{
self->flags ^= STRING_STORED_IN_UCHARS;
}
static inline void
str_set_stored_in_uchars(rb_str_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(rb_str_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(rb_str_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_HAS_SUPPLEMENTARY_SET,
STRING_HAS_SUPPLEMENTARY);
}
static inline void
str_set_ascii_only(rb_str_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_ASCII_ONLY_SET,
STRING_ASCII_ONLY);
}
static inline void
str_set_valid_encoding(rb_str_t *self, bool status)
{
str_set_facultative_flag(self, status, STRING_VALID_ENCODING_SET,
STRING_VALID_ENCODING);
}
void rb_str_NSCoder_encode(void *coder, VALUE str, const char *key);
VALUE rb_str_NSCoder_decode(void *coder, const char *key);
VALUE mr_enc_s_is_compatible(VALUE klass, SEL sel, VALUE str1, VALUE str2);
VALUE rb_str_intern_fast(VALUE str);
VALUE rstr_aref(VALUE str, SEL sel, int argc, VALUE *argv);
VALUE rstr_swapcase(VALUE str, SEL sel);
VALUE rstr_capitalize(VALUE str, SEL sel);
VALUE rstr_upcase(VALUE str, SEL sel);
VALUE rstr_downcase(VALUE str, SEL sel);
// The following functions should always been prefered over anything else,
// especially if this "else" is RSTRING_PTR and RSTRING_LEN.
// They also work on CFStrings.
VALUE rb_unicode_str_new(const UniChar *ptr, const size_t len);
void rb_str_get_uchars(VALUE str, UChar **chars_p, long *chars_len_p,
bool *need_free_p);
long rb_str_chars_len(VALUE str);
UChar rb_str_get_uchar(VALUE str, long pos);
void rb_str_append_uchar(VALUE str, UChar c);
void rb_str_append_uchars(VALUE str, const UChar *chars, long len);
unsigned long rb_str_hash_uchars(const UChar *chars, long chars_len);
long rb_uchar_strtol(UniChar *chars, long chars_len, long pos,
long *end_offset);
void rb_str_force_encoding(VALUE str, rb_encoding_t *encoding);
#if defined(__cplusplus)
} // extern "C"
#endif
#endif /* __ENCODING_H_ */