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