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