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