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