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