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