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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
/**********************************************************************
encoding.c -
$Author$
$Date$
created at: Thu May 24 17:23:27 JST 2007
Copyright (C) 2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "regenc.h"
#include <ctype.h>
static ID id_encoding;
static VALUE rb_cEncoding;
struct rb_encoding_entry {
const char *name;
rb_encoding *enc;
};
static struct rb_encoding_entry *enc_table;
static int enc_table_size;
static st_table *enc_table_alias;
#define ENC_UNINITIALIZED (&rb_cEncoding)
#define enc_initialized_p(enc) ((enc)->auxiliary_data != &rb_cEncoding)
#define ENC_FROM_ENCODING(enc) ((VALUE)(enc)->auxiliary_data)
static void
enc_mark(void *ptr)
{
}
static VALUE
enc_new(rb_encoding *encoding)
{
VALUE enc = Data_Wrap_Struct(rb_cEncoding, enc_mark, -1, encoding);
encoding->auxiliary_data = (void *)enc;
return enc;
}
VALUE
rb_enc_from_encoding(rb_encoding *encoding)
{
VALUE enc;
if (!encoding) return 0;
if (enc_initialized_p(encoding))
return ENC_FROM_ENCODING(encoding);
enc = enc_new(encoding);
rb_enc_associate(enc, encoding);
return enc;
}
static rb_encoding *
enc_check_encoding(VALUE obj)
{
if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA ||
RDATA(obj)->dmark != enc_mark) {
return 0;
}
return RDATA(obj)->data;
}
static rb_encoding *
enc_get_encoding(VALUE obj)
{
rb_encoding *enc = enc_check_encoding(obj);
if (!enc) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
rb_obj_classname(obj));
}
return enc;
}
int
rb_to_encoding_index(VALUE enc)
{
rb_encoding *encoding;
if (NIL_P(enc)) return 0;
encoding = enc_check_encoding(enc);
if (encoding) {
return rb_enc_to_index(encoding);
}
else {
return rb_enc_find_index(StringValueCStr(enc));
}
}
rb_encoding *
rb_to_encoding(VALUE enc)
{
rb_encoding *encoding;
int idx;
if (NIL_P(enc)) return rb_enc_from_index(0);
encoding = enc_check_encoding(enc);
if (encoding) return encoding;
if ((idx = rb_enc_find_index(StringValueCStr(enc))) < 0) {
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
}
return rb_enc_from_index(idx);
}
void
rb_gc_mark_encodings(void)
{
int i;
for (i = 0; i < enc_table_size; ++i) {
rb_encoding *enc = enc_table[i].enc;
if (enc && enc_initialized_p(enc)) {
rb_gc_mark(ENC_FROM_ENCODING(enc));
}
}
}
int
rb_enc_register(const char *name, rb_encoding *encoding)
{
struct rb_encoding_entry *ent;
int newsize;
if (!enc_table) {
ent = malloc(sizeof(*enc_table));
newsize = 1;
}
else {
newsize = enc_table_size + 1;
ent = realloc(enc_table, sizeof(*enc_table)*newsize);
}
if (!ent) return -1;
enc_table = ent;
enc_table_size = newsize;
ent = &enc_table[--newsize];
ent->name = name;
*(ent->enc = malloc(sizeof(rb_encoding))) = *encoding;
encoding = ent->enc;
encoding->name = name;
if (rb_cEncoding) {
VALUE enc = enc_new(encoding);
rb_enc_associate_index(enc, newsize);
}
else {
encoding->auxiliary_data = ENC_UNINITIALIZED;
}
return newsize;
}
int
rb_enc_alias(const char *alias, const char *orig)
{
st_data_t data;
int idx;
if (!enc_table_alias) {
enc_table_alias = st_init_strcasetable();
}
if ((idx = rb_enc_find_index(orig)) < 0) {
if (!st_lookup(enc_table_alias, (st_data_t)orig, &data))
return -1;
idx = (int)data;
}
st_insert(enc_table_alias, (st_data_t)alias, (st_data_t)idx);
return idx;
}
void
rb_enc_init(void)
{
#define ENC_REGISTER(enc) rb_enc_register(rb_enc_name(enc), enc)
ENC_REGISTER(ONIG_ENCODING_ASCII);
ENC_REGISTER(ONIG_ENCODING_EUC_JP);
ENC_REGISTER(ONIG_ENCODING_SJIS);
ENC_REGISTER(ONIG_ENCODING_UTF8);
#undef ENC_REGISTER
rb_enc_alias("ASCII", rb_enc_name(ONIG_ENCODING_ASCII));
rb_enc_alias("BINARY", rb_enc_name(ONIG_ENCODING_ASCII));
rb_enc_alias("US-ASCII", rb_enc_name(ONIG_ENCODING_ASCII)); /* will be defined separately in future. */
rb_enc_alias("SJIS", rb_enc_name(ONIG_ENCODING_SJIS));
}
rb_encoding *
rb_enc_from_index(int index)
{
if (!enc_table) {
rb_enc_init();
}
if (index < 0 || enc_table_size <= index) {
return 0;
}
return enc_table[index].enc;
}
int
rb_enc_find_index(const char *name)
{
int i;
st_data_t alias = 0;
if (!name) return -1;
if (!enc_table) {
rb_enc_init();
}
for (i=0; i<enc_table_size; i++) {
if (strcasecmp(name, enc_table[i].name) == 0) {
return i;
}
}
if (!alias && enc_table_alias) {
if (st_lookup(enc_table_alias, (st_data_t)name, &alias)) {
return (int)alias;
}
}
return -1;
}
rb_encoding *
rb_enc_find(const char *name)
{
int idx = rb_enc_find_index(name);
if (idx < 0) idx = 0;
return rb_enc_from_index(idx);
}
static int
enc_capable(VALUE obj)
{
if (IMMEDIATE_P(obj)) return Qfalse;
switch (BUILTIN_TYPE(obj)) {
case T_STRING:
case T_REGEXP:
case T_FILE:
return Qtrue;
case T_DATA:
if (RDATA(obj)->dmark == enc_mark) return Qtrue;
default:
return Qfalse;
}
}
static void
enc_check_capable(VALUE x)
{
if (!enc_capable(x)) {
const char *etype;
if (NIL_P(x)) {
etype = "nil";
}
else if (FIXNUM_P(x)) {
etype = "Fixnum";
}
else if (SYMBOL_P(x)) {
etype = "Symbol";
}
else if (rb_special_const_p(x)) {
etype = RSTRING_PTR(rb_obj_as_string(x));
}
else {
etype = rb_obj_classname(x);
}
rb_raise(rb_eTypeError, "wrong argument type %s (not encode capable)", etype);
}
}
ID
rb_id_encoding(void)
{
if (!id_encoding) {
id_encoding = rb_intern("encoding");
}
return id_encoding;
}
void
rb_enc_associate_index(VALUE obj, int idx)
{
enc_check_capable(obj);
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
!rb_enc_asciicompat(rb_enc_from_index(idx))) {
ENC_CODERANGE_CLEAR(obj);
}
if (idx < ENCODING_INLINE_MAX) {
ENCODING_SET(obj, idx);
return;
}
ENCODING_SET(obj, ENCODING_INLINE_MAX);
rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
return;
}
int
rb_enc_to_index(rb_encoding *enc)
{
int i;
if (!enc) return 0;
for (i=0; i<enc_table_size; i++) {
if (enc_table[i].enc == enc) {
return i;
}
}
return 0;
}
void
rb_enc_associate(VALUE obj, rb_encoding *enc)
{
rb_enc_associate_index(obj, rb_enc_to_index(enc));
}
int
rb_enc_get_index(VALUE obj)
{
int i;
if (!enc_capable(obj)) return -1;
i = ENCODING_GET(obj);
if (i == ENCODING_INLINE_MAX) {
VALUE iv;
iv = rb_ivar_get(obj, rb_id_encoding());
i = NUM2INT(iv);
}
return i;
}
rb_encoding*
rb_enc_get(VALUE obj)
{
return rb_enc_from_index(rb_enc_get_index(obj));
}
rb_encoding*
rb_enc_check(VALUE str1, VALUE str2)
{
rb_encoding *enc = rb_enc_compatible(str1, str2);
if (!enc)
rb_raise(rb_eArgError, "character encodings differ");
return enc;
}
rb_encoding*
rb_enc_compatible(VALUE str1, VALUE str2)
{
int idx1, idx2;
rb_encoding *enc;
idx1 = rb_enc_get_index(str1);
idx2 = rb_enc_get_index(str2);
if (idx1 == idx2) {
return rb_enc_from_index(idx1);
}
if (BUILTIN_TYPE(str1) != T_STRING) {
VALUE tmp = str1;
str1 = str2;
str2 = tmp;
}
if (BUILTIN_TYPE(str1) == T_STRING) {
int cr1, cr2;
cr1 = rb_enc_str_coderange(str1);
if (BUILTIN_TYPE(str2) == T_STRING) {
cr2 = rb_enc_str_coderange(str2);
if (cr1 != cr2) {
/* may need to handle ENC_CODERANGE_BROKEN */
if (cr1 == ENC_CODERANGE_SINGLE) return rb_enc_from_index(idx2);
if (cr2 == ENC_CODERANGE_SINGLE) return rb_enc_from_index(idx1);
}
if (cr1 == ENC_CODERANGE_SINGLE) return rb_enc_from_index(0);
}
if (cr1 == ENC_CODERANGE_SINGLE &&
rb_enc_asciicompat(enc = rb_enc_from_index(idx2)))
return enc;
}
return 0;
}
void
rb_enc_copy(VALUE obj1, VALUE obj2)
{
rb_enc_associate_index(obj1, rb_enc_get_index(obj2));
}
/*
* call-seq:
* obj.encoding => encoding
*
* Returns the Encoding object that represents the encoding of obj.
*/
VALUE
rb_obj_encoding(VALUE obj)
{
rb_encoding *enc = rb_enc_get(obj);
if (!enc) {
rb_raise(rb_eTypeError, "unknown encoding");
}
return rb_enc_from_encoding(enc);
}
char*
rb_enc_nth(const char *p, const char *e, int nth, rb_encoding *enc)
{
int c;
if (rb_enc_mbmaxlen(enc) == 1) {
p += nth;
}
else if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
p += nth * rb_enc_mbmaxlen(enc);
}
else {
for (c=0; p<e && nth--; c++) {
int n = rb_enc_mbclen(p, e, enc);
if (n == 0) return 0;
p += n;
}
}
return (char*)p;
}
long
rb_enc_strlen(const char *p, const char *e, rb_encoding *enc)
{
long c;
if (rb_enc_mbmaxlen(enc) == rb_enc_mbminlen(enc)) {
return (e - p) / rb_enc_mbminlen(enc);
}
for (c=0; p<e; c++) {
int n = rb_enc_mbclen(p, e, enc);
if (n == 0) return -1;
p += n;
}
return c;
}
int
rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
{
int n = ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
if (n == 0) {
rb_raise(rb_eArgError, "invalid mbstring sequence");
}
return n;
}
int
rb_enc_codelen(int c, rb_encoding *enc)
{
int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
if (n == 0) {
rb_raise(rb_eArgError, "invalid mbstring sequence");
}
return n;
}
int
rb_enc_toupper(int c, rb_encoding *enc)
{
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
}
int
rb_enc_tolower(int c, rb_encoding *enc)
{
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
}
static VALUE
enc_to_s(VALUE self)
{
return rb_sprintf("<%s:%s>", rb_obj_classname(self),
rb_enc_name(enc_get_encoding(self)));
}
static VALUE
enc_name(VALUE self)
{
return rb_str_new2(rb_enc_name(enc_get_encoding(self)));
}
static VALUE
enc_list(VALUE klass)
{
VALUE ary = rb_ary_new2(enc_table_size);
int i;
for (i = 0; i < enc_table_size; ++i) {
rb_encoding *enc = enc_table[i].enc;
if (enc) {
rb_ary_push(ary, rb_enc_from_encoding(enc));
}
}
return ary;
}
static VALUE
enc_find(VALUE klass, VALUE enc)
{
int idx;
if (SYMBOL_P(enc)) enc = rb_id2str(SYM2ID(enc));
idx = rb_enc_find_index(StringValueCStr(enc));
if (idx < 0) {
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
}
return rb_enc_from_encoding(rb_enc_from_index(idx));
}
static VALUE
enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
{
rb_encoding *enc = rb_enc_compatible(str1, str2);
VALUE encoding = Qnil;
if (!enc || !(encoding = rb_enc_from_encoding(enc)))
encoding = Qnil;
return encoding;
}
/* :nodoc: */
static VALUE
enc_dump(int argc, VALUE *argv, VALUE self)
{
rb_scan_args(argc, argv, "01", 0);
return enc_name(self);
}
/* :nodoc: */
static VALUE
enc_load(VALUE klass, VALUE str)
{
return enc_find(klass, str);
}
static int primary_encoding_index;
rb_encoding *
rb_enc_default(void)
{
if (!enc_table) {
rb_enc_init();
}
return enc_table[0].enc;
}
rb_encoding *
rb_enc_primary(void)
{
return rb_enc_from_index(primary_encoding_index);
}
VALUE
rb_get_primary_encoding(void)
{
return rb_enc_from_encoding(rb_enc_primary());
}
static VALUE
get_primary_encoding(VALUE klass)
{
return rb_get_primary_encoding();
}
void
rb_set_primary_encoding(VALUE encoding)
{
rb_to_encoding(encoding);
primary_encoding_index = ENCODING_GET(encoding);
}
static void
set_encoding_const(const char *name, rb_encoding *enc)
{
VALUE encoding = rb_enc_from_encoding(enc);
char *s = (char *)name;
int haslower = 0, valid = 0;
if (ISUPPER(*s)) {
while (*++s && (ISALNUM(*s) || *s == '_')) {
if (ISLOWER(*s)) haslower = 1;
}
}
if (!*s) {
valid = 1;
rb_define_const(rb_cEncoding, name, encoding);
}
if (!valid || haslower) {
int len = strlen(name) + 1;
if (!haslower) {
while (!ISLOWER(*s) && *++s);
if (*s) haslower = 1;
}
MEMCPY(s = ALLOCA_N(char, len), name, char, len);
name = s;
if (!valid) {
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
for (; *s; ++s) {
if (!ISALNUM(*s)) *s = '_';
}
rb_define_const(rb_cEncoding, name, encoding);
}
if (haslower) {
for (s = (char *)name; *s; ++s) {
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
}
rb_define_const(rb_cEncoding, name, encoding);
}
}
}
static int
set_encoding_alias(st_data_t name, st_data_t orig, st_data_t arg)
{
rb_encoding *enc = rb_enc_from_index((int)orig);
set_encoding_const((const char *)name, enc);
return ST_CONTINUE;
}
void
Init_Encoding(void)
{
int i;
rb_cEncoding = rb_define_class("Encoding", rb_cObject);
rb_undef_alloc_func(rb_cEncoding);
rb_define_method(rb_cEncoding, "to_s", enc_to_s, 0);
rb_define_method(rb_cEncoding, "inspect", enc_to_s, 0);
rb_define_method(rb_cEncoding, "name", enc_name, 0);
rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
rb_define_singleton_method(rb_cEncoding, "primary_encoding", get_primary_encoding, 0);
for (i = 0; i < enc_table_size; ++i) {
rb_encoding *enc = enc_table[i].enc;
if (enc) set_encoding_const(rb_enc_name(enc), enc);
}
st_foreach(enc_table_alias, set_encoding_alias, 0);
}
|