Ruby 3.5.0dev (2025-07-15 revision b2a7b7699261d2a4ef8a9d5d38d3fb9dc99c8253)
encoding.c (b2a7b7699261d2a4ef8a9d5d38d3fb9dc99c8253)
1/**********************************************************************
2
3 encoding.c -
4
5 $Author$
6 created at: Thu May 24 17:23:27 JST 2007
7
8 Copyright (C) 2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <ctype.h>
15
16#include "encindex.h"
17#include "internal.h"
18#include "internal/enc.h"
19#include "internal/encoding.h"
20#include "internal/error.h"
21#include "internal/inits.h"
22#include "internal/load.h"
23#include "internal/object.h"
24#include "internal/string.h"
25#include "internal/vm.h"
26#include "regenc.h"
27#include "ruby/atomic.h"
28#include "ruby/encoding.h"
29#include "ruby/util.h"
30#include "ruby_assert.h"
31#include "vm_sync.h"
32
33#ifndef ENC_DEBUG
34#define ENC_DEBUG 0
35#endif
36#define ENC_ASSERT(expr) RUBY_ASSERT_WHEN(ENC_DEBUG, expr)
37#define MUST_STRING(str) (ENC_ASSERT(RB_TYPE_P(str, T_STRING)), str)
38
39#undef rb_ascii8bit_encindex
40#undef rb_utf8_encindex
41#undef rb_usascii_encindex
42
44
45#if defined __GNUC__ && __GNUC__ >= 4
46#pragma GCC visibility push(default)
47int rb_enc_register(const char *name, rb_encoding *encoding);
48void rb_enc_set_base(const char *name, const char *orig);
49int rb_enc_set_dummy(int index);
50void rb_encdb_declare(const char *name);
51int rb_encdb_replicate(const char *name, const char *orig);
52int rb_encdb_dummy(const char *name);
53int rb_encdb_alias(const char *alias, const char *orig);
54#pragma GCC visibility pop
55#endif
56
57static ID id_encoding;
59
60#define ENCODING_LIST_CAPA 256
61static VALUE rb_encoding_list;
62
64 rb_atomic_t loaded;
65 const char *name;
66 rb_encoding *enc;
67 rb_encoding *base;
68};
69
70static struct enc_table {
71 struct rb_encoding_entry list[ENCODING_LIST_CAPA];
72 int count;
73 st_table *names;
74} global_enc_table;
75
76static int
77enc_names_free_i(st_data_t name, st_data_t idx, st_data_t args)
78{
79 ruby_xfree((void *)name);
80 return ST_DELETE;
81}
82
83void
84rb_free_global_enc_table(void)
85{
86 for (size_t i = 0; i < ENCODING_LIST_CAPA; i++) {
87 xfree((void *)global_enc_table.list[i].enc);
88 }
89
90 st_foreach(global_enc_table.names, enc_names_free_i, (st_data_t)0);
91 st_free_table(global_enc_table.names);
92}
93
94static rb_encoding *global_enc_ascii,
95 *global_enc_utf_8,
96 *global_enc_us_ascii;
97
98#define GLOBAL_ENC_TABLE_LOCKING(tbl) \
99 for (struct enc_table *tbl = &global_enc_table, **locking = &tbl; \
100 locking; \
101 locking = NULL) \
102 RB_VM_LOCKING()
103
104
105#define ENC_DUMMY_FLAG (1<<24)
106#define ENC_INDEX_MASK (~(~0U<<24))
107
108#define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
109#define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
110#define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
111
112#define ENCODING_COUNT ENCINDEX_BUILTIN_MAX
113#define UNSPECIFIED_ENCODING INT_MAX
114
115#define ENCODING_NAMELEN_MAX 63
116#define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX)
117
118static const rb_data_type_t encoding_data_type = {
119 "encoding",
120 {0, 0, 0,},
121 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
122};
123
124#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
125#define is_obj_encoding(obj) (RB_TYPE_P((obj), T_DATA) && is_data_encoding(obj))
126
127int
128rb_data_is_encoding(VALUE obj)
129{
130 return is_data_encoding(obj);
131}
132
133static VALUE
134enc_new(rb_encoding *encoding)
135{
136 VALUE enc = TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, (void *)encoding);
137 rb_obj_freeze(enc);
139 return enc;
140}
141
142static void
143enc_list_update(int index, rb_raw_encoding *encoding)
144{
145 RUBY_ASSERT(index < ENCODING_LIST_CAPA);
146
147 VALUE list = rb_encoding_list;
148 if (list && NIL_P(rb_ary_entry(list, index))) {
149 /* initialize encoding data */
150 rb_ary_store(list, index, enc_new(encoding));
151 }
152}
153
154static VALUE
155enc_list_lookup(int idx)
156{
157 VALUE list, enc = Qnil;
158
159 if (idx < ENCODING_LIST_CAPA) {
160 list = rb_encoding_list;
161 RUBY_ASSERT(list);
162 enc = rb_ary_entry(list, idx);
163 }
164
165 if (NIL_P(enc)) {
166 rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
167 }
168 else {
169 return enc;
170 }
171}
172
173static VALUE
174rb_enc_from_encoding_index(int idx)
175{
176 return enc_list_lookup(idx);
177}
178
179VALUE
180rb_enc_from_encoding(rb_encoding *encoding)
181{
182 int idx;
183 if (!encoding) return Qnil;
184 idx = ENC_TO_ENCINDEX(encoding);
185 return rb_enc_from_encoding_index(idx);
186}
187
188int
189rb_enc_to_index(rb_encoding *enc)
190{
191 return enc ? ENC_TO_ENCINDEX(enc) : 0;
192}
193
194int
195rb_enc_dummy_p(rb_encoding *enc)
196{
197 return ENC_DUMMY_P(enc) != 0;
198}
199
200static int
201check_encoding(rb_encoding *enc)
202{
203 int index = rb_enc_to_index(enc);
204 if (rb_enc_from_index(index) != enc)
205 return -1;
206 if (rb_enc_autoload_p(enc)) {
207 index = rb_enc_autoload(enc);
208 }
209 return index;
210}
211
212static int
213enc_check_encoding(VALUE obj)
214{
215 if (!is_obj_encoding(obj)) {
216 return -1;
217 }
218 return check_encoding(RDATA(obj)->data);
219}
220
221NORETURN(static void not_encoding(VALUE enc));
222static void
223not_encoding(VALUE enc)
224{
225 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Encoding)",
226 rb_obj_class(enc));
227}
228
229static rb_encoding *
230must_encoding(VALUE enc)
231{
232 int index = enc_check_encoding(enc);
233 if (index < 0) {
234 not_encoding(enc);
235 }
236 return DATA_PTR(enc);
237}
238
239static rb_encoding *
240must_encindex(int index)
241{
242 rb_encoding *enc = rb_enc_from_index(index);
243 if (!enc) {
244 rb_raise(rb_eEncodingError, "encoding index out of bound: %d",
245 index);
246 }
247 if (ENC_TO_ENCINDEX(enc) != (int)(index & ENC_INDEX_MASK)) {
248 rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)",
249 index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc));
250 }
251 if (rb_enc_autoload_p(enc) && rb_enc_autoload(enc) == -1) {
252 rb_loaderror("failed to load encoding (%s)",
253 rb_enc_name(enc));
254 }
255 return enc;
256}
257
258int
259rb_to_encoding_index(VALUE enc)
260{
261 int idx;
262 const char *name;
263
264 idx = enc_check_encoding(enc);
265 if (idx >= 0) {
266 return idx;
267 }
268 else if (NIL_P(enc = rb_check_string_type(enc))) {
269 return -1;
270 }
271 if (!rb_enc_asciicompat(rb_enc_get(enc))) {
272 return -1;
273 }
274 if (!(name = rb_str_to_cstr(enc))) {
275 return -1;
276 }
277 return rb_enc_find_index(name);
278}
279
280static const char *
281name_for_encoding(volatile VALUE *enc)
282{
283 VALUE name = StringValue(*enc);
284 const char *n;
285
286 if (!rb_enc_asciicompat(rb_enc_get(name))) {
287 rb_raise(rb_eArgError, "invalid encoding name (non ASCII)");
288 }
289 if (!(n = rb_str_to_cstr(name))) {
290 rb_raise(rb_eArgError, "invalid encoding name (NUL byte)");
291 }
292 return n;
293}
294
295/* Returns encoding index or UNSPECIFIED_ENCODING */
296static int
297str_find_encindex(VALUE enc)
298{
299 int idx = rb_enc_find_index(name_for_encoding(&enc));
300 RB_GC_GUARD(enc);
301 return idx;
302}
303
304static int
305str_to_encindex(VALUE enc)
306{
307 int idx = str_find_encindex(enc);
308 if (idx < 0) {
309 rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc);
310 }
311 return idx;
312}
313
314static rb_encoding *
315str_to_encoding(VALUE enc)
316{
317 return rb_enc_from_index(str_to_encindex(enc));
318}
319
321rb_to_encoding(VALUE enc)
322{
323 if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
324 return str_to_encoding(enc);
325}
326
328rb_find_encoding(VALUE enc)
329{
330 int idx;
331 if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
332 idx = str_find_encindex(enc);
333 if (idx < 0) return NULL;
334 return rb_enc_from_index(idx);
335}
336
337static int
338enc_table_expand(struct enc_table *enc_table, int newsize)
339{
340 if (newsize > ENCODING_LIST_CAPA) {
341 rb_raise(rb_eEncodingError, "too many encoding (> %d)", ENCODING_LIST_CAPA);
342 }
343 return newsize;
344}
345
346static int
347enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding)
348{
349 ASSERT_vm_locking();
350
351 struct rb_encoding_entry *ent = &enc_table->list[index];
352 rb_raw_encoding *encoding;
353
354 if (!valid_encoding_name_p(name)) return -1;
355 if (!ent->name) {
356 ent->name = name = strdup(name);
357 }
358 else if (STRCASECMP(name, ent->name)) {
359 return -1;
360 }
361 encoding = (rb_raw_encoding *)ent->enc;
362 if (!encoding) {
363 encoding = xmalloc(sizeof(rb_encoding));
364 }
365
366 if (base_encoding) {
367 *encoding = *base_encoding;
368 }
369 else {
370 memset(encoding, 0, sizeof(*ent->enc));
371 }
372 encoding->name = name;
373 encoding->ruby_encoding_index = index;
374 ent->enc = encoding;
375 st_insert(enc_table->names, (st_data_t)name, (st_data_t)index);
376
377 enc_list_update(index, encoding);
378
379 // max_enc_len is used to mark a fully loaded encoding.
380 RUBY_ATOMIC_SET(ent->loaded, encoding->max_enc_len);
381
382 return index;
383}
384
385static int
386enc_register(struct enc_table *enc_table, const char *name, rb_encoding *encoding)
387{
388 ASSERT_vm_locking();
389
390 int index = enc_table->count;
391
392 enc_table->count = enc_table_expand(enc_table, index + 1);
393 return enc_register_at(enc_table, index, name, encoding);
394}
395
396static void set_encoding_const(const char *, rb_encoding *);
397static int enc_registered(struct enc_table *enc_table, const char *name);
398
399static rb_encoding *
400enc_from_index(struct enc_table *enc_table, int index)
401{
402 if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) {
403 return 0;
404 }
405 return enc_table->list[index].enc;
406}
407
409rb_enc_from_index(int index)
410{
411 return enc_from_index(&global_enc_table, index);
412}
413
414int
415rb_enc_register(const char *name, rb_encoding *encoding)
416{
417 int index;
418
419 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
420 index = enc_registered(enc_table, name);
421
422 if (index >= 0) {
423 rb_encoding *oldenc = enc_from_index(enc_table, index);
424 if (STRCASECMP(name, rb_enc_name(oldenc))) {
425 index = enc_register(enc_table, name, encoding);
426 }
427 else if (rb_enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
428 enc_register_at(enc_table, index, name, encoding);
429 }
430 else {
431 rb_raise(rb_eArgError, "encoding %s is already registered", name);
432 }
433 }
434 else {
435 index = enc_register(enc_table, name, encoding);
436 set_encoding_const(name, rb_enc_from_index(index));
437 }
438 }
439 return index;
440}
441
442int
443enc_registered(struct enc_table *enc_table, const char *name)
444{
445 ASSERT_vm_locking();
446 st_data_t idx = 0;
447
448 if (!name) return -1;
449 if (!enc_table->names) return -1;
450 if (st_lookup(enc_table->names, (st_data_t)name, &idx)) {
451 return (int)idx;
452 }
453 return -1;
454}
455
456void
457rb_encdb_declare(const char *name)
458{
459 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
460 int idx = enc_registered(enc_table, name);
461 if (idx < 0) {
462 idx = enc_register(enc_table, name, 0);
463 }
464 set_encoding_const(name, rb_enc_from_index(idx));
465 }
466}
467
468static void
469enc_check_addable(struct enc_table *enc_table, const char *name)
470{
471 if (enc_registered(enc_table, name) >= 0) {
472 rb_raise(rb_eArgError, "encoding %s is already registered", name);
473 }
474 else if (!valid_encoding_name_p(name)) {
475 rb_raise(rb_eArgError, "invalid encoding name: %s", name);
476 }
477}
478
479static rb_encoding*
480set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
481{
482 rb_encoding *enc = enc_table->list[index].enc;
483
484 ASSUME(enc);
485 enc_table->list[index].base = base;
486 if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc);
487 return enc;
488}
489
490/* for encdb.h
491 * Set base encoding for encodings which are not replicas
492 * but not in their own files.
493 */
494void
495rb_enc_set_base(const char *name, const char *orig)
496{
497 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
498 int idx = enc_registered(enc_table, name);
499 int origidx = enc_registered(enc_table, orig);
500 set_base_encoding(enc_table, idx, rb_enc_from_index(origidx));
501 }
502}
503
504/* for encdb.h
505 * Set encoding dummy.
506 */
507int
508rb_enc_set_dummy(int index)
509{
510 rb_encoding *enc = global_enc_table.list[index].enc;
511 ENC_SET_DUMMY((rb_raw_encoding *)enc);
512 return index;
513}
514
515static int
516enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encoding)
517{
518 int idx;
519
520 enc_check_addable(enc_table, name);
521 idx = enc_register(enc_table, name, encoding);
522 if (idx < 0) rb_raise(rb_eArgError, "invalid encoding name: %s", name);
523 set_base_encoding(enc_table, idx, encoding);
524 set_encoding_const(name, rb_enc_from_index(idx));
525 return idx;
526}
527
528static int
529enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encoding *origenc, int idx)
530{
531 if (idx < 0) {
532 idx = enc_register(enc_table, name, origenc);
533 }
534 else {
535 idx = enc_register_at(enc_table, idx, name, origenc);
536 }
537 if (idx >= 0) {
538 set_base_encoding(enc_table, idx, origenc);
539 set_encoding_const(name, rb_enc_from_index(idx));
540 }
541 else {
542 rb_raise(rb_eArgError, "failed to replicate encoding");
543 }
544 return idx;
545}
546
547int
548rb_encdb_replicate(const char *name, const char *orig)
549{
550 int r;
551
552 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
553 int origidx = enc_registered(enc_table, orig);
554 int idx = enc_registered(enc_table, name);
555
556 if (origidx < 0) {
557 origidx = enc_register(enc_table, orig, 0);
558 }
559 r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx);
560 }
561
562 return r;
563}
564
565int
566rb_define_dummy_encoding(const char *name)
567{
568 int index;
569
570 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
571 index = enc_replicate(enc_table, name, rb_ascii8bit_encoding());
572 rb_encoding *enc = enc_table->list[index].enc;
573 ENC_SET_DUMMY((rb_raw_encoding *)enc);
574 }
575
576 return index;
577}
578
579int
580rb_encdb_dummy(const char *name)
581{
582 int index;
583
584 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
585 index = enc_replicate_with_index(enc_table, name,
586 rb_ascii8bit_encoding(),
587 enc_registered(enc_table, name));
588 rb_encoding *enc = enc_table->list[index].enc;
589 ENC_SET_DUMMY((rb_raw_encoding *)enc);
590 }
591
592 return index;
593}
594
595/*
596 * call-seq:
597 * enc.dummy? -> true or false
598 *
599 * Returns true for dummy encodings.
600 * A dummy encoding is an encoding for which character handling is not properly
601 * implemented.
602 * It is used for stateful encodings.
603 *
604 * Encoding::ISO_2022_JP.dummy? #=> true
605 * Encoding::UTF_8.dummy? #=> false
606 *
607 */
608static VALUE
609enc_dummy_p(VALUE enc)
610{
611 return RBOOL(ENC_DUMMY_P(must_encoding(enc)));
612}
613
614/*
615 * call-seq:
616 * enc.ascii_compatible? -> true or false
617 *
618 * Returns whether ASCII-compatible or not.
619 *
620 * Encoding::UTF_8.ascii_compatible? #=> true
621 * Encoding::UTF_16BE.ascii_compatible? #=> false
622 *
623 */
624static VALUE
625enc_ascii_compatible_p(VALUE enc)
626{
627 return RBOOL(rb_enc_asciicompat(must_encoding(enc)));
628}
629
630/*
631 * Returns non-zero when the encoding is Unicode series other than UTF-7 else 0.
632 */
633int
634rb_enc_unicode_p(rb_encoding *enc)
635{
636 return ONIGENC_IS_UNICODE(enc);
637}
638
639static st_data_t
640enc_dup_name(st_data_t name)
641{
642 return (st_data_t)strdup((const char *)name);
643}
644
645/*
646 * Returns copied alias name when the key is added for st_table,
647 * else returns NULL.
648 */
649static int
650enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
651{
652 ASSERT_vm_locking();
653 return st_insert2(enc_table->names, (st_data_t)alias, (st_data_t)idx,
654 enc_dup_name);
655}
656
657static int
658enc_alias(struct enc_table *enc_table, const char *alias, int idx)
659{
660 if (!valid_encoding_name_p(alias)) return -1;
661 if (!enc_alias_internal(enc_table, alias, idx))
662 set_encoding_const(alias, enc_from_index(enc_table, idx));
663 return idx;
664}
665
666int
667rb_enc_alias(const char *alias, const char *orig)
668{
669 int idx, r;
670
671 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
672 enc_check_addable(enc_table, alias);
673 if ((idx = rb_enc_find_index(orig)) < 0) {
674 r = -1;
675 }
676 else {
677 r = enc_alias(enc_table, alias, idx);
678 }
679 }
680
681 return r;
682}
683
684int
685rb_encdb_alias(const char *alias, const char *orig)
686{
687 int r;
688
689 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
690 int idx = enc_registered(enc_table, orig);
691
692 if (idx < 0) {
693 idx = enc_register(enc_table, orig, 0);
694 }
695 r = enc_alias(enc_table, alias, idx);
696 }
697
698 return r;
699}
700
701static void
702rb_enc_init(struct enc_table *enc_table)
703{
704 ASSERT_vm_locking();
705 enc_table_expand(enc_table, ENCODING_COUNT + 1);
706 if (!enc_table->names) {
707 enc_table->names = st_init_strcasetable_with_size(ENCODING_LIST_CAPA);
708 }
709#define OnigEncodingASCII_8BIT OnigEncodingASCII
710#define ENC_REGISTER(enc) enc_register_at(enc_table, ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
711 ENC_REGISTER(ASCII_8BIT);
712 ENC_REGISTER(UTF_8);
713 ENC_REGISTER(US_ASCII);
714 global_enc_ascii = enc_table->list[ENCINDEX_ASCII_8BIT].enc;
715 global_enc_utf_8 = enc_table->list[ENCINDEX_UTF_8].enc;
716 global_enc_us_ascii = enc_table->list[ENCINDEX_US_ASCII].enc;
717#undef ENC_REGISTER
718#undef OnigEncodingASCII_8BIT
719#define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL)
720 ENCDB_REGISTER("UTF-16BE", UTF_16BE);
721 ENCDB_REGISTER("UTF-16LE", UTF_16LE);
722 ENCDB_REGISTER("UTF-32BE", UTF_32BE);
723 ENCDB_REGISTER("UTF-32LE", UTF_32LE);
724 ENCDB_REGISTER("UTF-16", UTF_16);
725 ENCDB_REGISTER("UTF-32", UTF_32);
726 ENCDB_REGISTER("UTF8-MAC", UTF8_MAC);
727
728 ENCDB_REGISTER("EUC-JP", EUC_JP);
729 ENCDB_REGISTER("Windows-31J", Windows_31J);
730#undef ENCDB_REGISTER
731 enc_table->count = ENCINDEX_BUILTIN_MAX;
732}
733
735rb_enc_get_from_index(int index)
736{
737 return must_encindex(index);
738}
739
740int rb_require_internal_silent(VALUE fname);
741
742static int
743load_encoding(const char *name)
744{
745 VALUE enclib = rb_sprintf("enc/%s.so", name);
746 VALUE debug = ruby_debug;
747 VALUE errinfo;
748 char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
749 int loaded;
750 int idx;
751
752 while (s < e) {
753 if (!ISALNUM(*s)) *s = '_';
754 else if (ISUPPER(*s)) *s = (char)TOLOWER(*s);
755 ++s;
756 }
757 enclib = rb_fstring(enclib);
759 errinfo = rb_errinfo();
760 loaded = rb_require_internal_silent(enclib);
761 ruby_debug = debug;
762 rb_set_errinfo(errinfo);
763
764 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
765 if (loaded < 0 || 1 < loaded) {
766 idx = -1;
767 }
768 else if ((idx = enc_registered(enc_table, name)) < 0) {
769 idx = -1;
770 }
771 else if (rb_enc_autoload_p(enc_table->list[idx].enc)) {
772 idx = -1;
773 }
774 }
775
776 return idx;
777}
778
779static int
780enc_autoload_body(rb_encoding *enc)
781{
782 rb_encoding *base;
783 int i = 0;
784
785 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
786 base = enc_table->list[ENC_TO_ENCINDEX(enc)].base;
787 if (base) {
788 do {
789 if (i >= enc_table->count) {
790 i = -1;
791 break;
792 }
793 } while (enc_table->list[i].enc != base && (++i, 1));
794 }
795 }
796
797 if (i == -1) return -1;
798
799 if (base) {
800 if (rb_enc_autoload_p(base)) {
801 if (rb_enc_autoload(base) < 0) return -1;
802 }
803 i = enc->ruby_encoding_index;
804
805 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
806 enc_register_at(enc_table, i & ENC_INDEX_MASK, rb_enc_name(enc), base);
807 }
808
809 ((rb_raw_encoding *)enc)->ruby_encoding_index = i;
810 i &= ENC_INDEX_MASK;
811 return i;
812 }
813
814 return -2;
815}
816
817int
818rb_enc_autoload(rb_encoding *enc)
819{
820 int i = enc_autoload_body(enc);
821 if (i == -2) {
822 i = load_encoding(rb_enc_name(enc));
823 }
824 return i;
825}
826
827bool
828rb_enc_autoload_p(rb_encoding *enc)
829{
830 int idx = ENC_TO_ENCINDEX(enc);
831 RUBY_ASSERT(rb_enc_from_index(idx) == enc);
832 return !RUBY_ATOMIC_LOAD(global_enc_table.list[idx].loaded);
833}
834
835/* Return encoding index or UNSPECIFIED_ENCODING from encoding name */
836int
837rb_enc_find_index(const char *name)
838{
839 int i;
840 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
841 i = enc_registered(enc_table, name);
842 }
843 rb_encoding *enc;
844
845 if (i < 0) {
846 i = load_encoding(name);
847 }
848 else if (!(enc = rb_enc_from_index(i))) {
849 if (i != UNSPECIFIED_ENCODING) {
850 rb_raise(rb_eArgError, "encoding %s is not registered", name);
851 }
852 }
853 else if (rb_enc_autoload_p(enc)) {
854 if (rb_enc_autoload(enc) < 0) {
855 rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
856 name);
857 return 0;
858 }
859 }
860 return i;
861}
862
863int
864rb_enc_find_index2(const char *name, long len)
865{
866 char buf[ENCODING_NAMELEN_MAX+1];
867
868 if (len > ENCODING_NAMELEN_MAX) return -1;
869 memcpy(buf, name, len);
870 buf[len] = '\0';
871 return rb_enc_find_index(buf);
872}
873
875rb_enc_find(const char *name)
876{
877 int idx = rb_enc_find_index(name);
878 if (idx < 0) idx = 0;
879 return rb_enc_from_index(idx);
880}
881
882static inline int
883enc_capable(VALUE obj)
884{
885 if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
886 switch (BUILTIN_TYPE(obj)) {
887 case T_STRING:
888 case T_REGEXP:
889 case T_FILE:
890 case T_SYMBOL:
891 return TRUE;
892 case T_DATA:
893 if (is_data_encoding(obj)) return TRUE;
894 default:
895 return FALSE;
896 }
897}
898
899int
900rb_enc_capable(VALUE obj)
901{
902 return enc_capable(obj);
903}
904
905ID
906rb_id_encoding(void)
907{
908 CONST_ID(id_encoding, "encoding");
909 return id_encoding;
910}
911
912static int
913enc_get_index_str(VALUE str)
914{
915 int i = ENCODING_GET_INLINED(str);
916 if (i == ENCODING_INLINE_MAX) {
917 VALUE iv;
918
919#if 0
920 iv = rb_ivar_get(str, rb_id_encoding());
921 i = NUM2INT(iv);
922#else
923 /*
924 * Tentatively, assume ASCII-8BIT, if encoding index instance
925 * variable is not found. This can happen when freeing after
926 * all instance variables are removed in `obj_free`.
927 */
928 iv = rb_attr_get(str, rb_id_encoding());
929 i = NIL_P(iv) ? ENCINDEX_ASCII_8BIT : NUM2INT(iv);
930#endif
931 }
932 return i;
933}
934
935int
936rb_enc_get_index(VALUE obj)
937{
938 int i = -1;
939 VALUE tmp;
940
941 if (SPECIAL_CONST_P(obj)) {
942 if (!SYMBOL_P(obj)) return -1;
943 obj = rb_sym2str(obj);
944 }
945 switch (BUILTIN_TYPE(obj)) {
946 case T_STRING:
947 case T_SYMBOL:
948 case T_REGEXP:
949 i = enc_get_index_str(obj);
950 break;
951 case T_FILE:
952 tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
953 if (NIL_P(tmp)) {
954 tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
955 }
956 if (is_obj_encoding(tmp)) {
957 i = enc_check_encoding(tmp);
958 }
959 break;
960 case T_DATA:
961 if (is_data_encoding(obj)) {
962 i = enc_check_encoding(obj);
963 }
964 break;
965 default:
966 break;
967 }
968 return i;
969}
970
971static void
972enc_set_index(VALUE obj, int idx)
973{
974 if (!enc_capable(obj)) {
975 rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object");
976 }
977
978 if (idx < ENCODING_INLINE_MAX) {
979 ENCODING_SET_INLINED(obj, idx);
980 return;
981 }
983 rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
984}
985
986void
987rb_enc_raw_set(VALUE obj, rb_encoding *enc)
988{
989 RUBY_ASSERT(enc_capable(obj));
990
991 int idx = enc ? ENC_TO_ENCINDEX(enc) : 0;
992
993 if (idx < ENCODING_INLINE_MAX) {
994 ENCODING_SET_INLINED(obj, idx);
995 return;
996 }
998 rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
999}
1000
1001void
1002rb_enc_set_index(VALUE obj, int idx)
1003{
1004 rb_check_frozen(obj);
1005 must_encindex(idx);
1006 enc_set_index(obj, idx);
1007}
1008
1009VALUE
1010rb_enc_associate_index(VALUE obj, int idx)
1011{
1012 rb_encoding *enc;
1013 int oldidx, oldtermlen, termlen;
1014
1015/* enc_check_capable(obj);*/
1016 rb_check_frozen(obj);
1017 oldidx = rb_enc_get_index(obj);
1018 if (oldidx == idx)
1019 return obj;
1020 if (SPECIAL_CONST_P(obj)) {
1021 rb_raise(rb_eArgError, "cannot set encoding");
1022 }
1023 enc = must_encindex(idx);
1024 if (!ENC_CODERANGE_ASCIIONLY(obj) ||
1025 !rb_enc_asciicompat(enc)) {
1027 }
1028 termlen = rb_enc_mbminlen(enc);
1029 oldtermlen = rb_enc_mbminlen(rb_enc_from_index(oldidx));
1030 if (oldtermlen != termlen && RB_TYPE_P(obj, T_STRING)) {
1031 rb_str_change_terminator_length(obj, oldtermlen, termlen);
1032 }
1033 enc_set_index(obj, idx);
1034 return obj;
1035}
1036
1037VALUE
1038rb_enc_associate(VALUE obj, rb_encoding *enc)
1039{
1040 return rb_enc_associate_index(obj, rb_enc_to_index(enc));
1041}
1042
1044rb_enc_get(VALUE obj)
1045{
1046 return rb_enc_from_index(rb_enc_get_index(obj));
1047}
1048
1049const char *
1050rb_enc_inspect_name(rb_encoding *enc)
1051{
1052 if (enc == global_enc_ascii) {
1053 return "BINARY (ASCII-8BIT)";
1054 }
1055 return enc->name;
1056}
1057
1058static rb_encoding*
1059rb_encoding_check(rb_encoding* enc, VALUE str1, VALUE str2)
1060{
1061 if (!enc)
1062 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
1063 rb_enc_inspect_name(rb_enc_get(str1)),
1064 rb_enc_inspect_name(rb_enc_get(str2)));
1065 return enc;
1066}
1067
1068static rb_encoding* enc_compatible_str(VALUE str1, VALUE str2);
1069
1071rb_enc_check_str(VALUE str1, VALUE str2)
1072{
1073 rb_encoding *enc = enc_compatible_str(MUST_STRING(str1), MUST_STRING(str2));
1074 return rb_encoding_check(enc, str1, str2);
1075}
1076
1078rb_enc_check(VALUE str1, VALUE str2)
1079{
1080 rb_encoding *enc = rb_enc_compatible(str1, str2);
1081 return rb_encoding_check(enc, str1, str2);
1082}
1083
1084static rb_encoding*
1085enc_compatible_latter(VALUE str1, VALUE str2, int idx1, int idx2)
1086{
1087 if (idx1 < 0 || idx2 < 0)
1088 return 0;
1089
1090 if (idx1 == idx2) {
1091 return rb_enc_from_index(idx1);
1092 }
1093
1094 int isstr1, isstr2;
1095 rb_encoding *enc1 = rb_enc_from_index(idx1);
1096 rb_encoding *enc2 = rb_enc_from_index(idx2);
1097
1098 isstr2 = RB_TYPE_P(str2, T_STRING);
1099 if (isstr2 && RSTRING_LEN(str2) == 0)
1100 return enc1;
1101 isstr1 = RB_TYPE_P(str1, T_STRING);
1102 if (isstr1 && isstr2 && RSTRING_LEN(str1) == 0)
1103 return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
1104 if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
1105 return 0;
1106 }
1107
1108 /* objects whose encoding is the same of contents */
1109 if (!isstr2 && idx2 == ENCINDEX_US_ASCII)
1110 return enc1;
1111 if (!isstr1 && idx1 == ENCINDEX_US_ASCII)
1112 return enc2;
1113
1114 if (!isstr1) {
1115 VALUE tmp = str1;
1116 int idx0 = idx1;
1117 str1 = str2;
1118 str2 = tmp;
1119 idx1 = idx2;
1120 idx2 = idx0;
1121 idx0 = isstr1;
1122 isstr1 = isstr2;
1123 isstr2 = idx0;
1124 }
1125 if (isstr1) {
1126 int cr1, cr2;
1127
1128 cr1 = rb_enc_str_coderange(str1);
1129 if (isstr2) {
1130 cr2 = rb_enc_str_coderange(str2);
1131 if (cr1 != cr2) {
1132 /* may need to handle ENC_CODERANGE_BROKEN */
1133 if (cr1 == ENC_CODERANGE_7BIT) return enc2;
1134 if (cr2 == ENC_CODERANGE_7BIT) return enc1;
1135 }
1136 if (cr2 == ENC_CODERANGE_7BIT) {
1137 return enc1;
1138 }
1139 }
1140 if (cr1 == ENC_CODERANGE_7BIT)
1141 return enc2;
1142 }
1143 return 0;
1144}
1145
1146static rb_encoding*
1147enc_compatible_str(VALUE str1, VALUE str2)
1148{
1149 int idx1 = enc_get_index_str(str1);
1150 int idx2 = enc_get_index_str(str2);
1151
1152 return enc_compatible_latter(str1, str2, idx1, idx2);
1153}
1154
1156rb_enc_compatible(VALUE str1, VALUE str2)
1157{
1158 int idx1 = rb_enc_get_index(str1);
1159 int idx2 = rb_enc_get_index(str2);
1160
1161 return enc_compatible_latter(str1, str2, idx1, idx2);
1162}
1163
1164void
1165rb_enc_copy(VALUE obj1, VALUE obj2)
1166{
1167 rb_enc_associate_index(obj1, rb_enc_get_index(obj2));
1168}
1169
1170
1171/*
1172 * call-seq:
1173 * obj.encoding -> encoding
1174 *
1175 * Returns the Encoding object that represents the encoding of obj.
1176 */
1177
1178VALUE
1179rb_obj_encoding(VALUE obj)
1180{
1181 int idx = rb_enc_get_index(obj);
1182 if (idx < 0) {
1183 rb_raise(rb_eTypeError, "unknown encoding");
1184 }
1185 return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
1186}
1187
1188int
1189rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
1190{
1191 return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1192}
1193
1194int
1195rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
1196{
1197 int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1198 if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
1199 return MBCLEN_CHARFOUND_LEN(n);
1200 else {
1201 int min = rb_enc_mbminlen(enc);
1202 return min <= e-p ? min : (int)(e-p);
1203 }
1204}
1205
1206int
1207rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
1208{
1209 int n;
1210 if (e <= p)
1211 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
1212 n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1213 if (e-p < n)
1214 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
1215 return n;
1216}
1217
1218int
1219rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
1220{
1221 unsigned int c;
1222 int l;
1223 if (e <= p)
1224 return -1;
1225 if (rb_enc_asciicompat(enc)) {
1226 c = (unsigned char)*p;
1227 if (!ISASCII(c))
1228 return -1;
1229 if (len) *len = 1;
1230 return c;
1231 }
1232 l = rb_enc_precise_mbclen(p, e, enc);
1233 if (!MBCLEN_CHARFOUND_P(l))
1234 return -1;
1235 c = rb_enc_mbc_to_codepoint(p, e, enc);
1236 if (!rb_enc_isascii(c, enc))
1237 return -1;
1238 if (len) *len = l;
1239 return c;
1240}
1241
1242unsigned int
1243rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
1244{
1245 int r;
1246 if (e <= p)
1247 rb_raise(rb_eArgError, "empty string");
1248 r = rb_enc_precise_mbclen(p, e, enc);
1249 if (!MBCLEN_CHARFOUND_P(r)) {
1250 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
1251 }
1252 if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
1253 return rb_enc_mbc_to_codepoint(p, e, enc);
1254}
1255
1256int
1257rb_enc_codelen(int c, rb_encoding *enc)
1258{
1259 int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
1260 if (n == 0) {
1261 rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
1262 }
1263 return n;
1264}
1265
1266int
1267rb_enc_toupper(int c, rb_encoding *enc)
1268{
1269 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
1270}
1271
1272int
1273rb_enc_tolower(int c, rb_encoding *enc)
1274{
1275 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
1276}
1277
1278/*
1279 * call-seq:
1280 * enc.inspect -> string
1281 *
1282 * Returns a string which represents the encoding for programmers.
1283 *
1284 * Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>"
1285 * Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
1286 */
1287static VALUE
1288enc_inspect(VALUE self)
1289{
1290 rb_encoding *enc;
1291
1292 if (!is_data_encoding(self)) {
1293 not_encoding(self);
1294 }
1295 if (!(enc = DATA_PTR(self)) || rb_enc_from_index(rb_enc_to_index(enc)) != enc) {
1296 rb_raise(rb_eTypeError, "broken Encoding");
1297 }
1298
1299 return rb_enc_sprintf(rb_usascii_encoding(),
1300 "#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
1301 rb_enc_inspect_name(enc),
1302 (ENC_DUMMY_P(enc) ? " (dummy)" : ""),
1303 rb_enc_autoload_p(enc) ? " (autoload)" : "");
1304}
1305
1306/*
1307 * call-seq:
1308 * enc.name -> string
1309 * enc.to_s -> string
1310 *
1311 * Returns the name of the encoding.
1312 *
1313 * Encoding::UTF_8.name #=> "UTF-8"
1314 */
1315static VALUE
1316enc_name(VALUE self)
1317{
1318 return rb_fstring_cstr(rb_enc_name((rb_encoding*)DATA_PTR(self)));
1319}
1320
1321static int
1322enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
1323{
1324 VALUE *arg = (VALUE *)args;
1325
1326 if ((int)idx == (int)arg[0]) {
1327 VALUE str = rb_interned_str_cstr((char *)name);
1328 rb_ary_push(arg[1], str);
1329 }
1330 return ST_CONTINUE;
1331}
1332
1333/*
1334 * call-seq:
1335 * enc.names -> array
1336 *
1337 * Returns the list of name and aliases of the encoding.
1338 *
1339 * Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"]
1340 */
1341static VALUE
1342enc_names(VALUE self)
1343{
1344 VALUE args[2];
1345
1346 args[0] = (VALUE)rb_to_encoding_index(self);
1347 args[1] = rb_ary_new2(0);
1348 st_foreach(global_enc_table.names, enc_names_i, (st_data_t)args);
1349 return args[1];
1350}
1351
1352/*
1353 * call-seq:
1354 * Encoding.list -> [enc1, enc2, ...]
1355 *
1356 * Returns the list of loaded encodings.
1357 *
1358 * Encoding.list
1359 * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
1360 * #<Encoding:ISO-2022-JP (dummy)>]
1361 *
1362 * Encoding.find("US-ASCII")
1363 * #=> #<Encoding:US-ASCII>
1364 *
1365 * Encoding.list
1366 * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
1367 * #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
1368 *
1369 */
1370static VALUE
1371enc_list(VALUE klass)
1372{
1373 VALUE ary = rb_ary_new2(0);
1374 rb_ary_replace(ary, rb_encoding_list);
1375 return ary;
1376}
1377
1378/*
1379 * call-seq:
1380 * Encoding.find(string) -> enc
1381 *
1382 * Search the encoding with specified <i>name</i>.
1383 * <i>name</i> should be a string.
1384 *
1385 * Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
1386 *
1387 * Names which this method accept are encoding names and aliases
1388 * including following special aliases
1389 *
1390 * "external":: default external encoding
1391 * "internal":: default internal encoding
1392 * "locale":: locale encoding
1393 * "filesystem":: filesystem encoding
1394 *
1395 * An ArgumentError is raised when no encoding with <i>name</i>.
1396 * Only <code>Encoding.find("internal")</code> however returns nil
1397 * when no encoding named "internal", in other words, when Ruby has no
1398 * default internal encoding.
1399 */
1400static VALUE
1401enc_find(VALUE klass, VALUE enc)
1402{
1403 int idx;
1404 if (is_obj_encoding(enc))
1405 return enc;
1406 idx = str_to_encindex(enc);
1407 if (idx == UNSPECIFIED_ENCODING) return Qnil;
1408 return rb_enc_from_encoding_index(idx);
1409}
1410
1411/*
1412 * call-seq:
1413 * Encoding.compatible?(obj1, obj2) -> enc or nil
1414 *
1415 * Checks the compatibility of two objects.
1416 *
1417 * If the objects are both strings they are compatible when they are
1418 * concatenatable. The encoding of the concatenated string will be returned
1419 * if they are compatible, nil if they are not.
1420 *
1421 * Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
1422 * #=> #<Encoding:ISO-8859-1>
1423 *
1424 * Encoding.compatible?(
1425 * "\xa1".force_encoding("iso-8859-1"),
1426 * "\xa1\xa1".force_encoding("euc-jp"))
1427 * #=> nil
1428 *
1429 * If the objects are non-strings their encodings are compatible when they
1430 * have an encoding and:
1431 * * Either encoding is US-ASCII compatible
1432 * * One of the encodings is a 7-bit encoding
1433 *
1434 */
1435static VALUE
1436enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
1437{
1438 rb_encoding *enc;
1439
1440 if (!enc_capable(str1)) return Qnil;
1441 if (!enc_capable(str2)) return Qnil;
1442 enc = rb_enc_compatible(str1, str2);
1443 if (!enc) return Qnil;
1444 return rb_enc_from_encoding(enc);
1445}
1446
1447NORETURN(static VALUE enc_s_alloc(VALUE klass));
1448/* :nodoc: */
1449static VALUE
1450enc_s_alloc(VALUE klass)
1451{
1452 rb_undefined_alloc(klass);
1454}
1455
1456/* :nodoc: */
1457static VALUE
1458enc_dump(int argc, VALUE *argv, VALUE self)
1459{
1460 rb_check_arity(argc, 0, 1);
1461 return enc_name(self);
1462}
1463
1464/* :nodoc: */
1465static VALUE
1466enc_load(VALUE klass, VALUE str)
1467{
1468 return str;
1469}
1470
1471/* :nodoc: */
1472static VALUE
1473enc_m_loader(VALUE klass, VALUE str)
1474{
1475 return enc_find(klass, str);
1476}
1477
1479rb_ascii8bit_encoding(void)
1480{
1481 return global_enc_ascii;
1482}
1483
1484int
1485rb_ascii8bit_encindex(void)
1486{
1487 return ENCINDEX_ASCII_8BIT;
1488}
1489
1491rb_utf8_encoding(void)
1492{
1493 return global_enc_utf_8;
1494}
1495
1496int
1497rb_utf8_encindex(void)
1498{
1499 return ENCINDEX_UTF_8;
1500}
1501
1503rb_usascii_encoding(void)
1504{
1505 return global_enc_us_ascii;
1506}
1507
1508int
1509rb_usascii_encindex(void)
1510{
1511 return ENCINDEX_US_ASCII;
1512}
1513
1514int rb_locale_charmap_index(void);
1515
1516int
1517rb_locale_encindex(void)
1518{
1519 int idx = rb_locale_charmap_index();
1520
1521 if (idx < 0) idx = ENCINDEX_UTF_8;
1522
1523 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1524 if (enc_registered(enc_table, "locale") < 0) {
1525# if defined _WIN32
1526 void Init_w32_codepage(void);
1527 Init_w32_codepage();
1528# endif
1529 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1530 enc_alias_internal(enc_table, "locale", idx);
1531 }
1532 }
1533 }
1534
1535 return idx;
1536}
1537
1539rb_locale_encoding(void)
1540{
1541 return rb_enc_from_index(rb_locale_encindex());
1542}
1543
1544int
1545rb_filesystem_encindex(void)
1546{
1547 int idx;
1548 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1549 idx = enc_registered(enc_table, "filesystem");
1550 }
1551 if (idx < 0) idx = ENCINDEX_ASCII_8BIT;
1552 return idx;
1553}
1554
1556rb_filesystem_encoding(void)
1557{
1558 return rb_enc_from_index(rb_filesystem_encindex());
1559}
1560
1562 int index; /* -2 => not yet set, -1 => nil */
1563 rb_encoding *enc;
1564};
1565
1566static struct default_encoding default_external = {0};
1567
1568static int
1569enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
1570{
1571 int overridden = FALSE;
1572
1573 if (def->index != -2)
1574 /* Already set */
1575 overridden = TRUE;
1576
1577 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1578 if (NIL_P(encoding)) {
1579 def->index = -1;
1580 def->enc = 0;
1581 char *name_dup = strdup(name);
1582
1583 st_data_t existing_name = (st_data_t)name_dup;
1584 if (st_delete(enc_table->names, &existing_name, NULL)) {
1585 xfree((void *)existing_name);
1586 }
1587
1588 st_insert(enc_table->names, (st_data_t)name_dup,
1589 (st_data_t)UNSPECIFIED_ENCODING);
1590 }
1591 else {
1592 def->index = rb_enc_to_index(rb_to_encoding(encoding));
1593 def->enc = 0;
1594 enc_alias_internal(enc_table, name, def->index);
1595 }
1596
1597 if (def == &default_external) {
1598 enc_alias_internal(enc_table, "filesystem", Init_enc_set_filesystem_encoding());
1599 }
1600 }
1601
1602 return overridden;
1603}
1604
1606rb_default_external_encoding(void)
1607{
1608 if (default_external.enc) return default_external.enc;
1609
1610 if (default_external.index >= 0) {
1611 default_external.enc = rb_enc_from_index(default_external.index);
1612 return default_external.enc;
1613 }
1614 else {
1615 return rb_locale_encoding();
1616 }
1617}
1618
1619VALUE
1620rb_enc_default_external(void)
1621{
1622 return rb_enc_from_encoding(rb_default_external_encoding());
1623}
1624
1625/*
1626 * call-seq:
1627 * Encoding.default_external -> enc
1628 *
1629 * Returns default external encoding.
1630 *
1631 * The default external encoding is used by default for strings created from
1632 * the following locations:
1633 *
1634 * * CSV
1635 * * File data read from disk
1636 * * SDBM
1637 * * StringIO
1638 * * Zlib::GzipReader
1639 * * Zlib::GzipWriter
1640 * * String#inspect
1641 * * Regexp#inspect
1642 *
1643 * While strings created from these locations will have this encoding, the
1644 * encoding may not be valid. Be sure to check String#valid_encoding?.
1645 *
1646 * File data written to disk will be transcoded to the default external
1647 * encoding when written, if default_internal is not nil.
1648 *
1649 * The default external encoding is initialized by the -E option.
1650 * If -E isn't set, it is initialized to UTF-8 on Windows and the locale on
1651 * other operating systems.
1652 */
1653static VALUE
1654get_default_external(VALUE klass)
1655{
1656 return rb_enc_default_external();
1657}
1658
1659void
1660rb_enc_set_default_external(VALUE encoding)
1661{
1662 if (NIL_P(encoding)) {
1663 rb_raise(rb_eArgError, "default external can not be nil");
1664 }
1665 enc_set_default_encoding(&default_external, encoding,
1666 "external");
1667}
1668
1669/*
1670 * call-seq:
1671 * Encoding.default_external = enc
1672 *
1673 * Sets default external encoding. You should not set
1674 * Encoding::default_external in ruby code as strings created before changing
1675 * the value may have a different encoding from strings created after the value
1676 * was changed., instead you should use <tt>ruby -E</tt> to invoke ruby with
1677 * the correct default_external.
1678 *
1679 * See Encoding::default_external for information on how the default external
1680 * encoding is used.
1681 */
1682static VALUE
1683set_default_external(VALUE klass, VALUE encoding)
1684{
1685 rb_warning("setting Encoding.default_external");
1686 rb_enc_set_default_external(encoding);
1687 return encoding;
1688}
1689
1690static struct default_encoding default_internal = {-2};
1691
1693rb_default_internal_encoding(void)
1694{
1695 if (!default_internal.enc && default_internal.index >= 0) {
1696 default_internal.enc = rb_enc_from_index(default_internal.index);
1697 }
1698 return default_internal.enc; /* can be NULL */
1699}
1700
1701VALUE
1702rb_enc_default_internal(void)
1703{
1704 /* Note: These functions cope with default_internal not being set */
1705 return rb_enc_from_encoding(rb_default_internal_encoding());
1706}
1707
1708/*
1709 * call-seq:
1710 * Encoding.default_internal -> enc
1711 *
1712 * Returns default internal encoding. Strings will be transcoded to the
1713 * default internal encoding in the following places if the default internal
1714 * encoding is not nil:
1715 *
1716 * * CSV
1717 * * Etc.sysconfdir and Etc.systmpdir
1718 * * File data read from disk
1719 * * File names from Dir
1720 * * Integer#chr
1721 * * String#inspect and Regexp#inspect
1722 * * Strings returned from Readline
1723 * * Strings returned from SDBM
1724 * * Time#zone
1725 * * Values from ENV
1726 * * Values in ARGV including $PROGRAM_NAME
1727 *
1728 * Additionally String#encode and String#encode! use the default internal
1729 * encoding if no encoding is given.
1730 *
1731 * The script encoding (__ENCODING__), not default_internal, is used as the
1732 * encoding of created strings.
1733 *
1734 * Encoding::default_internal is initialized with -E option or nil otherwise.
1735 */
1736static VALUE
1737get_default_internal(VALUE klass)
1738{
1739 return rb_enc_default_internal();
1740}
1741
1742void
1743rb_enc_set_default_internal(VALUE encoding)
1744{
1745 enc_set_default_encoding(&default_internal, encoding,
1746 "internal");
1747}
1748
1749/*
1750 * call-seq:
1751 * Encoding.default_internal = enc or nil
1752 *
1753 * Sets default internal encoding or removes default internal encoding when
1754 * passed nil. You should not set Encoding::default_internal in ruby code as
1755 * strings created before changing the value may have a different encoding
1756 * from strings created after the change. Instead you should use
1757 * <tt>ruby -E</tt> to invoke ruby with the correct default_internal.
1758 *
1759 * See Encoding::default_internal for information on how the default internal
1760 * encoding is used.
1761 */
1762static VALUE
1763set_default_internal(VALUE klass, VALUE encoding)
1764{
1765 rb_warning("setting Encoding.default_internal");
1766 rb_enc_set_default_internal(encoding);
1767 return encoding;
1768}
1769
1770static void
1771set_encoding_const(const char *name, rb_encoding *enc)
1772{
1773 VALUE encoding = rb_enc_from_encoding(enc);
1774 char *s = (char *)name;
1775 int haslower = 0, hasupper = 0, valid = 0;
1776
1777 if (ISDIGIT(*s)) return;
1778 if (ISUPPER(*s)) {
1779 hasupper = 1;
1780 while (*++s && (ISALNUM(*s) || *s == '_')) {
1781 if (ISLOWER(*s)) haslower = 1;
1782 }
1783 }
1784 if (!*s) {
1785 if (s - name > ENCODING_NAMELEN_MAX) return;
1786 valid = 1;
1787 rb_define_const(rb_cEncoding, name, encoding);
1788 }
1789 if (!valid || haslower) {
1790 size_t len = s - name;
1791 if (len > ENCODING_NAMELEN_MAX) return;
1792 if (!haslower || !hasupper) {
1793 do {
1794 if (ISLOWER(*s)) haslower = 1;
1795 if (ISUPPER(*s)) hasupper = 1;
1796 } while (*++s && (!haslower || !hasupper));
1797 len = s - name;
1798 }
1799 len += strlen(s);
1800 if (len++ > ENCODING_NAMELEN_MAX) return;
1801 MEMCPY(s = ALLOCA_N(char, len), name, char, len);
1802 name = s;
1803 if (!valid) {
1804 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
1805 for (; *s; ++s) {
1806 if (!ISALNUM(*s)) *s = '_';
1807 }
1808 if (hasupper) {
1809 rb_define_const(rb_cEncoding, name, encoding);
1810 }
1811 }
1812 if (haslower) {
1813 for (s = (char *)name; *s; ++s) {
1814 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
1815 }
1816 rb_define_const(rb_cEncoding, name, encoding);
1817 }
1818 }
1819}
1820
1821static int
1822rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
1823{
1824 VALUE ary = (VALUE)arg;
1825 VALUE str = rb_interned_str_cstr((char *)name);
1826 rb_ary_push(ary, str);
1827 return ST_CONTINUE;
1828}
1829
1830/*
1831 * call-seq:
1832 * Encoding.name_list -> ["enc1", "enc2", ...]
1833 *
1834 * Returns the list of available encoding names.
1835 *
1836 * Encoding.name_list
1837 * #=> ["US-ASCII", "ASCII-8BIT", "UTF-8",
1838 * "ISO-8859-1", "Shift_JIS", "EUC-JP",
1839 * "Windows-31J",
1840 * "BINARY", "CP932", "eucJP"]
1841 *
1842 */
1843
1844static VALUE
1845rb_enc_name_list(VALUE klass)
1846{
1847 VALUE ary = rb_ary_new2(global_enc_table.names->num_entries);
1848 st_foreach(global_enc_table.names, rb_enc_name_list_i, (st_data_t)ary);
1849 return ary;
1850}
1851
1852static int
1853rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
1854{
1855 VALUE *p = (VALUE *)arg;
1856 VALUE aliases = p[0], ary = p[1];
1857 int idx = (int)orig;
1858 VALUE key, str = rb_ary_entry(ary, idx);
1859
1860 if (NIL_P(str)) {
1861 rb_encoding *enc = rb_enc_from_index(idx);
1862
1863 if (!enc) return ST_CONTINUE;
1864 if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
1865 return ST_CONTINUE;
1866 }
1867 str = rb_fstring_cstr(rb_enc_name(enc));
1868 rb_ary_store(ary, idx, str);
1869 }
1870 key = rb_interned_str_cstr((char *)name);
1871 rb_hash_aset(aliases, key, str);
1872 return ST_CONTINUE;
1873}
1874
1875/*
1876 * call-seq:
1877 * Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...}
1878 *
1879 * Returns the hash of available encoding alias and original encoding name.
1880 *
1881 * Encoding.aliases
1882 * #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII",
1883 * "SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
1884 *
1885 */
1886
1887static VALUE
1888rb_enc_aliases(VALUE klass)
1889{
1890 VALUE aliases[2];
1891 aliases[0] = rb_hash_new();
1892 aliases[1] = rb_ary_new();
1893
1894 st_foreach(global_enc_table.names, rb_enc_aliases_enc_i, (st_data_t)aliases);
1895
1896 return aliases[0];
1897}
1898
1899/*
1900 * An \Encoding instance represents a character encoding usable in Ruby.
1901 * It is defined as a constant under the \Encoding namespace.
1902 * It has a name and, optionally, aliases:
1903 *
1904 * Encoding::US_ASCII.name # => "US-ASCII"
1905 * Encoding::US_ASCII.names # => ["US-ASCII", "ASCII", "ANSI_X3.4-1968", "646"]
1906 *
1907 * A Ruby method that accepts an encoding as an argument will accept:
1908 *
1909 * - An \Encoding object.
1910 * - The name of an encoding.
1911 * - An alias for an encoding name.
1912 *
1913 * These are equivalent:
1914 *
1915 * 'foo'.encode(Encoding::US_ASCII) # Encoding object.
1916 * 'foo'.encode('US-ASCII') # Encoding name.
1917 * 'foo'.encode('ASCII') # Encoding alias.
1918 *
1919 * For a full discussion of encodings and their uses,
1920 * see {the Encodings document}[rdoc-ref:encodings.rdoc].
1921 *
1922 * Encoding::ASCII_8BIT is a special-purpose encoding that is usually used for
1923 * a string of bytes, not a string of characters.
1924 * But as the name indicates, its characters in the ASCII range
1925 * are considered as ASCII characters.
1926 * This is useful when you use other ASCII-compatible encodings.
1927 *
1928 */
1929
1930void
1931Init_Encoding(void)
1932{
1933 VALUE list;
1934 int i;
1935
1936 rb_cEncoding = rb_define_class("Encoding", rb_cObject);
1937 rb_define_alloc_func(rb_cEncoding, enc_s_alloc);
1939 rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
1940 rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
1941 rb_define_method(rb_cEncoding, "name", enc_name, 0);
1942 rb_define_method(rb_cEncoding, "names", enc_names, 0);
1943 rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
1944 rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0);
1945 rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
1946 rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0);
1947 rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0);
1948 rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
1949 rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
1950
1951 rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
1952 rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
1953
1954 rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
1955 rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
1956 rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0);
1957 rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
1958 rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); /* in localeinit.c */
1959
1960 struct enc_table *enc_table = &global_enc_table;
1961
1962 list = rb_encoding_list = rb_ary_new2(ENCODING_LIST_CAPA);
1963 RBASIC_CLEAR_CLASS(list);
1964 rb_vm_register_global_object(list);
1965
1966 for (i = 0; i < enc_table->count; ++i) {
1967 rb_ary_push(list, enc_new(enc_table->list[i].enc));
1968 }
1969
1970 rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);
1971}
1972
1973void
1974Init_unicode_version(void)
1975{
1976 extern const char onigenc_unicode_version_string[];
1977
1978 VALUE str = rb_usascii_str_new_static(onigenc_unicode_version_string,
1979 strlen(onigenc_unicode_version_string));
1980 OBJ_FREEZE(str);
1981 rb_define_const(rb_cEncoding, "UNICODE_VERSION", str);
1982}
1983
1984void
1985Init_encodings(void)
1986{
1987 rb_enc_init(&global_enc_table);
1988}
1989
1990/* locale insensitive ctype functions */
1991
1992void
1993rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
1994{
1995 st_foreach(global_enc_table.names, func, arg);
1996}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
Atomic operations.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:150
#define RUBY_ATOMIC_SET(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except for the return type.
Definition atomic.h:160
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
Definition sprintf.c:1198
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:280
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1479
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2663
#define ENCODING_SET_INLINED(obj, i)
Old name of RB_ENCODING_SET_INLINED.
Definition encoding.h:106
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define ISUPPER
Old name of rb_isupper.
Definition ctype.h:89
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:206
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
#define ISLOWER
Old name of rb_islower.
Definition ctype.h:90
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:517
#define ENCODING_INLINE_MAX
Old name of RUBY_ENCODING_INLINE_MAX.
Definition encoding.h:67
#define STRCASECMP
Old name of st_locale_insensitive_strcasecmp.
Definition ctype.h:102
#define ISASCII
Old name of rb_isascii.
Definition ctype.h:85
#define TOLOWER
Old name of rb_tolower.
Definition ctype.h:101
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define ENC_CODERANGE_ASCIIONLY(obj)
Old name of RB_ENC_CODERANGE_ASCIIONLY.
Definition coderange.h:185
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define ENCODING_GET_INLINED(obj)
Old name of RB_ENCODING_GET_INLINED.
Definition encoding.h:108
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
Definition coderange.h:187
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define ISALNUM
Old name of rb_isalnum.
Definition ctype.h:91
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:129
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition error.h:486
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1437
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
Definition error.c:3812
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1436
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
VALUE rb_cEncoding
Encoding class.
Definition encoding.c:58
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1299
Encoding relates APIs.
VALUE rb_locale_charmap(VALUE klass)
Returns a platform-depended "charmap" of the current locale.
Definition localeinit.c:91
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:931
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:950
VALUE rb_ary_replace(VALUE copy, VALUE orig)
Replaces the contents of the former object with the contents of the latter.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_interned_str_cstr(const char *ptr)
Identical to rb_interned_str(), except it assumes the passed pointer is a pointer to a C's string.
Definition string.c:12628
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2908
VALUE rb_usascii_str_new_static(const char *ptr, long len)
Identical to rb_str_new_static(), except it generates a string of "US ASCII" encoding instead of "bin...
Definition string.c:1169
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2094
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1447
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:999
int len
Length of the buffer.
Definition io.h:8
#define strdup(s)
Just another name of ruby_strdup.
Definition util.h:187
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:137
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
#define RDATA(obj)
Convenient casting macro.
Definition rdata.h:59
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:442
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:450
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:203
Definition encoding.c:63
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376