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