22 #define ENC_ASSERT (!ENC_DEBUG)?(void)0:assert 23 #define MUST_STRING(str) (ENC_ASSERT(RB_TYPE_P(str, T_STRING)), str) 25 #undef rb_ascii8bit_encindex 26 #undef rb_utf8_encindex 27 #undef rb_usascii_encindex 31 #if defined __GNUC__ && __GNUC__ >= 4 32 #pragma GCC visibility push(default) 41 #pragma GCC visibility pop 44 static ID id_encoding;
46 static VALUE rb_encoding_list;
61 #define ENC_DUMMY_FLAG (1<<24) 62 #define ENC_INDEX_MASK (~(~0U<<24)) 64 #define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK) 65 #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG) 66 #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG) 70 #define ENCODING_COUNT ENCINDEX_BUILTIN_MAX 71 #define UNSPECIFIED_ENCODING INT_MAX 73 #define ENCODING_NAMELEN_MAX 63 74 #define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX) 76 #define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc)) 78 static int load_encoding(
const char *
name);
86 #define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type) 87 #define is_obj_encoding(obj) (RB_TYPE_P((obj), T_DATA) && is_data_encoding(obj)) 102 rb_enc_from_encoding_index(
int idx)
106 if (!(list = rb_encoding_list)) {
107 rb_bug(
"rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
111 rb_bug(
"rb_enc_from_encoding_index(%d): not created yet", idx);
120 if (!encoding)
return Qnil;
122 return rb_enc_from_encoding_index(idx);
146 index = enc_autoload(enc);
152 enc_check_encoding(
VALUE obj)
157 return check_encoding(
RDATA(obj)->data);
162 not_encoding(
VALUE enc)
169 must_encoding(
VALUE enc)
171 int index = enc_check_encoding(enc);
179 must_encindex(
int index)
202 idx = enc_check_encoding(enc);
217 str_find_encindex(
VALUE enc)
230 str_to_encindex(
VALUE enc)
232 int idx = str_find_encindex(enc);
240 str_to_encoding(
VALUE enc)
248 if (enc_check_encoding(enc) >= 0)
return RDATA(enc)->data;
249 return str_to_encoding(enc);
256 if (enc_check_encoding(enc) >= 0)
return RDATA(enc)->data;
257 idx = str_find_encindex(enc);
258 if (idx < 0)
return NULL;
268 enc_table_expand(
int newsize)
273 if (enc_table.size >= newsize)
return newsize;
274 newsize = (newsize + 7) / 8 * 8;
275 ent =
realloc(enc_table.list,
sizeof(*enc_table.list) * newsize);
277 memset(ent + enc_table.size, 0,
sizeof(*ent)*(newsize - enc_table.size));
278 enc_table.list = ent;
279 enc_table.size = newsize;
284 enc_register_at(
int index,
const char *
name,
rb_encoding *base_encoding)
302 *encoding = *base_encoding;
305 memset(encoding, 0,
sizeof(*ent->
enc));
311 list = rb_encoding_list;
322 int index = enc_table.count;
324 if ((index = enc_table_expand(index + 1)) < 0)
return -1;
325 enc_table.count = index;
326 return enc_register_at(index - 1,
name, encoding);
329 static void set_encoding_const(
const char *,
rb_encoding *);
340 index = enc_register(name, encoding);
343 enc_register_at(index, name, encoding);
350 index = enc_register(name, encoding);
361 idx = enc_register(name, 0);
367 enc_check_duplication(
const char *
name)
379 enc_table.list[index].base =
base;
413 enc_check_duplication(name);
414 idx = enc_register(name, encoding);
415 set_base_encoding(idx, encoding);
432 return rb_enc_from_encoding_index(
438 enc_replicate_with_index(
const char *
name,
rb_encoding *origenc,
int idx)
441 idx = enc_register(
name, origenc);
444 idx = enc_register_at(idx,
name, origenc);
447 set_base_encoding(idx, origenc);
460 origidx = enc_register(orig, 0);
500 enc_dummy_p(
VALUE enc)
516 enc_ascii_compatible_p(
VALUE enc)
541 enc_alias_internal(
const char *
alias,
int idx)
548 enc_alias(
const char *
alias,
int idx)
551 if (!enc_alias_internal(
alias, idx))
561 enc_check_duplication(alias);
562 if (!enc_table.list) {
568 return enc_alias(alias, idx);
577 idx = enc_register(orig, 0);
579 return enc_alias(alias, idx);
592 if (!enc_table.names) {
595 #define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc) 600 #define ENCDB_REGISTER(name, enc) enc_register_at(ENCINDEX_##enc, name, NULL) 611 #undef ENCDB_REGISTER 618 if (!enc_table.list) {
624 return enc_table.list[index].enc;
630 return must_encindex(index);
638 if (!name)
return -1;
639 if (!enc_table.list)
return -1;
647 load_encoding(
const char *
name)
671 if (loaded < 0 || 1 < loaded)
return -1;
686 if (i >= enc_table.count)
return -1;
687 }
while (enc_table.list[i].enc != base && (++i, 1));
689 if (enc_autoload(base) < 0)
return -1;
710 i = load_encoding(name);
718 if (enc_autoload(enc) < 0) {
719 rb_warn(
"failed to load encoding (%s); use ASCII-8BIT instead",
731 if (idx < 0) idx = 0;
736 enc_capable(
VALUE obj)
760 enc_get_index_str(
VALUE str)
787 i = enc_get_index_str(obj);
793 if (
NIL_P(obj))
break;
796 i = enc_check_encoding(obj);
807 enc_set_index(
VALUE obj,
int idx)
822 enc_set_index(obj, idx);
829 int oldidx, oldtermlen, termlen;
839 enc = must_encindex(idx);
849 enc_set_index(obj, idx);
890 enc_compatible_latter(
VALUE str1,
VALUE str2,
int idx1,
int idx2)
947 int idx1 = enc_get_index_str(str1);
948 int idx2 = enc_get_index_str(str2);
950 if (idx1 < 0 || idx2 < 0)
957 return enc_compatible_latter(str1, str2, idx1, idx2);
967 if (idx1 < 0 || idx2 < 0)
974 return enc_compatible_latter(str1, str2, idx1, idx2);
1015 return min <= e-p ? min : (int)(e-p);
1039 c = (
unsigned char)*p;
1069 #undef rb_enc_codepoint 1086 #undef rb_enc_code_to_mbclen 1115 enc_inspect(
VALUE self)
1142 enc_name(
VALUE self)
1152 if ((
int)idx == (int)arg[0]) {
1169 enc_names(
VALUE self)
1198 enc_list(
VALUE klass)
1233 idx = str_to_encindex(enc);
1235 return rb_enc_from_encoding_index(idx);
1267 if (!enc_capable(str1))
return Qnil;
1268 if (!enc_capable(str2))
return Qnil;
1270 if (!enc)
return Qnil;
1276 enc_s_alloc(
VALUE klass)
1278 rb_undefined_alloc(klass);
1287 return enc_name(
self);
1301 return enc_find(klass, str);
1307 if (!enc_table.list) {
1322 if (!enc_table.list) {
1337 if (!enc_table.list) {
1363 enc_alias_internal(
"locale", idx);
1400 int overridden =
FALSE;
1402 if (def->
index != -2)
1406 if (
NIL_P(encoding)) {
1415 enc_alias_internal(name, def->
index);
1418 if (def == &default_external)
1427 if (default_external.
enc)
return default_external.
enc;
1429 if (default_external.
index >= 0) {
1431 return default_external.
enc;
1471 get_default_external(
VALUE klass)
1479 if (
NIL_P(encoding)) {
1482 enc_set_default_encoding(&default_external, encoding,
1500 set_default_external(
VALUE klass,
VALUE encoding)
1502 rb_warning(
"setting Encoding.default_external");
1512 if (!default_internal.
enc && default_internal.
index >= 0) {
1515 return default_internal.
enc;
1555 get_default_internal(
VALUE klass)
1563 enc_set_default_encoding(&default_internal, encoding,
1581 set_default_internal(
VALUE klass,
VALUE encoding)
1583 rb_warning(
"setting Encoding.default_internal");
1592 char *s = (
char *)
name;
1593 int haslower = 0, hasupper = 0, valid = 0;
1598 while (*++s && (
ISALNUM(*s) || *s ==
'_')) {
1599 if (
ISLOWER(*s)) haslower = 1;
1607 if (!valid || haslower) {
1610 if (!haslower || !hasupper) {
1612 if (
ISLOWER(*s)) haslower = 1;
1613 if (
ISUPPER(*s)) hasupper = 1;
1614 }
while (*++s && (!haslower || !hasupper));
1631 for (s = (
char *)name; *s; ++s) {
1663 rb_enc_name_list(
VALUE klass)
1674 VALUE aliases = p[0], ary = p[1];
1675 int idx = (int)orig;
1707 rb_enc_aliases(
VALUE klass)
1920 #define rb_intern(str) rb_intern_const(str) 1951 rb_encoding_list =
list;
1954 for (i = 0; i < enc_table.count; ++i) {
1955 rb_ary_push(list, enc_new(enc_table.list[i].enc));
#define RBASIC_CLEAR_CLASS(obj)
#define ENCINDEX_US_ASCII
int rb_enc_codelen(int c, rb_encoding *enc)
int rb_enc_get_index(VALUE obj)
#define ONIGENC_CODE_TO_MBCLEN(enc, code)
#define MBCLEN_CHARFOUND_P(ret)
#define is_obj_encoding(obj)
void rb_warn(const char *fmt,...)
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
void rb_bug(const char *fmt,...)
VALUE rb_ary_entry(VALUE ary, long offset)
#define MBCLEN_CHARFOUND_LEN(ret)
#define rb_enc_mbc_to_codepoint(p, e, enc)
void rb_enc_copy(VALUE obj1, VALUE obj2)
#define RUBY_TYPED_FREE_IMMEDIATELY
void rb_enc_set_base(const char *name, const char *orig)
size_t strlen(const char *)
#define ONIGENC_IS_UNICODE(enc)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
#define rb_usascii_str_new2
VALUE rb_fstring_cstr(const char *str)
void rb_raise(VALUE exc, const char *fmt,...)
#define TypedData_Wrap_Struct(klass, data_type, sval)
rb_encoding * rb_to_encoding(VALUE enc)
int rb_enc_dummy_p(rb_encoding *enc)
#define ENC_CODERANGE_CLEAR(obj)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
int rb_enc_tolower(int c, rb_encoding *enc)
rb_encoding * rb_default_internal_encoding(void)
VALUE rb_ary_push(VALUE ary, VALUE item)
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
int rb_usascii_encindex(void)
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
VALUE rb_locale_charmap(VALUE klass)
#define ENC_SET_DUMMY(enc)
#define ENCODING_GET_INLINED(obj)
int rb_enc_str_coderange(VALUE)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
VALUE rb_ivar_get(VALUE, ID)
void rb_enc_set_default_external(VALUE encoding)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
int rb_enc_set_dummy(int index)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
#define ONIGENC_MBC_ENC_LEN(enc, p, e)
#define ENC_REGISTER(enc)
int rb_enc_registered(const char *name)
void Init_w32_codepage(void)
int rb_filesystem_encindex(void)
rb_encoding * rb_utf8_encoding(void)
void rb_undef_method(VALUE klass, const char *name)
#define ENC_CODERANGE_7BIT
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
int rb_enc_toupper(int c, rb_encoding *enc)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
int st_insert2(st_table *, st_data_t, st_data_t, st_data_t(*)(st_data_t))
st_table * st_init_strcasetable(void)
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
#define RB_TYPE_P(obj, type)
void rb_encdb_set_unicode(int index)
#define ENCINDEX_BUILTIN_MAX
int rb_to_encoding_index(VALUE enc)
rb_encoding * rb_default_external_encoding(void)
int rb_enc_to_index(rb_encoding *enc)
int rb_data_is_encoding(VALUE obj)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
int rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
RUBY_EXTERN VALUE rb_cObject
#define rb_enc_isascii(c, enc)
VALUE rb_ary_replace(VALUE copy, VALUE orig)
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
int rb_ascii8bit_encindex(void)
VALUE rb_enc_default_external(void)
int rb_require_internal(VALUE fname, int safe)
#define ENC_CODERANGE_ASCIIONLY(obj)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_enc_set_index(VALUE obj, int idx)
int rb_enc_replicate(const char *name, rb_encoding *encoding)
void rb_define_const(VALUE, const char *, VALUE)
void rb_ary_store(VALUE ary, long idx, VALUE val)
int rb_encdb_alias(const char *alias, const char *orig)
int rb_locale_encindex(void)
#define ALLOCA_N(type, n)
NORETURN(static void not_encoding(VALUE enc))
int rb_locale_charmap_index(void)
void rb_gc_register_mark_object(VALUE obj)
#define MEMCPY(p1, p2, type, n)
#define ENCDB_REGISTER(name, enc)
VALUE rb_enc_associate_index(VALUE obj, int idx)
#define rb_enc_mbminlen(enc)
rb_encoding * rb_find_encoding(VALUE enc)
int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
int rb_encdb_dummy(const char *name)
VALUE rb_sprintf(const char *format,...)
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
int rb_enc_unicode_p(rb_encoding *enc)
#define ONIGENC_ASCII_CODE_TO_UPPER_CASE(c)
VALUE rb_ivar_set(VALUE, ID, VALUE)
#define ONIGENC_FLAG_UNICODE
rb_encoding * rb_usascii_encoding(void)
int rb_encdb_replicate(const char *name, const char *orig)
#define ENCODING_NAMELEN_MAX
int rb_define_dummy_encoding(const char *name)
rb_encoding * rb_locale_encoding(void)
#define ONIGENC_IS_ASCII_CODE(code)
int rb_utf8_encindex(void)
#define ENCODING_SET_INLINED(obj, i)
int rb_enc_code_to_mbclen(int code, rb_encoding *enc)
VALUE rb_obj_encoding(VALUE obj)
int Init_enc_set_filesystem_encoding(void)
#define rb_enc_asciicompat(enc)
void rb_enc_foreach_name(int(*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
#define enc_autoload_p(enc)
register unsigned int len
#define StringValueCStr(v)
rb_encoding * rb_enc_get(VALUE obj)
#define ENCODING_INLINE_MAX
VALUE rb_check_string_type(VALUE)
void rb_enc_set_default_internal(VALUE encoding)
#define valid_encoding_name_p(name)
OnigEncodingType rb_raw_encoding
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
void rb_warning(const char *fmt,...)
int rb_enc_alias(const char *alias, const char *orig)
#define is_data_encoding(obj)
struct rb_encoding_entry * list
#define ENC_TO_ENCINDEX(enc)
rb_encoding * rb_filesystem_encoding(void)
rb_encoding * rb_enc_get_from_index(int index)
VALUE rb_errinfo(void)
The current exception in the current thread.
VALUE rb_enc_default_internal(void)
unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
#define STRCASECMP(s1, s2)
rb_encoding * rb_ascii8bit_encoding(void)
int rb_enc_find_index(const char *name)
rb_encoding * rb_enc_check_str(VALUE str1, VALUE str2)
#define rb_check_frozen(obj)
#define CONST_ID(var, str)
int rb_enc_register(const char *name, rb_encoding *encoding)
#define SPECIAL_CONST_P(x)
void rb_encdb_declare(const char *name)
int rb_enc_str_asciionly_p(VALUE)
void rb_gc_mark_encodings(void)
#define UNSPECIFIED_ENCODING
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
#define ONIGENC_PRECISE_MBC_ENC_LEN(enc, p, e)
void rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int termlen)
void rb_loaderror(const char *fmt,...)
rb_encoding * rb_enc_find(const char *name)
#define ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n)
#define ONIGENC_ASCII_CODE_TO_LOWER_CASE(c)
rb_encoding * rb_enc_from_index(int index)