Ruby 4.1.0dev (2026-06-15 revision 4def576d21a768c85db6d79d99ae339314e3de5c)
encoding.c (4def576d21a768c85db6d79d99ae339314e3de5c)
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_FREE_IMMEDIATELY | 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 FL_SET_RAW(new_list, RUBY_FL_SHAREABLE);
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
1201static rb_encoding*
1202enc_compatible_str(VALUE str1, VALUE str2)
1203{
1204 int idx1 = enc_get_index_str(str1);
1205 int idx2 = enc_get_index_str(str2);
1206
1207 return enc_compatible_latter(str1, str2, idx1, idx2);
1208}
1209
1211rb_enc_compatible(VALUE str1, VALUE str2)
1212{
1213 int idx1 = rb_enc_get_index(str1);
1214 int idx2 = rb_enc_get_index(str2);
1215
1216 return enc_compatible_latter(str1, str2, idx1, idx2);
1217}
1218
1219void
1220rb_enc_copy(VALUE obj1, VALUE obj2)
1221{
1222 rb_enc_associate_index(obj1, rb_enc_get_index(obj2));
1223}
1224
1225
1226/*
1227 * call-seq:
1228 * encoding -> encoding
1229 *
1230 * Returns an Encoding object that represents the encoding of +self+;
1231 * see {Encodings}[rdoc-ref:encodings.rdoc].
1232 *
1233 * Related: see {Querying}[rdoc-ref:String@Querying].
1234 */
1235
1236VALUE
1237rb_obj_encoding(VALUE obj)
1238{
1239 int idx = rb_enc_get_index(obj);
1240 if (idx < 0) {
1241 rb_raise(rb_eTypeError, "unknown encoding");
1242 }
1243 return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
1244}
1245
1246int
1247rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
1248{
1249 return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1250}
1251
1252int
1253rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
1254{
1255 int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1256 if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
1257 return MBCLEN_CHARFOUND_LEN(n);
1258 else {
1259 int min = rb_enc_mbminlen(enc);
1260 return min <= e-p ? min : (int)(e-p);
1261 }
1262}
1263
1264int
1265rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
1266{
1267 int n;
1268 if (e <= p)
1269 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
1270 n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
1271 if (e-p < n)
1272 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
1273 return n;
1274}
1275
1276int
1277rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
1278{
1279 unsigned int c;
1280 int l;
1281 if (e <= p)
1282 return -1;
1283 if (rb_enc_asciicompat(enc)) {
1284 c = (unsigned char)*p;
1285 if (!ISASCII(c))
1286 return -1;
1287 if (len) *len = 1;
1288 return c;
1289 }
1290 l = rb_enc_precise_mbclen(p, e, enc);
1291 if (!MBCLEN_CHARFOUND_P(l))
1292 return -1;
1293 c = rb_enc_mbc_to_codepoint(p, e, enc);
1294 if (!rb_enc_isascii(c, enc))
1295 return -1;
1296 if (len) *len = l;
1297 return c;
1298}
1299
1300unsigned int
1301rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
1302{
1303 int r;
1304 if (e <= p)
1305 rb_raise(rb_eArgError, "empty string");
1306 r = rb_enc_precise_mbclen(p, e, enc);
1307 if (!MBCLEN_CHARFOUND_P(r)) {
1308 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
1309 }
1310 if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
1311 return rb_enc_mbc_to_codepoint(p, e, enc);
1312}
1313
1314int
1315rb_enc_codelen(int c, rb_encoding *enc)
1316{
1317 int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
1318 if (n == 0) {
1319 rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
1320 }
1321 return n;
1322}
1323
1324int
1325rb_enc_toupper(int c, rb_encoding *enc)
1326{
1327 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
1328}
1329
1330int
1331rb_enc_tolower(int c, rb_encoding *enc)
1332{
1333 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
1334}
1335
1336/*
1337 * call-seq:
1338 * enc.inspect -> string
1339 *
1340 * Returns a string which represents the encoding for programmers.
1341 *
1342 * Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>"
1343 * Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
1344 */
1345static VALUE
1346enc_inspect(VALUE self)
1347{
1348 rb_encoding *enc;
1349
1350 if (!is_obj_encoding(self)) { /* do not resolve autoload */
1351 not_encoding(self);
1352 }
1353 if (!(enc = RTYPEDDATA_GET_DATA(self)) || rb_enc_from_index(rb_enc_to_index(enc)) != enc) {
1354 rb_raise(rb_eTypeError, "broken Encoding");
1355 }
1356
1357 return rb_enc_sprintf(rb_usascii_encoding(),
1358 "#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
1359 rb_enc_inspect_name(enc),
1360 (ENC_DUMMY_P(enc) ? " (dummy)" : ""),
1361 rb_enc_autoload_p(enc) ? " (autoload)" : "");
1362}
1363
1364
1365static int
1366enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
1367{
1368 VALUE *arg = (VALUE *)args;
1369
1370 if ((int)idx == (int)arg[0]) {
1371 VALUE str = rb_interned_str_cstr((char *)name);
1372 rb_ary_push(arg[1], str);
1373 }
1374 return ST_CONTINUE;
1375}
1376
1377/*
1378 * call-seq:
1379 * enc.names -> array
1380 *
1381 * Returns the list of name and aliases of the encoding.
1382 *
1383 * Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"]
1384 */
1385static VALUE
1386enc_names(VALUE self)
1387{
1388 VALUE args[2];
1389
1390 args[0] = (VALUE)rb_to_encoding_index(self);
1391 args[1] = rb_ary_new2(0);
1392
1393 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1394 st_foreach(enc_table->names, enc_names_i, (st_data_t)args);
1395 }
1396 return args[1];
1397}
1398
1399/*
1400 * call-seq:
1401 * Encoding.list -> [enc1, enc2, ...]
1402 *
1403 * Returns the list of loaded encodings.
1404 *
1405 * Encoding.list
1406 * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
1407 * #<Encoding:ISO-2022-JP (dummy)>]
1408 *
1409 * Encoding.find("US-ASCII")
1410 * #=> #<Encoding:US-ASCII>
1411 *
1412 * Encoding.list
1413 * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
1414 * #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
1415 *
1416 */
1417static VALUE
1418enc_list(VALUE klass)
1419{
1420 VALUE list = RUBY_ATOMIC_VALUE_LOAD(rb_encoding_list);
1421 return rb_ary_dup(list);
1422}
1423
1424/*
1425 * call-seq:
1426 * Encoding.find(string) -> enc
1427 *
1428 * Search the encoding with specified <i>name</i>.
1429 * <i>name</i> should be a string.
1430 *
1431 * Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
1432 *
1433 * Names which this method accept are encoding names and aliases
1434 * including following special aliases
1435 *
1436 * "external":: default external encoding
1437 * "internal":: default internal encoding
1438 * "locale":: locale encoding
1439 * "filesystem":: filesystem encoding
1440 *
1441 * An ArgumentError is raised when no encoding with <i>name</i>.
1442 * Only <code>Encoding.find("internal")</code> however returns nil
1443 * when no encoding named "internal", in other words, when Ruby has no
1444 * default internal encoding.
1445 */
1446static VALUE
1447enc_find(VALUE klass, VALUE enc)
1448{
1449 int idx;
1450 if (is_obj_encoding(enc))
1451 return enc;
1452 idx = str_to_encindex(enc);
1453 if (idx == UNSPECIFIED_ENCODING) return Qnil;
1454 return rb_enc_from_encoding_index(idx);
1455}
1456
1457/*
1458 * call-seq:
1459 * Encoding.compatible?(obj1, obj2) -> enc or nil
1460 *
1461 * Checks the compatibility of two objects.
1462 *
1463 * If the objects are both strings they are compatible when they are
1464 * concatenatable. The encoding of the concatenated string will be returned
1465 * if they are compatible, nil if they are not.
1466 *
1467 * Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
1468 * #=> #<Encoding:ISO-8859-1>
1469 *
1470 * Encoding.compatible?(
1471 * "\xa1".force_encoding("iso-8859-1"),
1472 * "\xa1\xa1".force_encoding("euc-jp"))
1473 * #=> nil
1474 *
1475 * If the objects are non-strings their encodings are compatible when they
1476 * have an encoding and:
1477 * * Either encoding is US-ASCII compatible
1478 * * One of the encodings is a 7-bit encoding
1479 *
1480 */
1481static VALUE
1482enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
1483{
1484 rb_encoding *enc;
1485
1486 if (!enc_capable(str1)) return Qnil;
1487 if (!enc_capable(str2)) return Qnil;
1488 enc = rb_enc_compatible(str1, str2);
1489 if (!enc) return Qnil;
1490 return rb_enc_from_encoding(enc);
1491}
1492
1493NORETURN(static VALUE enc_s_alloc(VALUE klass));
1494/* :nodoc: */
1495static VALUE
1496enc_s_alloc(VALUE klass)
1497{
1498 rb_undefined_alloc(klass);
1500}
1501
1502/* :nodoc: */
1503static VALUE
1504enc_dump(int argc, VALUE *argv, VALUE self)
1505{
1506 rb_check_arity(argc, 0, 1);
1507 return rb_attr_get(self, id_i_name);
1508}
1509
1510/* :nodoc: */
1511static VALUE
1512enc_load(VALUE klass, VALUE str)
1513{
1514 return str;
1515}
1516
1517/* :nodoc: */
1518static VALUE
1519enc_m_loader(VALUE klass, VALUE str)
1520{
1521 return enc_find(klass, str);
1522}
1523
1525rb_ascii8bit_encoding(void)
1526{
1527 return global_enc_ascii;
1528}
1529
1530int
1531rb_ascii8bit_encindex(void)
1532{
1533 return ENCINDEX_ASCII_8BIT;
1534}
1535
1537rb_utf8_encoding(void)
1538{
1539 return global_enc_utf_8;
1540}
1541
1542int
1543rb_utf8_encindex(void)
1544{
1545 return ENCINDEX_UTF_8;
1546}
1547
1549rb_usascii_encoding(void)
1550{
1551 return global_enc_us_ascii;
1552}
1553
1554int
1555rb_usascii_encindex(void)
1556{
1557 return ENCINDEX_US_ASCII;
1558}
1559
1560int rb_locale_charmap_index(void);
1561
1562int
1563rb_locale_encindex(void)
1564{
1565 // `rb_locale_charmap_index` can call `enc_find_index`, which can
1566 // load an encoding. This needs to be done without VM lock held.
1567 ASSERT_vm_unlocking();
1568 int idx = rb_locale_charmap_index();
1569
1570 if (idx < 0) idx = ENCINDEX_UTF_8;
1571
1572 if (!RUBY_ATOMIC_LOAD(locale_alias_registered)) {
1573 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1574 if (enc_registered(enc_table, "locale") < 0) {
1575# if defined _WIN32
1576 void Init_w32_codepage(void);
1577 Init_w32_codepage();
1578# endif
1579 enc_alias_internal(enc_table, "locale", idx);
1580 }
1581 RUBY_ATOMIC_SET(locale_alias_registered, 1);
1582 }
1583 }
1584
1585 return idx;
1586}
1587
1589rb_locale_encoding(void)
1590{
1591 return rb_enc_from_index(rb_locale_encindex());
1592}
1593
1594int
1595rb_filesystem_encindex(void)
1596{
1597 return filesystem_encindex;
1598}
1599
1601rb_filesystem_encoding(void)
1602{
1603 return rb_enc_from_index(rb_filesystem_encindex());
1604}
1605
1607 int index; /* -2 => not yet set, -1 => nil */
1608 rb_encoding *enc;
1609};
1610
1611static struct default_encoding default_external = {0};
1612
1613static int
1614enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
1615{
1616 int overridden = FALSE;
1617
1618 if (def->index != -2)
1619 /* Already set */
1620 overridden = TRUE;
1621
1622 int index = 0;
1623 if (!NIL_P(encoding)) {
1624 enc_check_encoding(encoding); // loads it if necessary. Needs to be done outside of VM lock.
1625 index = rb_enc_to_index(rb_to_encoding(encoding));
1626 }
1627
1628 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1629 if (NIL_P(encoding)) {
1630 def->index = -1;
1631 def->enc = 0;
1632 char *name_dup = strdup(name);
1633
1634 st_data_t existing_name = (st_data_t)name_dup;
1635 if (st_delete(enc_table->names, &existing_name, NULL)) {
1636 xfree((void *)existing_name);
1637 }
1638
1639 st_insert(enc_table->names, (st_data_t)name_dup,
1640 (st_data_t)UNSPECIFIED_ENCODING);
1641 }
1642 else {
1643 def->index = index;
1644 def->enc = 0;
1645 enc_alias_internal(enc_table, name, def->index);
1646 }
1647
1648 if (def == &default_external) {
1649 int fs_idx = Init_enc_set_filesystem_encoding();
1650 enc_alias_internal(enc_table, "filesystem", fs_idx);
1651 filesystem_encindex = fs_idx;
1652 }
1653 }
1654
1655 return overridden;
1656}
1657
1659rb_default_external_encoding(void)
1660{
1661 if (default_external.enc) return default_external.enc;
1662
1663 if (default_external.index >= 0) {
1664 default_external.enc = rb_enc_from_index(default_external.index);
1665 return default_external.enc;
1666 }
1667 else {
1668 return rb_locale_encoding();
1669 }
1670}
1671
1672VALUE
1673rb_enc_default_external(void)
1674{
1675 return rb_enc_from_encoding(rb_default_external_encoding());
1676}
1677
1678/*
1679 * call-seq:
1680 * Encoding.default_external -> enc
1681 *
1682 * Returns default external encoding.
1683 *
1684 * The default external encoding is used by default for strings created from
1685 * the following locations:
1686 *
1687 * * CSV
1688 * * File data read from disk
1689 * * SDBM
1690 * * StringIO
1691 * * Zlib::GzipReader
1692 * * Zlib::GzipWriter
1693 * * String#inspect
1694 * * Regexp#inspect
1695 *
1696 * While strings created from these locations will have this encoding, the
1697 * encoding may not be valid. Be sure to check String#valid_encoding?.
1698 *
1699 * File data written to disk will be transcoded to the default external
1700 * encoding when written, if default_internal is not nil.
1701 *
1702 * The default external encoding is initialized by the -E option.
1703 * If -E isn't set, it is initialized to UTF-8 on Windows and the locale on
1704 * other operating systems.
1705 */
1706static VALUE
1707get_default_external(VALUE klass)
1708{
1709 return rb_enc_default_external();
1710}
1711
1712void
1713rb_enc_set_default_external(VALUE encoding)
1714{
1715 if (NIL_P(encoding)) {
1716 rb_raise(rb_eArgError, "default external can not be nil");
1717 }
1718 enc_set_default_encoding(&default_external, encoding,
1719 "external");
1720}
1721
1722/*
1723 * call-seq:
1724 * Encoding.default_external = enc
1725 *
1726 * Sets default external encoding. You should not set
1727 * Encoding::default_external in ruby code as strings created before changing
1728 * the value may have a different encoding from strings created after the value
1729 * was changed., instead you should use <tt>ruby -E</tt> to invoke ruby with
1730 * the correct default_external.
1731 *
1732 * See Encoding::default_external for information on how the default external
1733 * encoding is used.
1734 */
1735static VALUE
1736set_default_external(VALUE klass, VALUE encoding)
1737{
1738 rb_warning("setting Encoding.default_external");
1739 rb_enc_set_default_external(encoding);
1740 return encoding;
1741}
1742
1743static struct default_encoding default_internal = {-2};
1744
1746rb_default_internal_encoding(void)
1747{
1748 if (!default_internal.enc && default_internal.index >= 0) {
1749 default_internal.enc = rb_enc_from_index(default_internal.index);
1750 }
1751 return default_internal.enc; /* can be NULL */
1752}
1753
1754VALUE
1755rb_enc_default_internal(void)
1756{
1757 /* Note: These functions cope with default_internal not being set */
1758 return rb_enc_from_encoding(rb_default_internal_encoding());
1759}
1760
1761/*
1762 * call-seq:
1763 * Encoding.default_internal -> enc
1764 *
1765 * Returns default internal encoding. Strings will be transcoded to the
1766 * default internal encoding in the following places if the default internal
1767 * encoding is not nil:
1768 *
1769 * * CSV
1770 * * Etc.sysconfdir and Etc.systmpdir
1771 * * File data read from disk
1772 * * File names from Dir
1773 * * Integer#chr
1774 * * String#inspect and Regexp#inspect
1775 * * Strings returned from Readline
1776 * * Strings returned from SDBM
1777 * * Time#zone
1778 * * Values from ENV
1779 * * Values in ARGV including $PROGRAM_NAME
1780 *
1781 * Additionally String#encode and String#encode! use the default internal
1782 * encoding if no encoding is given.
1783 *
1784 * The script encoding (__ENCODING__), not default_internal, is used as the
1785 * encoding of created strings.
1786 *
1787 * Encoding::default_internal is initialized with -E option or nil otherwise.
1788 */
1789static VALUE
1790get_default_internal(VALUE klass)
1791{
1792 return rb_enc_default_internal();
1793}
1794
1795void
1796rb_enc_set_default_internal(VALUE encoding)
1797{
1798 enc_set_default_encoding(&default_internal, encoding,
1799 "internal");
1800}
1801
1802/*
1803 * call-seq:
1804 * Encoding.default_internal = enc or nil
1805 *
1806 * Sets default internal encoding or removes default internal encoding when
1807 * passed nil. You should not set Encoding::default_internal in ruby code as
1808 * strings created before changing the value may have a different encoding
1809 * from strings created after the change. Instead you should use
1810 * <tt>ruby -E</tt> to invoke ruby with the correct default_internal.
1811 *
1812 * See Encoding::default_internal for information on how the default internal
1813 * encoding is used.
1814 */
1815static VALUE
1816set_default_internal(VALUE klass, VALUE encoding)
1817{
1818 rb_warning("setting Encoding.default_internal");
1819 rb_enc_set_default_internal(encoding);
1820 return encoding;
1821}
1822
1823static void
1824set_encoding_const(const char *name, rb_encoding *enc)
1825{
1826 VALUE encoding = rb_enc_from_encoding(enc);
1827 char *s = (char *)name;
1828 int haslower = 0, hasupper = 0, valid = 0;
1829
1830 if (ISDIGIT(*s)) return;
1831 if (ISUPPER(*s)) {
1832 hasupper = 1;
1833 while (*++s && (ISALNUM(*s) || *s == '_')) {
1834 if (ISLOWER(*s)) haslower = 1;
1835 }
1836 }
1837 if (!*s) {
1838 if (s - name > ENCODING_NAMELEN_MAX) return;
1839 valid = 1;
1840 rb_define_const(rb_cEncoding, name, encoding);
1841 }
1842 if (!valid || haslower) {
1843 size_t len = s - name;
1844 if (len > ENCODING_NAMELEN_MAX) return;
1845 if (!haslower || !hasupper) {
1846 do {
1847 if (ISLOWER(*s)) haslower = 1;
1848 if (ISUPPER(*s)) hasupper = 1;
1849 } while (*++s && (!haslower || !hasupper));
1850 len = s - name;
1851 }
1852 len += strlen(s);
1853 if (len++ > ENCODING_NAMELEN_MAX) return;
1854 MEMCPY(s = ALLOCA_N(char, len), name, char, len);
1855 name = s;
1856 if (!valid) {
1857 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
1858 for (; *s; ++s) {
1859 if (!ISALNUM(*s)) *s = '_';
1860 }
1861 if (hasupper) {
1862 rb_define_const(rb_cEncoding, name, encoding);
1863 }
1864 }
1865 if (haslower) {
1866 for (s = (char *)name; *s; ++s) {
1867 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
1868 }
1869 rb_define_const(rb_cEncoding, name, encoding);
1870 }
1871 }
1872}
1873
1874static int
1875rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
1876{
1877 VALUE ary = (VALUE)arg;
1878 VALUE str = rb_interned_str_cstr((char *)name);
1879 rb_ary_push(ary, str);
1880 return ST_CONTINUE;
1881}
1882
1883/*
1884 * call-seq:
1885 * Encoding.name_list -> ["enc1", "enc2", ...]
1886 *
1887 * Returns the list of available encoding names.
1888 *
1889 * Encoding.name_list
1890 * #=> ["US-ASCII", "ASCII-8BIT", "UTF-8",
1891 * "ISO-8859-1", "Shift_JIS", "EUC-JP",
1892 * "Windows-31J",
1893 * "BINARY", "CP932", "eucJP"]
1894 *
1895 */
1896
1897static VALUE
1898rb_enc_name_list(VALUE klass)
1899{
1900 VALUE ary;
1901 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1902 ary = rb_ary_new2(enc_table->names->num_entries);
1903 st_foreach(enc_table->names, rb_enc_name_list_i, (st_data_t)ary);
1904 }
1905 return ary;
1906}
1907
1908static int
1909rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
1910{
1911 VALUE *p = (VALUE *)arg;
1912 VALUE aliases = p[0], ary = p[1];
1913 int idx = (int)orig;
1914 VALUE key, str = rb_ary_entry(ary, idx);
1915
1916 if (NIL_P(str)) {
1917 rb_encoding *enc = rb_enc_from_index(idx);
1918
1919 if (!enc) return ST_CONTINUE;
1920 if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
1921 return ST_CONTINUE;
1922 }
1923 str = rb_fstring_cstr(rb_enc_name(enc));
1924 rb_ary_store(ary, idx, str);
1925 }
1926 key = rb_interned_str_cstr((char *)name);
1927 rb_hash_aset(aliases, key, str);
1928 return ST_CONTINUE;
1929}
1930
1931/*
1932 * call-seq:
1933 * Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...}
1934 *
1935 * Returns the hash of available encoding alias and original encoding name.
1936 *
1937 * Encoding.aliases
1938 * #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII",
1939 * "SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
1940 *
1941 */
1942
1943static VALUE
1944rb_enc_aliases(VALUE klass)
1945{
1946 VALUE aliases[2];
1947 aliases[0] = rb_hash_new();
1948 aliases[1] = rb_ary_new();
1949
1950 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
1951 st_foreach(enc_table->names, rb_enc_aliases_enc_i, (st_data_t)aliases);
1952 }
1953
1954 return aliases[0];
1955}
1956
1957/*
1958 * An \Encoding instance represents a character encoding usable in Ruby.
1959 * It is defined as a constant under the \Encoding namespace.
1960 * It has a name and, optionally, aliases:
1961 *
1962 * Encoding::US_ASCII.name # => "US-ASCII"
1963 * Encoding::US_ASCII.names # => ["US-ASCII", "ASCII", "ANSI_X3.4-1968", "646"]
1964 *
1965 * A Ruby method that accepts an encoding as an argument will accept:
1966 *
1967 * - An \Encoding object.
1968 * - The name of an encoding.
1969 * - An alias for an encoding name.
1970 *
1971 * These are equivalent:
1972 *
1973 * 'foo'.encode(Encoding::US_ASCII) # Encoding object.
1974 * 'foo'.encode('US-ASCII') # Encoding name.
1975 * 'foo'.encode('ASCII') # Encoding alias.
1976 *
1977 * For a full discussion of encodings and their uses,
1978 * see {the Encodings document}[rdoc-ref:encodings.rdoc].
1979 *
1980 * Encoding::ASCII_8BIT is a special-purpose encoding that is usually used for
1981 * a string of bytes, not a string of characters.
1982 * But as the name indicates, its characters in the ASCII range
1983 * are considered as ASCII characters.
1984 * This is useful when you use other ASCII-compatible encodings.
1985 *
1986 */
1987
1988void
1989Init_Encoding(void)
1990{
1991 VALUE list;
1992 int i;
1993
1994 id_i_name = rb_intern_const("@name");
1996 rb_define_alloc_func(rb_cEncoding, enc_s_alloc);
1998
1999 /* The name of the encoding.
2000 *
2001 * Encoding::UTF_8.name #=> "UTF-8"
2002 */
2003 rb_attr(rb_cEncoding, rb_intern("name"), TRUE, FALSE, Qfalse);
2004 rb_define_alias(rb_cEncoding, "to_s", "name");
2005
2006 rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
2007 rb_define_method(rb_cEncoding, "names", enc_names, 0);
2008 rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
2009 rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0);
2010 rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
2011 rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0);
2012 rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0);
2013 rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
2014 rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
2015
2016 rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
2017 rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
2018
2019 rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
2020 rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
2021 rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0);
2022 rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
2023 rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); /* in localeinit.c */
2024
2025 struct enc_table *enc_table = &global_enc_table;
2026
2027 rb_gc_register_address(&rb_encoding_list);
2028 list = rb_encoding_list = rb_ary_new2(ENCODING_LIST_CAPA);
2029 RBASIC_CLEAR_CLASS(list);
2030
2031 for (i = 0; i < enc_table->count; ++i) {
2032 rb_ary_push(list, enc_new(enc_table->list[i].enc));
2033 }
2034
2035 rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);
2036}
2037
2038void
2039Init_unicode_version(void)
2040{
2041 extern const char onigenc_unicode_version_string[];
2042
2043 VALUE str = rb_usascii_str_new_static(onigenc_unicode_version_string,
2044 strlen(onigenc_unicode_version_string));
2045 OBJ_FREEZE(str);
2046 /* The supported Unicode version. */
2047 rb_define_const(rb_cEncoding, "UNICODE_VERSION", str);
2048}
2049
2050void
2051Init_encodings(void)
2052{
2053 rb_enc_init(&global_enc_table);
2054}
2055
2056/* locale insensitive ctype functions */
2057
2058void
2059rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
2060{
2061 GLOBAL_ENC_TABLE_LOCKING(enc_table) {
2062 st_foreach(enc_table->names, func, arg);
2063 }
2064}
#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:1210
@ 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:1404
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2779
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2589
#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:1430
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1437
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:467
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
Definition error.c:3877
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1436
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:498
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:235
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:974
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:12788
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2976
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:1201
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:2057
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:1506
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:2419
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:1024
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:138
#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:122
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:543
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:224
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