-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.c
1894 lines (1711 loc) · 46.4 KB
/
encoding.c
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
encoding.c -
$Author$
created at: Thu May 24 17:23:27 JST 2007
Copyright (C) 2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "internal.h"
#include "regenc.h"
#include <ctype.h>
#ifndef NO_LOCALE_CHARMAP
#ifdef __CYGWIN__
#include <windows.h>
#endif
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#endif
#include "ruby/util.h"
#if defined __GNUC__ && __GNUC__ >= 4
#pragma GCC visibility push(default)
int rb_enc_register(const char *name, rb_encoding *encoding);
void rb_enc_set_base(const char *name, const char *orig);
void rb_encdb_declare(const char *name);
int rb_encdb_replicate(const char *name, const char *orig);
int rb_encdb_dummy(const char *name);
int rb_encdb_alias(const char *alias, const char *orig);
#pragma GCC visibility pop
#endif
static ID id_encoding;
VALUE rb_cEncoding;
static VALUE rb_encoding_list;
struct rb_encoding_entry {
const char *name;
rb_encoding *enc;
rb_encoding *base;
};
static struct {
struct rb_encoding_entry *list;
int count;
int size;
st_table *names;
} enc_table;
void rb_enc_init(void);
#define ENCODING_COUNT ENCINDEX_BUILTIN_MAX
#define UNSPECIFIED_ENCODING INT_MAX
#define ENCODING_NAMELEN_MAX 63
#define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX)
#define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc))
static int load_encoding(const char *name);
static size_t
enc_memsize(const void *p)
{
return 0;
}
static const rb_data_type_t encoding_data_type = {
"encoding",
{0, 0, enc_memsize,},
};
#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
static VALUE
enc_new(rb_encoding *encoding)
{
return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, encoding);
}
static VALUE
rb_enc_from_encoding_index(int idx)
{
VALUE list, enc;
if (!(list = rb_encoding_list)) {
rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
}
enc = rb_ary_entry(list, idx);
if (NIL_P(enc)) {
rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
}
return enc;
}
VALUE
rb_enc_from_encoding(rb_encoding *encoding)
{
int idx;
if (!encoding) return Qnil;
idx = ENC_TO_ENCINDEX(encoding);
return rb_enc_from_encoding_index(idx);
}
static int enc_autoload(rb_encoding *);
static int
check_encoding(rb_encoding *enc)
{
int index = rb_enc_to_index(enc);
if (rb_enc_from_index(index) != enc)
return -1;
if (enc_autoload_p(enc)) {
index = enc_autoload(enc);
}
return index;
}
static int
enc_check_encoding(VALUE obj)
{
if (SPECIAL_CONST_P(obj) || !rb_typeddata_is_kind_of(obj, &encoding_data_type)) {
return -1;
}
return check_encoding(RDATA(obj)->data);
}
static int
must_encoding(VALUE enc)
{
int index = enc_check_encoding(enc);
if (index < 0) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
rb_obj_classname(enc));
}
return index;
}
int
rb_to_encoding_index(VALUE enc)
{
int idx;
idx = enc_check_encoding(enc);
if (idx >= 0) {
return idx;
}
else if (NIL_P(enc = rb_check_string_type(enc))) {
return -1;
}
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
return -1;
}
return rb_enc_find_index(StringValueCStr(enc));
}
/* Returns encoding index or UNSPECIFIED_ENCODING */
static int
str_find_encindex(VALUE enc)
{
int idx;
StringValue(enc);
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
rb_raise(rb_eArgError, "invalid name encoding (non ASCII)");
}
idx = rb_enc_find_index(StringValueCStr(enc));
return idx;
}
static int
str_to_encindex(VALUE enc)
{
int idx = str_find_encindex(enc);
if (idx < 0) {
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
}
return idx;
}
static rb_encoding *
str_to_encoding(VALUE enc)
{
return rb_enc_from_index(str_to_encindex(enc));
}
rb_encoding *
rb_to_encoding(VALUE enc)
{
if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
return str_to_encoding(enc);
}
rb_encoding *
rb_find_encoding(VALUE enc)
{
int idx;
if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
idx = str_find_encindex(enc);
if (idx < 0) return NULL;
return rb_enc_from_index(idx);
}
void
rb_gc_mark_encodings(void)
{
}
static int
enc_table_expand(int newsize)
{
struct rb_encoding_entry *ent;
int count = newsize;
if (enc_table.size >= newsize) return newsize;
newsize = (newsize + 7) / 8 * 8;
ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize);
if (!ent) return -1;
memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size));
enc_table.list = ent;
enc_table.size = newsize;
return count;
}
static int
enc_register_at(int index, const char *name, rb_encoding *encoding)
{
struct rb_encoding_entry *ent = &enc_table.list[index];
VALUE list;
if (!valid_encoding_name_p(name)) return -1;
if (!ent->name) {
ent->name = name = strdup(name);
}
else if (STRCASECMP(name, ent->name)) {
return -1;
}
if (!ent->enc) {
ent->enc = xmalloc(sizeof(rb_encoding));
}
if (encoding) {
*ent->enc = *encoding;
}
else {
memset(ent->enc, 0, sizeof(*ent->enc));
}
encoding = ent->enc;
encoding->name = name;
encoding->ruby_encoding_index = index;
st_insert(enc_table.names, (st_data_t)name, (st_data_t)index);
list = rb_encoding_list;
if (list && NIL_P(rb_ary_entry(list, index))) {
/* initialize encoding data */
rb_ary_store(list, index, enc_new(encoding));
}
return index;
}
static int
enc_register(const char *name, rb_encoding *encoding)
{
int index = enc_table.count;
if ((index = enc_table_expand(index + 1)) < 0) return -1;
enc_table.count = index;
return enc_register_at(index - 1, name, encoding);
}
static void set_encoding_const(const char *, rb_encoding *);
int rb_enc_registered(const char *name);
int
rb_enc_register(const char *name, rb_encoding *encoding)
{
int index = rb_enc_registered(name);
if (index >= 0) {
rb_encoding *oldenc = rb_enc_from_index(index);
if (STRCASECMP(name, rb_enc_name(oldenc))) {
index = enc_register(name, encoding);
}
else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
enc_register_at(index, name, encoding);
}
else {
rb_raise(rb_eArgError, "encoding %s is already registered", name);
}
}
else {
index = enc_register(name, encoding);
set_encoding_const(name, rb_enc_from_index(index));
}
return index;
}
void
rb_encdb_declare(const char *name)
{
int idx = rb_enc_registered(name);
if (idx < 0) {
idx = enc_register(name, 0);
}
set_encoding_const(name, rb_enc_from_index(idx));
}
static void
enc_check_duplication(const char *name)
{
if (rb_enc_registered(name) >= 0) {
rb_raise(rb_eArgError, "encoding %s is already registered", name);
}
}
static rb_encoding*
set_base_encoding(int index, rb_encoding *base)
{
rb_encoding *enc = enc_table.list[index].enc;
enc_table.list[index].base = base;
if (rb_enc_dummy_p(base)) ENC_SET_DUMMY(enc);
return enc;
}
/* for encdb.h
* Set base encoding for encodings which are not replicas
* but not in their own files.
*/
void
rb_enc_set_base(const char *name, const char *orig)
{
int idx = rb_enc_registered(name);
int origidx = rb_enc_registered(orig);
set_base_encoding(idx, rb_enc_from_index(origidx));
}
int
rb_enc_replicate(const char *name, rb_encoding *encoding)
{
int idx;
enc_check_duplication(name);
idx = enc_register(name, encoding);
set_base_encoding(idx, encoding);
set_encoding_const(name, rb_enc_from_index(idx));
return idx;
}
/*
* call-seq:
* enc.replicate(name) -> encoding
*
* Returns a replicated encoding of _enc_ whose name is _name_.
* The new encoding should have the same byte structure of _enc_.
* If _name_ is used by another encoding, raise ArgumentError.
*
*/
static VALUE
enc_replicate(VALUE encoding, VALUE name)
{
return rb_enc_from_encoding_index(
rb_enc_replicate(StringValueCStr(name),
rb_to_encoding(encoding)));
}
static int
enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx)
{
if (idx < 0) {
idx = enc_register(name, origenc);
}
else {
idx = enc_register_at(idx, name, origenc);
}
if (idx >= 0) {
set_base_encoding(idx, origenc);
set_encoding_const(name, rb_enc_from_index(idx));
}
return idx;
}
int
rb_encdb_replicate(const char *name, const char *orig)
{
int origidx = rb_enc_registered(orig);
int idx = rb_enc_registered(name);
if (origidx < 0) {
origidx = enc_register(orig, 0);
}
return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx);
}
int
rb_define_dummy_encoding(const char *name)
{
int index = rb_enc_replicate(name, rb_ascii8bit_encoding());
rb_encoding *enc = enc_table.list[index].enc;
ENC_SET_DUMMY(enc);
return index;
}
int
rb_encdb_dummy(const char *name)
{
int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(),
rb_enc_registered(name));
rb_encoding *enc = enc_table.list[index].enc;
ENC_SET_DUMMY(enc);
return index;
}
/*
* call-seq:
* enc.dummy? -> true or false
*
* Returns true for dummy encodings.
* A dummy encoding is an encoding for which character handling is not properly
* implemented.
* It is used for stateful encodings.
*
* Encoding::ISO_2022_JP.dummy? #=> true
* Encoding::UTF_8.dummy? #=> false
*
*/
static VALUE
enc_dummy_p(VALUE enc)
{
return ENC_DUMMY_P(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
}
/*
* call-seq:
* enc.ascii_compatible? -> true or false
*
* Returns whether ASCII-compatible or not.
*
* Encoding::UTF_8.ascii_compatible? #=> true
* Encoding::UTF_16BE.ascii_compatible? #=> false
*
*/
static VALUE
enc_ascii_compatible_p(VALUE enc)
{
return rb_enc_asciicompat(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
}
/*
* Returns 1 when the encoding is Unicode series other than UTF-7 else 0.
*/
int
rb_enc_unicode_p(rb_encoding *enc)
{
const char *name = rb_enc_name(enc);
return name[0] == 'U' && name[1] == 'T' && name[2] == 'F' && name[4] != '7';
}
/*
* Returns copied alias name when the key is added for st_table,
* else returns NULL.
*/
static int
enc_alias_internal(const char *alias, int idx)
{
return st_insert2(enc_table.names, (st_data_t)alias, (st_data_t)idx,
(st_data_t(*)(st_data_t))strdup);
}
static int
enc_alias(const char *alias, int idx)
{
if (!valid_encoding_name_p(alias)) return -1;
if (!enc_alias_internal(alias, idx))
set_encoding_const(alias, rb_enc_from_index(idx));
return idx;
}
int
rb_enc_alias(const char *alias, const char *orig)
{
int idx;
enc_check_duplication(alias);
if (!enc_table.list) {
rb_enc_init();
}
if ((idx = rb_enc_find_index(orig)) < 0) {
return -1;
}
return enc_alias(alias, idx);
}
int
rb_encdb_alias(const char *alias, const char *orig)
{
int idx = rb_enc_registered(orig);
if (idx < 0) {
idx = enc_register(orig, 0);
}
return enc_alias(alias, idx);
}
enum {
ENCINDEX_ASCII,
ENCINDEX_UTF_8,
ENCINDEX_US_ASCII,
ENCINDEX_BUILTIN_MAX
};
extern rb_encoding OnigEncodingUTF_8;
extern rb_encoding OnigEncodingUS_ASCII;
void
rb_enc_init(void)
{
enc_table_expand(ENCODING_COUNT + 1);
if (!enc_table.names) {
enc_table.names = st_init_strcasetable();
}
#define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
ENC_REGISTER(ASCII);
ENC_REGISTER(UTF_8);
ENC_REGISTER(US_ASCII);
#undef ENC_REGISTER
enc_table.count = ENCINDEX_BUILTIN_MAX;
}
rb_encoding *
rb_enc_from_index(int index)
{
if (!enc_table.list) {
rb_enc_init();
}
if (index < 0 || enc_table.count <= index) {
return 0;
}
return enc_table.list[index].enc;
}
int
rb_enc_registered(const char *name)
{
st_data_t idx = 0;
if (!name) return -1;
if (!enc_table.list) return -1;
if (st_lookup(enc_table.names, (st_data_t)name, &idx)) {
return (int)idx;
}
return -1;
}
static VALUE
require_enc(VALUE enclib)
{
int safe = rb_safe_level();
return rb_require_safe(enclib, safe > 3 ? 3 : safe);
}
static int
load_encoding(const char *name)
{
VALUE enclib = rb_sprintf("enc/%s.so", name);
VALUE verbose = ruby_verbose;
VALUE debug = ruby_debug;
VALUE loaded;
char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
int idx;
while (s < e) {
if (!ISALNUM(*s)) *s = '_';
else if (ISUPPER(*s)) *s = TOLOWER(*s);
++s;
}
FL_UNSET(enclib, FL_TAINT|FL_UNTRUSTED);
OBJ_FREEZE(enclib);
ruby_verbose = Qfalse;
ruby_debug = Qfalse;
loaded = rb_protect(require_enc, enclib, 0);
ruby_verbose = verbose;
ruby_debug = debug;
rb_set_errinfo(Qnil);
if (NIL_P(loaded)) return -1;
if ((idx = rb_enc_registered(name)) < 0) return -1;
if (enc_autoload_p(enc_table.list[idx].enc)) return -1;
return idx;
}
static int
enc_autoload(rb_encoding *enc)
{
int i;
rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base;
if (base) {
i = 0;
do {
if (i >= enc_table.count) return -1;
} while (enc_table.list[i].enc != base && (++i, 1));
if (enc_autoload_p(base)) {
if (enc_autoload(base) < 0) return -1;
}
i = ENC_TO_ENCINDEX(enc);
enc_register_at(i, rb_enc_name(enc), base);
}
else {
i = load_encoding(rb_enc_name(enc));
}
return i;
}
/* Return encoding index or UNSPECIFIED_ENCODING from encoding name */
int
rb_enc_find_index(const char *name)
{
int i = rb_enc_registered(name);
rb_encoding *enc;
if (i < 0) {
i = load_encoding(name);
}
else if (!(enc = rb_enc_from_index(i))) {
if (i != UNSPECIFIED_ENCODING) {
rb_raise(rb_eArgError, "encoding %s is not registered", name);
}
}
else if (enc_autoload_p(enc)) {
if (enc_autoload(enc) < 0) {
rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
name);
return 0;
}
}
return i;
}
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 inline int
enc_capable(VALUE obj)
{
if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
switch (BUILTIN_TYPE(obj)) {
case T_STRING:
case T_REGEXP:
case T_FILE:
return TRUE;
case T_DATA:
if (is_data_encoding(obj)) return TRUE;
default:
return FALSE;
}
}
ID
rb_id_encoding(void)
{
CONST_ID(id_encoding, "encoding");
return id_encoding;
}
int
rb_enc_get_index(VALUE obj)
{
int i = -1;
VALUE tmp;
if (SPECIAL_CONST_P(obj)) {
if (!SYMBOL_P(obj)) return -1;
obj = rb_id2str(SYM2ID(obj));
}
switch (BUILTIN_TYPE(obj)) {
as_default:
default:
case T_STRING:
case T_REGEXP:
i = ENCODING_GET_INLINED(obj);
if (i == ENCODING_INLINE_MAX) {
VALUE iv;
iv = rb_ivar_get(obj, rb_id_encoding());
i = NUM2INT(iv);
}
break;
case T_FILE:
tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0);
if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0);
else obj = tmp;
if (NIL_P(obj)) break;
case T_DATA:
if (is_data_encoding(obj)) {
i = enc_check_encoding(obj);
}
else {
goto as_default;
}
break;
}
return i;
}
static void
enc_set_index(VALUE obj, int idx)
{
if (idx < ENCODING_INLINE_MAX) {
ENCODING_SET_INLINED(obj, idx);
return;
}
ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
}
void
rb_enc_set_index(VALUE obj, int idx)
{
rb_check_frozen(obj);
enc_set_index(obj, idx);
}
VALUE
rb_enc_associate_index(VALUE obj, int idx)
{
/* enc_check_capable(obj);*/
rb_check_frozen(obj);
if (rb_enc_get_index(obj) == idx)
return obj;
if (SPECIAL_CONST_P(obj)) {
rb_raise(rb_eArgError, "cannot set encoding");
}
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
!rb_enc_asciicompat(rb_enc_from_index(idx))) {
ENC_CODERANGE_CLEAR(obj);
}
enc_set_index(obj, idx);
return obj;
}
VALUE
rb_enc_associate(VALUE obj, rb_encoding *enc)
{
return rb_enc_associate_index(obj, rb_enc_to_index(enc));
}
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_eEncCompatError, "incompatible character encodings: %s and %s",
rb_enc_name(rb_enc_get(str1)),
rb_enc_name(rb_enc_get(str2)));
return enc;
}
rb_encoding*
rb_enc_compatible(VALUE str1, VALUE str2)
{
int idx1, idx2;
rb_encoding *enc1, *enc2;
int isstr1, isstr2;
idx1 = rb_enc_get_index(str1);
idx2 = rb_enc_get_index(str2);
if (idx1 < 0 || idx2 < 0)
return 0;
if (idx1 == idx2) {
return rb_enc_from_index(idx1);
}
enc1 = rb_enc_from_index(idx1);
enc2 = rb_enc_from_index(idx2);
isstr2 = RB_TYPE_P(str2, T_STRING);
if (isstr2 && RSTRING_LEN(str2) == 0)
return enc1;
isstr1 = RB_TYPE_P(str1, T_STRING);
if (isstr1 && RSTRING_LEN(str1) == 0)
return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
return 0;
}
/* objects whose encoding is the same of contents */
if (!isstr2 && idx2 == ENCINDEX_US_ASCII)
return enc1;
if (!isstr1 && idx1 == ENCINDEX_US_ASCII)
return enc2;
if (!isstr1) {
VALUE tmp = str1;
int idx0 = idx1;
str1 = str2;
str2 = tmp;
idx1 = idx2;
idx2 = idx0;
idx0 = isstr1;
isstr1 = isstr2;
isstr2 = idx0;
}
if (isstr1) {
int cr1, cr2;
cr1 = rb_enc_str_coderange(str1);
if (isstr2) {
cr2 = rb_enc_str_coderange(str2);
if (cr1 != cr2) {
/* may need to handle ENC_CODERANGE_BROKEN */
if (cr1 == ENC_CODERANGE_7BIT) return enc2;
if (cr2 == ENC_CODERANGE_7BIT) return enc1;
}
if (cr2 == ENC_CODERANGE_7BIT) {
return enc1;
}
}
if (cr1 == ENC_CODERANGE_7BIT)
return enc2;
}
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)
{
int idx = rb_enc_get_index(obj);
if (idx < 0) {
rb_raise(rb_eTypeError, "unknown encoding");
}
return rb_enc_from_encoding_index(idx);
}
int
rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
{
return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
}
int
rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
{
int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
return MBCLEN_CHARFOUND_LEN(n);
else {
int min = rb_enc_mbminlen(enc);
return min <= e-p ? min : (int)(e-p);
}
}
int
rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
{
int n;
if (e <= p)
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
if (e-p < n)
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
return n;
}
int
rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
{
unsigned int c, l;
if (e <= p)
return -1;
if (rb_enc_asciicompat(enc)) {
c = (unsigned char)*p;
if (!ISASCII(c))
return -1;
if (len) *len = 1;
return c;
}
l = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(l))
return -1;
c = rb_enc_mbc_to_codepoint(p, e, enc);
if (!rb_enc_isascii(c, enc))
return -1;
if (len) *len = l;
return c;
}
unsigned int
rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
{
int r;
if (e <= p)
rb_raise(rb_eArgError, "empty string");
r = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(r)) {
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
}
if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
return rb_enc_mbc_to_codepoint(p, e, enc);
}
#undef rb_enc_codepoint
unsigned int
rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
{
return rb_enc_codepoint_len(p, e, 0, enc);
}
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 codepoint 0x%x in %s", c, rb_enc_name(enc));
}
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));
}
/*
* call-seq:
* enc.inspect -> string
*
* Returns a string which represents the encoding for programmers.
*
* Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>"
* Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
*/
static VALUE
enc_inspect(VALUE self)
{
VALUE str = rb_sprintf("#<%s:%s%s>", rb_obj_classname(self),
rb_enc_name((rb_encoding*)DATA_PTR(self)),
(enc_dummy_p(self) ? " (dummy)" : ""));
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
return str;
}
/*
* call-seq:
* enc.name -> string
*
* Returns the name of the encoding.
*
* Encoding::UTF_8.name #=> "UTF-8"
*/
static VALUE
enc_name(VALUE self)
{
return rb_usascii_str_new2(rb_enc_name((rb_encoding*)DATA_PTR(self)));
}
static int
enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
{
VALUE *arg = (VALUE *)args;
if ((int)idx == (int)arg[0]) {
VALUE str = rb_usascii_str_new2((char *)name);