diff options
Diffstat (limited to 'encoding.c')
| -rw-r--r-- | encoding.c | 1791 |
1 files changed, 1142 insertions, 649 deletions
diff --git a/encoding.c b/encoding.c index 22a03d0ae4..8bb393b471 100644 --- a/encoding.c +++ b/encoding.c @@ -9,38 +9,109 @@ **********************************************************************/ -#include "ruby/ruby.h" -#include "ruby/encoding.h" -#include "regenc.h" +#include "ruby/internal/config.h" + #include <ctype.h> -#ifndef NO_LOCALE_CHARMAP -#ifdef __CYGWIN__ -#include <windows.h> -#endif -#ifdef HAVE_LANGINFO_H -#include <langinfo.h> + +#include "encindex.h" +#include "internal.h" +#include "internal/enc.h" +#include "internal/encoding.h" +#include "internal/error.h" +#include "internal/inits.h" +#include "internal/load.h" +#include "internal/object.h" +#include "internal/string.h" +#include "internal/vm.h" +#include "regenc.h" +#include "ruby/atomic.h" +#include "ruby/encoding.h" +#include "ruby/util.h" +#include "ruby/ractor.h" +#include "ruby_assert.h" +#include "vm_sync.h" +#include "ruby_atomic.h" + +#ifndef ENC_DEBUG +#define ENC_DEBUG 0 #endif +#define ENC_ASSERT(expr) RUBY_ASSERT_WHEN(ENC_DEBUG, expr) +#define MUST_STRING(str) (ENC_ASSERT(RB_TYPE_P(str, T_STRING)), str) + +#undef rb_ascii8bit_encindex +#undef rb_utf8_encindex +#undef rb_usascii_encindex + +typedef OnigEncodingType rb_raw_encoding; + +#if defined __GNUC__ && __GNUC__ >= 4 +#pragma GCC visibility push(default) +int rb_enc_register(const char *name, rb_encoding *encoding); +void rb_enc_set_base(const char *name, const char *orig); +int rb_enc_set_dummy(int index); +void rb_encdb_declare(const char *name); +int rb_encdb_replicate(const char *name, const char *orig); +int rb_encdb_dummy(const char *name); +int rb_encdb_alias(const char *alias, const char *orig); +#pragma GCC visibility pop #endif -#include "ruby/util.h" -static ID id_encoding; +static ID id_encoding, id_i_name; VALUE rb_cEncoding; + +#define ENCODING_LIST_CAPA 256 static VALUE rb_encoding_list; struct rb_encoding_entry { + rb_atomic_t loaded; const char *name; rb_encoding *enc; rb_encoding *base; }; -static struct { - struct rb_encoding_entry *list; +static struct enc_table { + struct rb_encoding_entry list[ENCODING_LIST_CAPA]; int count; - int size; st_table *names; -} enc_table; +} global_enc_table; + +static int +enc_names_free_i(st_data_t name, st_data_t idx, st_data_t args) +{ + ruby_xfree((void *)name); + return ST_DELETE; +} + +void +rb_free_global_enc_table(void) +{ + for (size_t i = 0; i < ENCODING_LIST_CAPA; i++) { + xfree((void *)global_enc_table.list[i].enc); + } + + st_foreach(global_enc_table.names, enc_names_free_i, (st_data_t)0); + st_free_table(global_enc_table.names); +} -void rb_enc_init(void); +static rb_encoding *global_enc_ascii, + *global_enc_utf_8, + *global_enc_us_ascii; + +static int filesystem_encindex = ENCINDEX_ASCII_8BIT; + +#define GLOBAL_ENC_TABLE_LOCKING(tbl) \ + for (struct enc_table *tbl = &global_enc_table, **locking = &tbl; \ + locking; \ + locking = NULL) \ + RB_VM_LOCKING() + + +#define ENC_DUMMY_FLAG (1<<24) +#define ENC_INDEX_MASK (~(~0U<<24)) + +#define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK) +#define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG) +#define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG) #define ENCODING_COUNT ENCINDEX_BUILTIN_MAX #define UNSPECIFIED_ENCODING INT_MAX @@ -48,41 +119,72 @@ void rb_enc_init(void); #define ENCODING_NAMELEN_MAX 63 #define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX) -#define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc)) +static const rb_data_type_t encoding_data_type = { + "encoding", + {0, 0, 0,}, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED +}; -static int load_encoding(const char *name); +#define is_encoding_type(obj) (RTYPEDDATA_TYPE(obj) == &encoding_data_type) +#define is_data_encoding(obj) (rbimpl_rtypeddata_p(obj) && is_encoding_type(obj)) +#define is_obj_encoding(obj) (rbimpl_obj_typeddata_p(obj) && is_encoding_type(obj)) -static size_t -enc_memsize(const void *p) +int +rb_data_is_encoding(VALUE obj) { - return 0; + return is_data_encoding(obj); } -static const rb_data_type_t encoding_data_type = { - "encoding", 0, 0, enc_memsize, -}; - -#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type) - static VALUE enc_new(rb_encoding *encoding) { - return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, encoding); + VALUE enc = TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, (void *)encoding); + rb_ivar_set(enc, id_i_name, rb_fstring_cstr(encoding->name)); + RB_OBJ_SET_FROZEN_SHAREABLE(enc); + return enc; +} + +static void +enc_list_update(int index, rb_raw_encoding *encoding) +{ + RUBY_ASSERT(index < ENCODING_LIST_CAPA); + + VALUE list = RUBY_ATOMIC_VALUE_LOAD(rb_encoding_list); + + if (list && NIL_P(rb_ary_entry(list, index))) { + VALUE new_list = rb_ary_dup(list); + RBASIC_CLEAR_CLASS(new_list); + /* initialize encoding data */ + rb_ary_store(new_list, index, enc_new(encoding)); + rb_ary_freeze(new_list); + FL_SET_RAW(new_list, RUBY_FL_SHAREABLE); + RUBY_ATOMIC_VALUE_SET(rb_encoding_list, new_list); + } } static VALUE -rb_enc_from_encoding_index(int idx) +enc_list_lookup(int idx) { - VALUE list, enc; + VALUE list, enc = Qnil; - if (!(list = rb_encoding_list)) { - rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx); + if (idx < ENCODING_LIST_CAPA) { + list = RUBY_ATOMIC_VALUE_LOAD(rb_encoding_list); + RUBY_ASSERT(list); + enc = rb_ary_entry(list, idx); } - enc = rb_ary_entry(list, idx); + if (NIL_P(enc)) { - rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx); + rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx); } - return enc; + else { + return enc; + } +} + +static VALUE +rb_enc_from_encoding_index(int idx) +{ + return enc_list_lookup(idx); } VALUE @@ -94,16 +196,26 @@ rb_enc_from_encoding(rb_encoding *encoding) return rb_enc_from_encoding_index(idx); } -static int enc_autoload(rb_encoding *); +int +rb_enc_to_index(rb_encoding *enc) +{ + return enc ? ENC_TO_ENCINDEX(enc) : 0; +} + +int +rb_enc_dummy_p(rb_encoding *enc) +{ + return ENC_DUMMY_P(enc) != 0; +} static int check_encoding(rb_encoding *enc) { int index = rb_enc_to_index(enc); if (rb_enc_from_index(index) != enc) - return -1; - if (enc_autoload_p(enc)) { - index = enc_autoload(enc); + return -1; + if (rb_enc_autoload_p(enc)) { + index = rb_enc_autoload(enc); } return index; } @@ -111,181 +223,322 @@ check_encoding(rb_encoding *enc) static int enc_check_encoding(VALUE obj) { - if (SPECIAL_CONST_P(obj) || !rb_typeddata_is_kind_of(obj, &encoding_data_type)) { - return -1; + if (!is_obj_encoding(obj)) { + return -1; } - return check_encoding(RDATA(obj)->data); + return check_encoding(RTYPEDDATA_GET_DATA(obj)); } -static int +NORETURN(static void not_encoding(VALUE enc)); +static void +not_encoding(VALUE enc) +{ + rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Encoding)", + rb_obj_class(enc)); +} + +static rb_encoding * must_encoding(VALUE enc) { int index = enc_check_encoding(enc); if (index < 0) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)", - rb_obj_classname(enc)); + not_encoding(enc); } - return index; + return RTYPEDDATA_GET_DATA(enc); +} + +static rb_encoding * +must_encindex(int index) +{ + rb_encoding *enc = rb_enc_from_index(index); + if (!enc) { + rb_raise(rb_eEncodingError, "encoding index out of bound: %d", + index); + } + if (rb_enc_autoload_p(enc) && rb_enc_autoload(enc) == -1) { + rb_loaderror("failed to load encoding (%s)", + rb_enc_name(enc)); + } + if (ENC_TO_ENCINDEX(enc) != (int)(index & ENC_INDEX_MASK)) { + rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)", + index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc)); + } + return enc; } int rb_to_encoding_index(VALUE enc) { + ASSERT_vm_unlocking(); // can load encoding, so must not hold VM lock int idx; + const char *name; idx = enc_check_encoding(enc); if (idx >= 0) { - return idx; + return idx; } else if (NIL_P(enc = rb_check_string_type(enc))) { - return -1; + return -1; } if (!rb_enc_asciicompat(rb_enc_get(enc))) { - return -1; + return -1; } - return rb_enc_find_index(StringValueCStr(enc)); + if (!(name = rb_str_to_cstr(enc))) { + return -1; + } + return rb_enc_find_index(name); } -static rb_encoding * -to_encoding(VALUE enc) +static const char * +name_for_encoding(volatile VALUE *enc) { - int idx; + VALUE name = StringValue(*enc); + const char *n; - StringValue(enc); - if (!rb_enc_asciicompat(rb_enc_get(enc))) { - rb_raise(rb_eArgError, "invalid name encoding (non ASCII)"); + if (!rb_enc_asciicompat(rb_enc_get(name))) { + rb_raise(rb_eArgError, "invalid encoding name (non ASCII)"); + } + if (!(n = rb_str_to_cstr(name))) { + rb_raise(rb_eArgError, "invalid encoding name (NUL byte)"); } - idx = rb_enc_find_index(StringValueCStr(enc)); + return n; +} + +/* Returns encoding index or UNSPECIFIED_ENCODING */ +static int +str_find_encindex(VALUE enc) +{ + int idx = rb_enc_find_index(name_for_encoding(&enc)); + RB_GC_GUARD(enc); + return idx; +} + +static int +str_to_encindex(VALUE enc) +{ + int idx = str_find_encindex(enc); if (idx < 0) { - rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); + rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc); } - return rb_enc_from_index(idx); + return idx; +} + +static rb_encoding * +str_to_encoding(VALUE enc) +{ + return rb_enc_from_index(str_to_encindex(enc)); } rb_encoding * rb_to_encoding(VALUE enc) { - if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data; - return to_encoding(enc); + if (enc_check_encoding(enc) >= 0) return RTYPEDDATA_GET_DATA(enc); + return str_to_encoding(enc); } -void -rb_gc_mark_encodings(void) +rb_encoding * +rb_find_encoding(VALUE enc) { + int idx; + if (enc_check_encoding(enc) >= 0) return RTYPEDDATA_GET_DATA(enc); + idx = str_find_encindex(enc); + if (idx < 0) return NULL; + return rb_enc_from_index(idx); } static int -enc_table_expand(int newsize) +enc_table_expand(struct enc_table *enc_table, int newsize) +{ + if (newsize > ENCODING_LIST_CAPA) { + rb_raise(rb_eEncodingError, "too many encoding (> %d)", ENCODING_LIST_CAPA); + } + return newsize; +} + +/* Load an encoding using the values from base_encoding */ +static void +enc_load_from_base(struct enc_table *enc_table, int index, rb_encoding *base_encoding) { - struct rb_encoding_entry *ent; - int count = newsize; + ASSERT_vm_locking(); + + struct rb_encoding_entry *ent = &enc_table->list[index]; + + if (ent->loaded) { + return; + } - if (enc_table.size >= newsize) return newsize; - newsize = (newsize + 7) / 8 * 8; - ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize); - if (!ent) return -1; - memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size)); - enc_table.list = ent; - enc_table.size = newsize; - return count; + rb_raw_encoding *encoding = (rb_raw_encoding *)ent->enc; + RUBY_ASSERT(encoding); + + // FIXME: Before the base is loaded, the encoding may be accessed + // concurrently by other Ractors. + // We're copying all fields from base_encoding except name and + // ruby_encoding_index which we preserve from the original. Since these are + // the only fields other threads should read it is likely safe despite + // technically being a data race. + rb_raw_encoding tmp_encoding = *base_encoding; + tmp_encoding.name = encoding->name; + tmp_encoding.ruby_encoding_index = encoding->ruby_encoding_index; + *encoding = tmp_encoding; + + RUBY_ATOMIC_SET(ent->loaded, encoding->max_enc_len); } static int -enc_register_at(int index, const char *name, rb_encoding *encoding) +enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding) { - struct rb_encoding_entry *ent = &enc_table.list[index]; - VALUE list; + ASSERT_vm_locking(); - if (!valid_encoding_name_p(name)) return -1; - if (!ent->name) { - ent->name = name = strdup(name); - } - else if (STRCASECMP(name, ent->name)) { - return -1; - } - if (!ent->enc) { - ent->enc = xmalloc(sizeof(rb_encoding)); + struct rb_encoding_entry *ent = &enc_table->list[index]; + rb_raw_encoding *encoding; + + RUBY_ASSERT(!ent->loaded); + RUBY_ASSERT(!ent->name); + RUBY_ASSERT(!ent->enc); + RUBY_ASSERT(!ent->base); + + RUBY_ASSERT(valid_encoding_name_p(name)); + + ent->name = name = strdup(name); + + encoding = ZALLOC(rb_raw_encoding); + encoding->name = name; + encoding->ruby_encoding_index = index; + ent->enc = encoding; + + if (st_insert(enc_table->names, (st_data_t)name, (st_data_t)index)) { + rb_bug("encoding name was somehow registered twice"); } - if (encoding) { - *ent->enc = *encoding; + + enc_list_update(index, encoding); + + if (base_encoding) { + enc_load_from_base(enc_table, index, base_encoding); } else { - memset(ent->enc, 0, sizeof(*ent->enc)); - } - encoding = ent->enc; - encoding->name = name; - encoding->ruby_encoding_index = index; - st_insert(enc_table.names, (st_data_t)name, (st_data_t)index); - list = rb_encoding_list; - if (list && NIL_P(rb_ary_entry(list, index))) { - /* initialize encoding data */ - rb_ary_store(list, index, enc_new(encoding)); + /* it should not be loaded yet */ + RUBY_ASSERT(!encoding->max_enc_len); } + return index; } static int -enc_register(const char *name, rb_encoding *encoding) +enc_register(struct enc_table *enc_table, const char *name, rb_encoding *encoding) { - int index = enc_table.count; + ASSERT_vm_locking(); - if ((index = enc_table_expand(index + 1)) < 0) return -1; - enc_table.count = index; - return enc_register_at(index - 1, name, encoding); + if (!valid_encoding_name_p(name)) return -1; + + int index = enc_table->count; + + enc_table->count = enc_table_expand(enc_table, index + 1); + return enc_register_at(enc_table, index, name, encoding); } static void set_encoding_const(const char *, rb_encoding *); -int rb_enc_registered(const char *name); +static int enc_registered(struct enc_table *enc_table, const char *name); + +static rb_encoding * +enc_from_index(struct enc_table *enc_table, int index) +{ + if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) { + return 0; + } + rb_encoding *enc = enc_table->list[index].enc; + RUBY_ASSERT(ENC_TO_ENCINDEX(enc) == index); + return enc; +} + +rb_encoding * +rb_enc_from_index(int index) +{ + return enc_from_index(&global_enc_table, index); +} int rb_enc_register(const char *name, rb_encoding *encoding) { - int index = rb_enc_registered(name); + int index; + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + index = enc_registered(enc_table, name); + + if (index >= 0) { + rb_encoding *oldenc = enc_from_index(enc_table, index); + if (STRCASECMP(name, rb_enc_name(oldenc))) { + index = enc_register(enc_table, name, encoding); + } + else if (rb_enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) { + enc_load_from_base(enc_table, index, encoding); + } + else { + rb_raise(rb_eArgError, "encoding %s is already registered", name); + } + } + else { + index = enc_register(enc_table, name, encoding); + set_encoding_const(name, rb_enc_from_index(index)); + } + } + return index; +} + +int +enc_registered(struct enc_table *enc_table, const char *name) +{ + ASSERT_vm_locking(); + st_data_t idx = 0; - if (index >= 0) { - rb_encoding *oldenc = rb_enc_from_index(index); - if (STRCASECMP(name, rb_enc_name(oldenc))) { - index = enc_register(name, encoding); - } - else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) { - enc_register_at(index, name, encoding); - } - else { - rb_raise(rb_eArgError, "encoding %s is already registered", name); - } + if (!name) return -1; + if (!enc_table->names) return -1; + if (st_lookup(enc_table->names, (st_data_t)name, &idx)) { + return (int)idx; } - else { - index = enc_register(name, encoding); - set_encoding_const(name, rb_enc_from_index(index)); + return -1; +} + +int +rb_enc_registered(const char *name) +{ + int idx; + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + idx = enc_registered(enc_table, name); } - return index; + return idx; } void rb_encdb_declare(const char *name) { - int idx = rb_enc_registered(name); - if (idx < 0) { - idx = enc_register(name, 0); + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + int idx = enc_registered(enc_table, name); + if (idx < 0) { + idx = enc_register(enc_table, name, 0); + } + set_encoding_const(name, rb_enc_from_index(idx)); } - set_encoding_const(name, rb_enc_from_index(idx)); } static void -enc_check_duplication(const char *name) +enc_check_addable(struct enc_table *enc_table, const char *name) { - if (rb_enc_registered(name) >= 0) { - rb_raise(rb_eArgError, "encoding %s is already registered", name); + if (enc_registered(enc_table, name) >= 0) { + rb_raise(rb_eArgError, "encoding %s is already registered", name); + } + else if (!valid_encoding_name_p(name)) { + rb_raise(rb_eArgError, "invalid encoding name: %s", name); } } static rb_encoding* -set_base_encoding(int index, rb_encoding *base) +set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base) { - rb_encoding *enc = enc_table.list[index].enc; + rb_encoding *enc = enc_table->list[index].enc; - enc_table.list[index].base = base; - if (rb_enc_dummy_p(base)) ENC_SET_DUMMY(enc); + ASSUME(enc); + enc_table->list[index].base = base; + if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc); return enc; } @@ -296,52 +549,52 @@ set_base_encoding(int index, rb_encoding *base) void rb_enc_set_base(const char *name, const char *orig) { - int idx = rb_enc_registered(name); - int origidx = rb_enc_registered(orig); - set_base_encoding(idx, rb_enc_from_index(origidx)); + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + int idx = enc_registered(enc_table, name); + int origidx = enc_registered(enc_table, orig); + set_base_encoding(enc_table, idx, rb_enc_from_index(origidx)); + } } +/* for encdb.h + * Set encoding dummy. + */ int -rb_enc_replicate(const char *name, rb_encoding *encoding) +rb_enc_set_dummy(int index) +{ + rb_encoding *enc = global_enc_table.list[index].enc; + ENC_SET_DUMMY((rb_raw_encoding *)enc); + return index; +} + +static int +enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encoding) { int idx; - enc_check_duplication(name); - idx = enc_register(name, encoding); - set_base_encoding(idx, encoding); + enc_check_addable(enc_table, name); + idx = enc_register(enc_table, name, encoding); + if (idx < 0) rb_raise(rb_eArgError, "invalid encoding name: %s", name); + set_base_encoding(enc_table, idx, encoding); set_encoding_const(name, rb_enc_from_index(idx)); return idx; } -/* - * call-seq: - * enc.replicate(name) => encoding - * - * Returns a replicated encoding of _enc whose name is _name_. - * The new encoding should have the same byte structure of _enc_. - * If _name_ is used by another encoding, raise ArgumentError. - * - */ -static VALUE -enc_replicate(VALUE encoding, VALUE name) -{ - return rb_enc_from_encoding_index( - rb_enc_replicate(RSTRING_PTR(name), - rb_to_encoding(encoding))); -} - static int -enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx) +enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encoding *origenc, int idx) { if (idx < 0) { - idx = enc_register(name, origenc); + idx = enc_register(enc_table, name, origenc); } else { - idx = enc_register_at(idx, name, origenc); + enc_load_from_base(enc_table, idx, origenc); } if (idx >= 0) { - set_base_encoding(idx, origenc); - set_encoding_const(name, rb_enc_from_index(idx)); + set_base_encoding(enc_table, idx, origenc); + set_encoding_const(name, rb_enc_from_index(idx)); + } + else { + rb_raise(rb_eArgError, "failed to replicate encoding"); } return idx; } @@ -349,39 +602,54 @@ enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx) int rb_encdb_replicate(const char *name, const char *orig) { - int origidx = rb_enc_registered(orig); - int idx = rb_enc_registered(name); + int r; + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + int origidx = enc_registered(enc_table, orig); + int idx = enc_registered(enc_table, name); - if (origidx < 0) { - origidx = enc_register(orig, 0); + if (origidx < 0) { + origidx = enc_register(enc_table, orig, 0); + } + r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx); } - return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx); + + return r; } int rb_define_dummy_encoding(const char *name) { - int index = rb_enc_replicate(name, rb_ascii8bit_encoding()); - rb_encoding *enc = enc_table.list[index].enc; + int index; + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + index = enc_replicate(enc_table, name, rb_ascii8bit_encoding()); + rb_encoding *enc = enc_table->list[index].enc; + ENC_SET_DUMMY((rb_raw_encoding *)enc); + } - ENC_SET_DUMMY(enc); return index; } int rb_encdb_dummy(const char *name) { - int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(), - rb_enc_registered(name)); - rb_encoding *enc = enc_table.list[index].enc; + int index; + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + index = enc_replicate_with_index(enc_table, name, + rb_ascii8bit_encoding(), + enc_registered(enc_table, name)); + rb_encoding *enc = enc_table->list[index].enc; + ENC_SET_DUMMY((rb_raw_encoding *)enc); + } - ENC_SET_DUMMY(enc); return index; } /* * call-seq: - * enc.dummy? => true or false + * enc.dummy? -> true or false * * Returns true for dummy encodings. * A dummy encoding is an encoding for which character handling is not properly @@ -395,12 +663,12 @@ rb_encdb_dummy(const char *name) static VALUE enc_dummy_p(VALUE enc) { - return ENC_DUMMY_P(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse; + return RBOOL(ENC_DUMMY_P(must_encoding(enc))); } /* * call-seq: - * enc.ascii_compatible? => true or false + * enc.ascii_compatible? -> true or false * * Returns whether ASCII-compatible or not. * @@ -411,192 +679,253 @@ enc_dummy_p(VALUE enc) static VALUE enc_ascii_compatible_p(VALUE enc) { - return rb_enc_asciicompat(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse; + return RBOOL(rb_enc_asciicompat(must_encoding(enc))); } /* - * Returns 1 when the encoding is Unicode series other than UTF-7 else 0. + * Returns non-zero when the encoding is Unicode series other than UTF-7 else 0. */ int rb_enc_unicode_p(rb_encoding *enc) { - return rb_utf8_encoding()->is_code_ctype == enc->is_code_ctype; + return ONIGENC_IS_UNICODE(enc); } -static const char * -enc_alias_internal(const char *alias, int idx) +static st_data_t +enc_dup_name(st_data_t name) +{ + return (st_data_t)strdup((const char *)name); +} + +/* + * Returns copied alias name when the key is added for st_table, + * else returns NULL. + */ +static int +enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx) { - alias = strdup(alias); - st_insert(enc_table.names, (st_data_t)alias, (st_data_t)idx); - return alias; + ASSERT_vm_locking(); + return st_insert2(enc_table->names, (st_data_t)alias, (st_data_t)idx, + enc_dup_name); } static int -enc_alias(const char *alias, int idx) +enc_alias(struct enc_table *enc_table, const char *alias, int idx) { if (!valid_encoding_name_p(alias)) return -1; - alias = enc_alias_internal(alias, idx); - set_encoding_const(alias, rb_enc_from_index(idx)); + if (!enc_alias_internal(enc_table, alias, idx)) + set_encoding_const(alias, enc_from_index(enc_table, idx)); return idx; } int rb_enc_alias(const char *alias, const char *orig) { - int idx; - - enc_check_duplication(alias); - if (!enc_table.list) { - rb_enc_init(); + int idx, r; + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + enc_check_addable(enc_table, alias); // can raise } - if ((idx = rb_enc_find_index(orig)) < 0) { - return -1; + + idx = rb_enc_find_index(orig); + if (idx < 0) return -1; + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + r = enc_alias(enc_table, alias, idx); } - return enc_alias(alias, idx); + + return r; } int rb_encdb_alias(const char *alias, const char *orig) { - int idx = rb_enc_registered(orig); + int r; - if (idx < 0) { - idx = enc_register(orig, 0); - } - return enc_alias(alias, idx); -} + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + int idx = enc_registered(enc_table, orig); -enum { - ENCINDEX_ASCII, - ENCINDEX_UTF_8, - ENCINDEX_US_ASCII, - ENCINDEX_BUILTIN_MAX -}; + if (idx < 0) { + idx = enc_register(enc_table, orig, 0); + } + r = enc_alias(enc_table, alias, idx); + } -extern rb_encoding OnigEncodingUTF_8; -extern rb_encoding OnigEncodingUS_ASCII; + return r; +} -void -rb_enc_init(void) +static void +rb_enc_init(struct enc_table *enc_table) { - enc_table_expand(ENCODING_COUNT + 1); - if (!enc_table.names) { - enc_table.names = st_init_strcasetable(); + ASSERT_vm_locking(); + enc_table_expand(enc_table, ENCODING_COUNT + 1); + if (!enc_table->names) { + enc_table->names = st_init_strcasetable_with_size(ENCODING_LIST_CAPA); } -#define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc) - ENC_REGISTER(ASCII); +#define OnigEncodingASCII_8BIT OnigEncodingASCII +#define ENC_REGISTER(enc) enc_register_at(enc_table, ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc) + ENC_REGISTER(ASCII_8BIT); ENC_REGISTER(UTF_8); ENC_REGISTER(US_ASCII); + global_enc_ascii = enc_table->list[ENCINDEX_ASCII_8BIT].enc; + global_enc_utf_8 = enc_table->list[ENCINDEX_UTF_8].enc; + global_enc_us_ascii = enc_table->list[ENCINDEX_US_ASCII].enc; #undef ENC_REGISTER - enc_table.count = ENCINDEX_BUILTIN_MAX; +#undef OnigEncodingASCII_8BIT +#define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL) + ENCDB_REGISTER("UTF-16BE", UTF_16BE); + ENCDB_REGISTER("UTF-16LE", UTF_16LE); + ENCDB_REGISTER("UTF-32BE", UTF_32BE); + ENCDB_REGISTER("UTF-32LE", UTF_32LE); + ENCDB_REGISTER("UTF-16", UTF_16); + ENCDB_REGISTER("UTF-32", UTF_32); + ENCDB_REGISTER("UTF8-MAC", UTF8_MAC); + + ENCDB_REGISTER("EUC-JP", EUC_JP); + ENCDB_REGISTER("Windows-31J", Windows_31J); +#undef ENCDB_REGISTER + enc_table->count = ENCINDEX_BUILTIN_MAX; } rb_encoding * -rb_enc_from_index(int index) +rb_enc_get_from_index(int index) { - if (!enc_table.list) { - rb_enc_init(); - } - if (index < 0 || enc_table.count <= index) { - return 0; - } - return enc_table.list[index].enc; + return must_encindex(index); } -int -rb_enc_registered(const char *name) -{ - st_data_t idx = 0; - - if (!name) return -1; - if (!enc_table.list) return -1; - if (st_lookup(enc_table.names, (st_data_t)name, &idx)) { - return (int)idx; - } - return -1; -} - -static VALUE -require_enc(VALUE enclib) -{ - return rb_require_safe(enclib, rb_safe_level()); -} +int rb_require_internal_silent(VALUE fname); static int load_encoding(const char *name) { + ASSERT_vm_unlocking(); VALUE enclib = rb_sprintf("enc/%s.so", name); - VALUE verbose = ruby_verbose; VALUE debug = ruby_debug; - VALUE loaded; + VALUE errinfo; char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3; + int loaded; int idx; while (s < e) { - if (!ISALNUM(*s)) *s = '_'; - else if (ISUPPER(*s)) *s = TOLOWER(*s); - ++s; + if (!ISALNUM(*s)) *s = '_'; + else if (ISUPPER(*s)) *s = (char)TOLOWER(*s); + ++s; } - OBJ_FREEZE(enclib); - ruby_verbose = Qfalse; + enclib = rb_fstring(enclib); ruby_debug = Qfalse; - loaded = rb_protect(require_enc, enclib, 0); - ruby_verbose = verbose; + errinfo = rb_errinfo(); + loaded = rb_require_internal_silent(enclib); // must run without VM_LOCK ruby_debug = debug; - rb_set_errinfo(Qnil); - if (NIL_P(loaded)) return -1; - if ((idx = rb_enc_registered(name)) < 0) return -1; - if (enc_autoload_p(enc_table.list[idx].enc)) return -1; + rb_set_errinfo(errinfo); + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + if (loaded < 0 || 1 < loaded) { + idx = -1; + } + else if ((idx = enc_registered(enc_table, name)) < 0) { + idx = -1; + } + else if (rb_enc_autoload_p(enc_table->list[idx].enc)) { + idx = -1; + } + } + return idx; } static int -enc_autoload(rb_encoding *enc) +enc_autoload_body(rb_encoding *enc) { - int i; - rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base; + rb_encoding *base; + int i = 0; + ASSERT_vm_unlocking(); + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + base = enc_table->list[ENC_TO_ENCINDEX(enc)].base; + } if (base) { - i = 0; - do { - if (i >= enc_table.count) return -1; - } while (enc_table.list[i].enc != base && (++i, 1)); - if (enc_autoload_p(base)) { - if (enc_autoload(base) < 0) return -1; - } - i = ENC_TO_ENCINDEX(enc); - enc_register_at(i, rb_enc_name(enc), base); + bool do_register = true; + if (rb_enc_autoload_p(base)) { + if (rb_enc_autoload(base) < 0) { + do_register = false; + i = -1; + } + } + + if (do_register) { + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + i = ENC_TO_ENCINDEX(enc); + enc_load_from_base(enc_table, i, base); + RUBY_ASSERT(((rb_raw_encoding *)enc)->ruby_encoding_index == i); + } + } } else { - i = load_encoding(rb_enc_name(enc)); + i = -2; + } + + return i; +} + +int +rb_enc_autoload(rb_encoding *enc) +{ + ASSERT_vm_unlocking(); + int i = enc_autoload_body(enc); + if (i == -2) { + i = load_encoding(rb_enc_name(enc)); } return i; } +bool +rb_enc_autoload_p(rb_encoding *enc) +{ + int idx = ENC_TO_ENCINDEX(enc); + RUBY_ASSERT(rb_enc_from_index(idx) == enc); + return !RUBY_ATOMIC_LOAD(global_enc_table.list[idx].loaded); +} + +/* Return encoding index or UNSPECIFIED_ENCODING from encoding name */ int rb_enc_find_index(const char *name) { - int i = rb_enc_registered(name); + int i; + ASSERT_vm_unlocking(); // it needs to be unlocked so it can call `load_encoding` if necessary + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + i = enc_registered(enc_table, name); + } rb_encoding *enc; if (i < 0) { - i = load_encoding(name); + i = load_encoding(name); } else if (!(enc = rb_enc_from_index(i))) { - if (i != UNSPECIFIED_ENCODING) { - rb_raise(rb_eArgError, "encoding %s is not registered", name); - } + if (i != UNSPECIFIED_ENCODING) { + rb_raise(rb_eArgError, "encoding %s is not registered", name); + } } - else if (enc_autoload_p(enc)) { - if (enc_autoload(enc) < 0) { - rb_warn("failed to load encoding (%s); use ASCII-8BIT instead", - name); - return 0; - } + else if (rb_enc_autoload_p(enc)) { + if (rb_enc_autoload(enc) < 0) { + rb_warn("failed to load encoding (%s); use ASCII-8BIT instead", + name); + return 0; + } } return i; } +int +rb_enc_find_index2(const char *name, long len) +{ + char buf[ENCODING_NAMELEN_MAX+1]; + + if (len > ENCODING_NAMELEN_MAX) return -1; + memcpy(buf, name, len); + buf[len] = '\0'; + return rb_enc_find_index(buf); +} + rb_encoding * rb_enc_find(const char *name) { @@ -613,14 +942,21 @@ enc_capable(VALUE obj) case T_STRING: case T_REGEXP: case T_FILE: - return TRUE; + case T_SYMBOL: + return TRUE; case T_DATA: - if (is_data_encoding(obj)) return TRUE; + if (is_data_encoding(obj)) return TRUE; default: - return FALSE; + return FALSE; } } +int +rb_enc_capable(VALUE obj) +{ + return enc_capable(obj); +} + ID rb_id_encoding(void) { @@ -628,6 +964,29 @@ rb_id_encoding(void) return id_encoding; } +static int +enc_get_index_str(VALUE str) +{ + int i = ENCODING_GET_INLINED(str); + if (i == ENCODING_INLINE_MAX) { + VALUE iv; + +#if 0 + iv = rb_ivar_get(str, rb_id_encoding()); + i = NUM2INT(iv); +#else + /* + * Tentatively, assume ASCII-8BIT, if encoding index instance + * variable is not found. This can happen when freeing after + * all instance variables are removed in `obj_free`. + */ + iv = rb_attr_get(str, rb_id_encoding()); + i = NIL_P(iv) ? ENCINDEX_ASCII_8BIT : NUM2INT(iv); +#endif + } + return i; +} + int rb_enc_get_index(VALUE obj) { @@ -635,65 +994,97 @@ rb_enc_get_index(VALUE obj) VALUE tmp; if (SPECIAL_CONST_P(obj)) { - if (!SYMBOL_P(obj)) return -1; - obj = rb_id2str(SYM2ID(obj)); + if (!SYMBOL_P(obj)) return -1; + obj = rb_sym2str(obj); } switch (BUILTIN_TYPE(obj)) { - as_default: - default: case T_STRING: + case T_SYMBOL: case T_REGEXP: - i = ENCODING_GET_INLINED(obj); - if (i == ENCODING_INLINE_MAX) { - VALUE iv; - - iv = rb_ivar_get(obj, rb_id_encoding()); - i = NUM2INT(iv); - } - break; + i = enc_get_index_str(obj); + break; case T_FILE: - tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0); - if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0); - else obj = tmp; - if (NIL_P(obj)) break; + tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0); + if (NIL_P(tmp)) { + tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0); + } + if (is_obj_encoding(tmp)) { + i = enc_check_encoding(tmp); + } + break; case T_DATA: - if (is_data_encoding(obj)) { - i = enc_check_encoding(obj); - } - else { - goto as_default; - } - break; + if (is_data_encoding(obj)) { + i = enc_check_encoding(obj); + } + break; + default: + break; } return i; } +static void +enc_set_index(VALUE obj, int idx) +{ + if (!enc_capable(obj)) { + rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object"); + } + + if (idx < ENCODING_INLINE_MAX) { + ENCODING_SET_INLINED(obj, idx); + return; + } + ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX); + rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx)); +} + void -rb_enc_set_index(VALUE obj, int idx) +rb_enc_raw_set(VALUE obj, rb_encoding *enc) { + RUBY_ASSERT(enc_capable(obj)); + + int idx = enc ? ENC_TO_ENCINDEX(enc) : 0; + if (idx < ENCODING_INLINE_MAX) { - ENCODING_SET_INLINED(obj, idx); - return; + ENCODING_SET_INLINED(obj, idx); + return; } ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX); rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx)); - return; +} + +void +rb_enc_set_index(VALUE obj, int idx) +{ + rb_check_frozen(obj); + must_encindex(idx); + enc_set_index(obj, idx); } VALUE rb_enc_associate_index(VALUE obj, int idx) { -/* enc_check_capable(obj);*/ - if (rb_enc_get_index(obj) == idx) - return obj; + rb_encoding *enc; + int oldidx, oldtermlen, termlen; + + rb_check_frozen(obj); + oldidx = rb_enc_get_index(obj); + if (oldidx == idx) + return obj; if (SPECIAL_CONST_P(obj)) { - rb_raise(rb_eArgError, "cannot set encoding"); + rb_raise(rb_eArgError, "cannot set encoding"); } + enc = must_encindex(idx); if (!ENC_CODERANGE_ASCIIONLY(obj) || - !rb_enc_asciicompat(rb_enc_from_index(idx))) { - ENC_CODERANGE_CLEAR(obj); + !rb_enc_asciicompat(enc)) { + ENC_CODERANGE_CLEAR(obj); } - rb_enc_set_index(obj, idx); + termlen = rb_enc_mbminlen(enc); + oldtermlen = rb_enc_mbminlen(rb_enc_from_index(oldidx)); + if (oldtermlen != termlen && RB_TYPE_P(obj, T_STRING)) { + rb_str_change_terminator_length(obj, oldtermlen, termlen); + } + enc_set_index(obj, idx); return obj; } @@ -709,79 +1100,121 @@ rb_enc_get(VALUE obj) return rb_enc_from_index(rb_enc_get_index(obj)); } -rb_encoding* -rb_enc_check(VALUE str1, VALUE str2) +const char * +rb_enc_inspect_name(rb_encoding *enc) +{ + if (enc == global_enc_ascii) { + return "BINARY (ASCII-8BIT)"; + } + return enc->name; +} + +static rb_encoding* +rb_encoding_check(rb_encoding* enc, VALUE str1, VALUE str2) { - rb_encoding *enc = rb_enc_compatible(str1, str2); if (!enc) - rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s", - rb_enc_name(rb_enc_get(str1)), - rb_enc_name(rb_enc_get(str2))); + rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s", + rb_enc_inspect_name(rb_enc_get(str1)), + rb_enc_inspect_name(rb_enc_get(str2))); return enc; } +static rb_encoding* enc_compatible_str(VALUE str1, VALUE str2); + rb_encoding* -rb_enc_compatible(VALUE str1, VALUE str2) +rb_enc_check_str(VALUE str1, VALUE str2) { - int idx1, idx2; - rb_encoding *enc1, *enc2; + rb_encoding *enc = enc_compatible_str(MUST_STRING(str1), MUST_STRING(str2)); + return rb_encoding_check(enc, str1, str2); +} - idx1 = rb_enc_get_index(str1); - idx2 = rb_enc_get_index(str2); +rb_encoding* +rb_enc_check(VALUE str1, VALUE str2) +{ + rb_encoding *enc = rb_enc_compatible(str1, str2); + return rb_encoding_check(enc, str1, str2); +} +static rb_encoding* +enc_compatible_latter(VALUE str1, VALUE str2, int idx1, int idx2) +{ if (idx1 < 0 || idx2 < 0) return 0; if (idx1 == idx2) { - return rb_enc_from_index(idx1); + return rb_enc_from_index(idx1); } - enc1 = rb_enc_from_index(idx1); - enc2 = rb_enc_from_index(idx2); - if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0) - return (idx1 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc2)) ? enc2 : enc1; - if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0) - return (idx2 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc1)) ? enc1 : enc2; + int isstr1, isstr2; + rb_encoding *enc1 = rb_enc_from_index(idx1); + rb_encoding *enc2 = rb_enc_from_index(idx2); + + isstr2 = RB_TYPE_P(str2, T_STRING); + if (isstr2 && RSTRING_LEN(str2) == 0) + return enc1; + isstr1 = RB_TYPE_P(str1, T_STRING); + if (isstr1 && isstr2 && RSTRING_LEN(str1) == 0) + return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2; if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) { - return 0; + return 0; } /* objects whose encoding is the same of contents */ - if (BUILTIN_TYPE(str2) != T_STRING && idx2 == ENCINDEX_US_ASCII) - return enc1; - if (BUILTIN_TYPE(str1) != T_STRING && idx1 == ENCINDEX_US_ASCII) - return enc2; - - if (BUILTIN_TYPE(str1) != T_STRING) { - VALUE tmp = str1; - int idx0 = idx1; - str1 = str2; - str2 = tmp; - idx1 = idx2; - idx2 = idx0; - } - if (BUILTIN_TYPE(str1) == T_STRING) { - int cr1, cr2; - - cr1 = rb_enc_str_coderange(str1); - if (BUILTIN_TYPE(str2) == T_STRING) { - cr2 = rb_enc_str_coderange(str2); - if (cr1 != cr2) { - /* may need to handle ENC_CODERANGE_BROKEN */ - if (cr1 == ENC_CODERANGE_7BIT) return enc2; - if (cr2 == ENC_CODERANGE_7BIT) return enc1; - } - if (cr2 == ENC_CODERANGE_7BIT) { - if (idx1 == ENCINDEX_ASCII) return enc2; - return enc1; - } - } - if (cr1 == ENC_CODERANGE_7BIT) - return enc2; + if (!isstr2 && idx2 == ENCINDEX_US_ASCII) + return enc1; + if (!isstr1 && idx1 == ENCINDEX_US_ASCII) + return enc2; + + if (!isstr1) { + VALUE tmp = str1; + int idx0 = idx1; + str1 = str2; + str2 = tmp; + idx1 = idx2; + idx2 = idx0; + idx0 = isstr1; + isstr1 = isstr2; + isstr2 = idx0; + } + if (isstr1) { + int cr1, cr2; + + cr1 = rb_enc_str_coderange(str1); + if (isstr2) { + cr2 = rb_enc_str_coderange(str2); + if (cr1 != cr2) { + /* may need to handle ENC_CODERANGE_BROKEN */ + if (cr1 == ENC_CODERANGE_7BIT) return enc2; + if (cr2 == ENC_CODERANGE_7BIT) return enc1; + } + if (cr2 == ENC_CODERANGE_7BIT) { + return enc1; + } + } + if (cr1 == ENC_CODERANGE_7BIT) + return enc2; } return 0; } +static rb_encoding* +enc_compatible_str(VALUE str1, VALUE str2) +{ + int idx1 = enc_get_index_str(str1); + int idx2 = enc_get_index_str(str2); + + return enc_compatible_latter(str1, str2, idx1, idx2); +} + +rb_encoding* +rb_enc_compatible(VALUE str1, VALUE str2) +{ + int idx1 = rb_enc_get_index(str1); + int idx2 = rb_enc_get_index(str2); + + return enc_compatible_latter(str1, str2, idx1, idx2); +} + void rb_enc_copy(VALUE obj1, VALUE obj2) { @@ -791,19 +1224,22 @@ rb_enc_copy(VALUE obj1, VALUE obj2) /* * call-seq: - * obj.encoding => encoding + * encoding -> encoding * - * Returns the Encoding object that represents the encoding of obj. + * Returns an Encoding object that represents the encoding of +self+; + * see {Encodings}[rdoc-ref:encodings.rdoc]. + * + * Related: see {Querying}[rdoc-ref:String@Querying]. */ VALUE rb_obj_encoding(VALUE obj) { - rb_encoding *enc = rb_enc_get(obj); - if (!enc) { - rb_raise(rb_eTypeError, "unknown encoding"); + int idx = rb_enc_get_index(obj); + if (idx < 0) { + rb_raise(rb_eTypeError, "unknown encoding"); } - return rb_enc_from_encoding(enc); + return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK); } int @@ -839,7 +1275,8 @@ rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc) int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc) { - unsigned int c, l; + unsigned int c; + int l; if (e <= p) return -1; if (rb_enc_asciicompat(enc)) { @@ -866,19 +1303,11 @@ rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc) if (e <= p) rb_raise(rb_eArgError, "empty string"); r = rb_enc_precise_mbclen(p, e, enc); - if (MBCLEN_CHARFOUND_P(r)) { - if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r); - return rb_enc_mbc_to_codepoint(p, e, enc); + if (!MBCLEN_CHARFOUND_P(r)) { + rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc)); } - else - rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc)); -} - -#undef rb_enc_codepoint -unsigned int -rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc) -{ - return rb_enc_codepoint_len(p, e, 0, enc); + if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r); + return rb_enc_mbc_to_codepoint(p, e, enc); } int @@ -886,7 +1315,7 @@ rb_enc_codelen(int c, rb_encoding *enc) { int n = ONIGENC_CODE_TO_MBCLEN(enc,c); if (n == 0) { - rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc)); + rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc)); } return n; } @@ -905,7 +1334,7 @@ rb_enc_tolower(int c, rb_encoding *enc) /* * call-seq: - * enc.inspect => string + * enc.inspect -> string * * Returns a string which represents the encoding for programmers. * @@ -915,47 +1344,42 @@ rb_enc_tolower(int c, rb_encoding *enc) static VALUE enc_inspect(VALUE self) { - VALUE str = rb_sprintf("#<%s:%s%s>", rb_obj_classname(self), - rb_enc_name((rb_encoding*)DATA_PTR(self)), - (enc_dummy_p(self) ? " (dummy)" : "")); - ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT); - return str; -} + rb_encoding *enc; -/* - * call-seq: - * enc.name => string - * - * Returns the name of the encoding. - * - * Encoding::UTF_8.name => "UTF-8" - */ -static VALUE -enc_name(VALUE self) -{ - return rb_usascii_str_new2(rb_enc_name((rb_encoding*)DATA_PTR(self))); + if (!is_obj_encoding(self)) { /* do not resolve autoload */ + not_encoding(self); + } + if (!(enc = RTYPEDDATA_GET_DATA(self)) || rb_enc_from_index(rb_enc_to_index(enc)) != enc) { + rb_raise(rb_eTypeError, "broken Encoding"); + } + + return rb_enc_sprintf(rb_usascii_encoding(), + "#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self), + rb_enc_inspect_name(enc), + (ENC_DUMMY_P(enc) ? " (dummy)" : ""), + rb_enc_autoload_p(enc) ? " (autoload)" : ""); } + static int enc_names_i(st_data_t name, st_data_t idx, st_data_t args) { VALUE *arg = (VALUE *)args; if ((int)idx == (int)arg[0]) { - VALUE str = rb_usascii_str_new2((char *)name); - OBJ_FREEZE(str); - rb_ary_push(arg[1], str); + VALUE str = rb_interned_str_cstr((char *)name); + rb_ary_push(arg[1], str); } return ST_CONTINUE; } /* * call-seq: - * enc.names => array + * enc.names -> array * * Returns the list of name and aliases of the encoding. * - * Encoding::WINDOWS_31J.names => ["Windows-31J", "CP932", "csWindows31J"] + * Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"] */ static VALUE enc_names(VALUE self) @@ -964,46 +1388,46 @@ enc_names(VALUE self) args[0] = (VALUE)rb_to_encoding_index(self); args[1] = rb_ary_new2(0); - st_foreach(enc_table.names, enc_names_i, (st_data_t)args); + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + st_foreach(enc_table->names, enc_names_i, (st_data_t)args); + } return args[1]; } /* * call-seq: - * Encoding.list => [enc1, enc2, ...] + * Encoding.list -> [enc1, enc2, ...] * * Returns the list of loaded encodings. * * Encoding.list - * => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, - * #<Encoding:ISO-2022-JP (dummy)>] + * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, + * #<Encoding:ISO-2022-JP (dummy)>] * * Encoding.find("US-ASCII") - * => #<Encoding:US-ASCII> + * #=> #<Encoding:US-ASCII> * * Encoding.list - * => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, - * #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>] + * #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, + * #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>] * */ static VALUE enc_list(VALUE klass) { - VALUE ary = rb_ary_new2(0); - rb_ary_replace(ary, rb_encoding_list); - return ary; + VALUE list = RUBY_ATOMIC_VALUE_LOAD(rb_encoding_list); + return rb_ary_dup(list); } /* * call-seq: - * Encoding.find(string) => enc - * Encoding.find(symbol) => enc + * Encoding.find(string) -> enc * * Search the encoding with specified <i>name</i>. - * <i>name</i> should be a string or symbol. + * <i>name</i> should be a string. * - * Encoding.find("US-ASCII") => #<Encoding:US-ASCII> - * Encoding.find(:Shift_JIS) => #<Encoding:Shift_JIS> + * Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII> * * Names which this method accept are encoding names and aliases * including following special aliases @@ -1021,25 +1445,36 @@ enc_list(VALUE klass) static VALUE enc_find(VALUE klass, VALUE enc) { - return rb_enc_from_encoding(to_encoding(enc)); + int idx; + if (is_obj_encoding(enc)) + return enc; + idx = str_to_encindex(enc); + if (idx == UNSPECIFIED_ENCODING) return Qnil; + return rb_enc_from_encoding_index(idx); } /* * call-seq: - * Encoding.compatible?(str1, str2) => enc or nil + * Encoding.compatible?(obj1, obj2) -> enc or nil + * + * Checks the compatibility of two objects. * - * Checks the compatibility of two strings. - * If they are compatible, means concatenatable, - * returns an encoding which the concatenated string will be. - * If they are not compatible, nil is returned. + * If the objects are both strings they are compatible when they are + * concatenatable. The encoding of the concatenated string will be returned + * if they are compatible, nil if they are not. * * Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b") - * => #<Encoding:ISO-8859-1> + * #=> #<Encoding:ISO-8859-1> * * Encoding.compatible?( * "\xa1".force_encoding("iso-8859-1"), * "\xa1\xa1".force_encoding("euc-jp")) - * => nil + * #=> nil + * + * If the objects are non-strings their encodings are compatible when they + * have an encoding and: + * * Either encoding is US-ASCII compatible + * * One of the encodings is a 7-bit encoding * */ static VALUE @@ -1054,43 +1489,53 @@ enc_compatible_p(VALUE klass, VALUE str1, VALUE str2) return rb_enc_from_encoding(enc); } +NORETURN(static VALUE enc_s_alloc(VALUE klass)); +/* :nodoc: */ +static VALUE +enc_s_alloc(VALUE klass) +{ + rb_undefined_alloc(klass); + UNREACHABLE_RETURN(Qnil); +} + /* :nodoc: */ static VALUE enc_dump(int argc, VALUE *argv, VALUE self) { - rb_scan_args(argc, argv, "01", 0); - return enc_name(self); + rb_check_arity(argc, 0, 1); + return rb_attr_get(self, id_i_name); } /* :nodoc: */ static VALUE enc_load(VALUE klass, VALUE str) { + return str; +} + +/* :nodoc: */ +static VALUE +enc_m_loader(VALUE klass, VALUE str) +{ return enc_find(klass, str); } rb_encoding * rb_ascii8bit_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } - return enc_table.list[ENCINDEX_ASCII].enc; + return global_enc_ascii; } int rb_ascii8bit_encindex(void) { - return ENCINDEX_ASCII; + return ENCINDEX_ASCII_8BIT; } rb_encoding * rb_utf8_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } - return enc_table.list[ENCINDEX_UTF_8].enc; + return global_enc_utf_8; } int @@ -1102,10 +1547,7 @@ rb_utf8_encindex(void) rb_encoding * rb_usascii_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } - return enc_table.list[ENCINDEX_US_ASCII].enc; + return global_enc_us_ascii; } int @@ -1114,18 +1556,29 @@ rb_usascii_encindex(void) return ENCINDEX_US_ASCII; } -static int +int rb_locale_charmap_index(void); + +int rb_locale_encindex(void) { - VALUE charmap = rb_locale_charmap(rb_cEncoding); - int idx; + // `rb_locale_charmap_index` can call `enc_find_index`, which can + // load an encoding. This needs to be done without VM lock held. + ASSERT_vm_unlocking(); + int idx = rb_locale_charmap_index(); - if (NIL_P(charmap)) - idx = rb_usascii_encindex(); - else if ((idx = rb_enc_find_index(StringValueCStr(charmap))) < 0) - idx = rb_ascii8bit_encindex(); + if (idx < 0) idx = ENCINDEX_UTF_8; - if (rb_enc_registered("locale") < 0) enc_alias_internal("locale", idx); + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + if (enc_registered(enc_table, "locale") < 0) { +# if defined _WIN32 + void Init_w32_codepage(void); + Init_w32_codepage(); +# endif + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + enc_alias_internal(enc_table, "locale", idx); + } + } + } return idx; } @@ -1136,34 +1589,10 @@ rb_locale_encoding(void) return rb_enc_from_index(rb_locale_encindex()); } -static int -enc_set_filesystem_encoding(void) -{ - int idx; -#if defined NO_LOCALE_CHARMAP - idx = rb_enc_to_index(rb_default_external_encoding()); -#elif defined _WIN32 || defined __CYGWIN__ - char cp[sizeof(int) * 8 / 3 + 4]; - snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP()); - idx = rb_enc_find_index(cp); - if (idx < 0) idx = rb_ascii8bit_encindex(); -#elif defined __APPLE__ - idx = rb_utf8_encindex(); -#else - idx = rb_enc_to_index(rb_default_external_encoding()); -#endif - - enc_alias_internal("filesystem", idx); - return idx; -} - -static int +int rb_filesystem_encindex(void) { - int idx = rb_enc_registered("filesystem"); - if (idx < 0) - idx = enc_set_filesystem_encoding(); - return idx; + return filesystem_encindex; } rb_encoding * @@ -1185,23 +1614,41 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha int overridden = FALSE; if (def->index != -2) - /* Already set */ - overridden = TRUE; - - if (NIL_P(encoding)) { - def->index = -1; - def->enc = 0; - st_insert(enc_table.names, (st_data_t)strdup(name), - (st_data_t)UNSPECIFIED_ENCODING); + /* Already set */ + overridden = TRUE; + + int index = 0; + if (!NIL_P(encoding)) { + enc_check_encoding(encoding); // loads it if necessary. Needs to be done outside of VM lock. + index = rb_enc_to_index(rb_to_encoding(encoding)); + } + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + if (NIL_P(encoding)) { + def->index = -1; + def->enc = 0; + char *name_dup = strdup(name); + + st_data_t existing_name = (st_data_t)name_dup; + if (st_delete(enc_table->names, &existing_name, NULL)) { + xfree((void *)existing_name); + } + + st_insert(enc_table->names, (st_data_t)name_dup, + (st_data_t)UNSPECIFIED_ENCODING); + } + else { + def->index = index; + def->enc = 0; + enc_alias_internal(enc_table, name, def->index); + } + + if (def == &default_external) { + int fs_idx = Init_enc_set_filesystem_encoding(); + enc_alias_internal(enc_table, "filesystem", fs_idx); + filesystem_encindex = fs_idx; + } } - else { - def->index = rb_enc_to_index(rb_to_encoding(encoding)); - def->enc = 0; - enc_alias_internal(name, def->index); - } - - if (def == &default_external) - enc_set_filesystem_encoding(); return overridden; } @@ -1228,11 +1675,31 @@ rb_enc_default_external(void) /* * call-seq: - * Encoding.default_external => enc + * Encoding.default_external -> enc * * Returns default external encoding. * - * It is initialized by the locale or -E option. + * The default external encoding is used by default for strings created from + * the following locations: + * + * * CSV + * * File data read from disk + * * SDBM + * * StringIO + * * Zlib::GzipReader + * * Zlib::GzipWriter + * * String#inspect + * * Regexp#inspect + * + * While strings created from these locations will have this encoding, the + * encoding may not be valid. Be sure to check String#valid_encoding?. + * + * File data written to disk will be transcoded to the default external + * encoding when written, if default_internal is not nil. + * + * The default external encoding is initialized by the -E option. + * If -E isn't set, it is initialized to UTF-8 on Windows and the locale on + * other operating systems. */ static VALUE get_default_external(VALUE klass) @@ -1254,7 +1721,14 @@ rb_enc_set_default_external(VALUE encoding) * call-seq: * Encoding.default_external = enc * - * Sets default external encoding. + * Sets default external encoding. You should not set + * Encoding::default_external in ruby code as strings created before changing + * the value may have a different encoding from strings created after the value + * was changed., instead you should use <tt>ruby -E</tt> to invoke ruby with + * the correct default_external. + * + * See Encoding::default_external for information on how the default external + * encoding is used. */ static VALUE set_default_external(VALUE klass, VALUE encoding) @@ -1284,11 +1758,31 @@ rb_enc_default_internal(void) /* * call-seq: - * Encoding.default_internal => enc + * Encoding.default_internal -> enc + * + * Returns default internal encoding. Strings will be transcoded to the + * default internal encoding in the following places if the default internal + * encoding is not nil: + * + * * CSV + * * Etc.sysconfdir and Etc.systmpdir + * * File data read from disk + * * File names from Dir + * * Integer#chr + * * String#inspect and Regexp#inspect + * * Strings returned from Readline + * * Strings returned from SDBM + * * Time#zone + * * Values from ENV + * * Values in ARGV including $PROGRAM_NAME + * + * Additionally String#encode and String#encode! use the default internal + * encoding if no encoding is given. * - * Returns default internal encoding. + * The script encoding (__ENCODING__), not default_internal, is used as the + * encoding of created strings. * - * It is initialized by the source internal_encoding or -E option. + * Encoding::default_internal is initialized with -E option or nil otherwise. */ static VALUE get_default_internal(VALUE klass) @@ -1307,8 +1801,14 @@ rb_enc_set_default_internal(VALUE encoding) * call-seq: * Encoding.default_internal = enc or nil * - * Sets default internal encoding. - * Or removes default internal encoding when passed nil. + * Sets default internal encoding or removes default internal encoding when + * passed nil. You should not set Encoding::default_internal in ruby code as + * strings created before changing the value may have a different encoding + * from strings created after the change. Instead you should use + * <tt>ruby -E</tt> to invoke ruby with the correct default_internal. + * + * See Encoding::default_internal for information on how the default internal + * encoding is used. */ static VALUE set_default_internal(VALUE klass, VALUE encoding) @@ -1318,53 +1818,6 @@ set_default_internal(VALUE klass, VALUE encoding) return encoding; } -/* - * call-seq: - * Encoding.locale_charmap => string - * - * Returns the locale charmap name. - * - * Debian GNU/Linux - * LANG=C - * Encoding.locale_charmap => "ANSI_X3.4-1968" - * LANG=ja_JP.EUC-JP - * Encoding.locale_charmap => "EUC-JP" - * - * SunOS 5 - * LANG=C - * Encoding.locale_charmap => "646" - * LANG=ja - * Encoding.locale_charmap => "eucJP" - * - * The result is highly platform dependent. - * So Encoding.find(Encoding.locale_charmap) may cause an error. - * If you need some encoding object even for unknown locale, - * Encoding.find("locale") can be used. - * - */ -VALUE -rb_locale_charmap(VALUE klass) -{ -#if defined NO_LOCALE_CHARMAP - return rb_usascii_str_new2("ASCII-8BIT"); -#elif defined _WIN32 || defined __CYGWIN__ - const char *nl_langinfo_codeset(void); - const char *codeset = nl_langinfo_codeset(); - char cp[sizeof(int) * 3 + 4]; - if (!codeset) { - snprintf(cp, sizeof(cp), "CP%d", GetConsoleCP()); - codeset = cp; - } - return rb_usascii_str_new2(codeset); -#elif defined HAVE_LANGINFO_H - char *codeset; - codeset = nl_langinfo(CODESET); - return rb_usascii_str_new2(codeset); -#else - return Qnil; -#endif -} - static void set_encoding_const(const char *name, rb_encoding *enc) { @@ -1374,45 +1827,45 @@ set_encoding_const(const char *name, rb_encoding *enc) if (ISDIGIT(*s)) return; if (ISUPPER(*s)) { - hasupper = 1; - while (*++s && (ISALNUM(*s) || *s == '_')) { - if (ISLOWER(*s)) haslower = 1; - } + hasupper = 1; + while (*++s && (ISALNUM(*s) || *s == '_')) { + if (ISLOWER(*s)) haslower = 1; + } } if (!*s) { - if (s - name > ENCODING_NAMELEN_MAX) return; - valid = 1; - rb_define_const(rb_cEncoding, name, encoding); + if (s - name > ENCODING_NAMELEN_MAX) return; + valid = 1; + rb_define_const(rb_cEncoding, name, encoding); } if (!valid || haslower) { - size_t len = s - name; - if (len > ENCODING_NAMELEN_MAX) return; - if (!haslower || !hasupper) { - do { - if (ISLOWER(*s)) haslower = 1; - if (ISUPPER(*s)) hasupper = 1; - } while (*++s && (!haslower || !hasupper)); - len = s - name; - } - len += strlen(s); - if (len++ > ENCODING_NAMELEN_MAX) return; - MEMCPY(s = ALLOCA_N(char, len), name, char, len); - name = s; - if (!valid) { - if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); - for (; *s; ++s) { - if (!ISALNUM(*s)) *s = '_'; - } - if (hasupper) { - rb_define_const(rb_cEncoding, name, encoding); - } - } - if (haslower) { - for (s = (char *)name; *s; ++s) { - if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); - } - rb_define_const(rb_cEncoding, name, encoding); - } + size_t len = s - name; + if (len > ENCODING_NAMELEN_MAX) return; + if (!haslower || !hasupper) { + do { + if (ISLOWER(*s)) haslower = 1; + if (ISUPPER(*s)) hasupper = 1; + } while (*++s && (!haslower || !hasupper)); + len = s - name; + } + len += strlen(s); + if (len++ > ENCODING_NAMELEN_MAX) return; + MEMCPY(s = ALLOCA_N(char, len), name, char, len); + name = s; + if (!valid) { + if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); + for (; *s; ++s) { + if (!ISALNUM(*s)) *s = '_'; + } + if (hasupper) { + rb_define_const(rb_cEncoding, name, encoding); + } + } + if (haslower) { + for (s = (char *)name; *s; ++s) { + if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); + } + rb_define_const(rb_cEncoding, name, encoding); + } } } @@ -1420,31 +1873,33 @@ static int rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg) { VALUE ary = (VALUE)arg; - VALUE str = rb_usascii_str_new2((char *)name); - OBJ_FREEZE(str); + VALUE str = rb_interned_str_cstr((char *)name); rb_ary_push(ary, str); return ST_CONTINUE; } /* * call-seq: - * Encoding.name_list => ["enc1", "enc2", ...] + * Encoding.name_list -> ["enc1", "enc2", ...] * * Returns the list of available encoding names. * * Encoding.name_list - * => ["US-ASCII", "ASCII-8BIT", "UTF-8", - * "ISO-8859-1", "Shift_JIS", "EUC-JP", - * "Windows-31J", - * "BINARY", "CP932", "eucJP"] + * #=> ["US-ASCII", "ASCII-8BIT", "UTF-8", + * "ISO-8859-1", "Shift_JIS", "EUC-JP", + * "Windows-31J", + * "BINARY", "CP932", "eucJP"] * */ static VALUE rb_enc_name_list(VALUE klass) { - VALUE ary = rb_ary_new2(enc_table.names->num_entries); - st_foreach(enc_table.names, rb_enc_name_list_i, (st_data_t)ary); + VALUE ary; + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + ary = rb_ary_new2(enc_table->names->num_entries); + st_foreach(enc_table->names, rb_enc_name_list_i, (st_data_t)ary); + } return ary; } @@ -1457,31 +1912,29 @@ rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg) VALUE key, str = rb_ary_entry(ary, idx); if (NIL_P(str)) { - rb_encoding *enc = rb_enc_from_index(idx); - - if (!enc) return ST_CONTINUE; - if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) { - return ST_CONTINUE; - } - str = rb_usascii_str_new2(rb_enc_name(enc)); - OBJ_FREEZE(str); - rb_ary_store(ary, idx, str); - } - key = rb_usascii_str_new2((char *)name); - OBJ_FREEZE(key); + rb_encoding *enc = rb_enc_from_index(idx); + + if (!enc) return ST_CONTINUE; + if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) { + return ST_CONTINUE; + } + str = rb_fstring_cstr(rb_enc_name(enc)); + rb_ary_store(ary, idx, str); + } + key = rb_interned_str_cstr((char *)name); rb_hash_aset(aliases, key, str); return ST_CONTINUE; } /* * call-seq: - * Encoding.aliases => {"alias1" => "orig1", "alias2" => "orig2", ...} + * Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...} * * Returns the hash of available encoding alias and original encoding name. * * Encoding.aliases - * => {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1986"=>"US-ASCII", - * "SJIS"=>"Shift_JIS", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"} + * #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII", + * "SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"} * */ @@ -1491,27 +1944,67 @@ rb_enc_aliases(VALUE klass) VALUE aliases[2]; aliases[0] = rb_hash_new(); aliases[1] = rb_ary_new(); - st_foreach(enc_table.names, rb_enc_aliases_enc_i, (st_data_t)aliases); + + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + st_foreach(enc_table->names, rb_enc_aliases_enc_i, (st_data_t)aliases); + } + return aliases[0]; } +/* + * An \Encoding instance represents a character encoding usable in Ruby. + * It is defined as a constant under the \Encoding namespace. + * It has a name and, optionally, aliases: + * + * Encoding::US_ASCII.name # => "US-ASCII" + * Encoding::US_ASCII.names # => ["US-ASCII", "ASCII", "ANSI_X3.4-1968", "646"] + * + * A Ruby method that accepts an encoding as an argument will accept: + * + * - An \Encoding object. + * - The name of an encoding. + * - An alias for an encoding name. + * + * These are equivalent: + * + * 'foo'.encode(Encoding::US_ASCII) # Encoding object. + * 'foo'.encode('US-ASCII') # Encoding name. + * 'foo'.encode('ASCII') # Encoding alias. + * + * For a full discussion of encodings and their uses, + * see {the Encodings document}[rdoc-ref:encodings.rdoc]. + * + * Encoding::ASCII_8BIT is a special-purpose encoding that is usually used for + * a string of bytes, not a string of characters. + * But as the name indicates, its characters in the ASCII range + * are considered as ASCII characters. + * This is useful when you use other ASCII-compatible encodings. + * + */ + void Init_Encoding(void) { -#undef rb_intern -#define rb_intern(str) rb_intern_const(str) VALUE list; int i; + id_i_name = rb_intern_const("@name"); rb_cEncoding = rb_define_class("Encoding", rb_cObject); - rb_undef_alloc_func(rb_cEncoding); - rb_define_method(rb_cEncoding, "to_s", enc_name, 0); + rb_define_alloc_func(rb_cEncoding, enc_s_alloc); + rb_undef_method(CLASS_OF(rb_cEncoding), "new"); + + /* The name of the encoding. + * + * Encoding::UTF_8.name #=> "UTF-8" + */ + rb_attr(rb_cEncoding, rb_intern("name"), TRUE, FALSE, Qfalse); + rb_define_alias(rb_cEncoding, "to_s", "name"); + rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0); - rb_define_method(rb_cEncoding, "name", enc_name, 0); rb_define_method(rb_cEncoding, "names", enc_names, 0); rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0); rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0); - rb_define_method(rb_cEncoding, "replicate", enc_replicate, 1); rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0); rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0); rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0); @@ -1525,45 +2018,45 @@ Init_Encoding(void) rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1); rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0); rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1); - rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); + rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); /* in localeinit.c */ + + struct enc_table *enc_table = &global_enc_table; - list = rb_ary_new2(enc_table.count); - RBASIC(list)->klass = 0; - rb_encoding_list = list; - rb_gc_register_mark_object(list); + rb_gc_register_address(&rb_encoding_list); + list = rb_encoding_list = rb_ary_new2(ENCODING_LIST_CAPA); + RBASIC_CLEAR_CLASS(list); - for (i = 0; i < enc_table.count; ++i) { - rb_ary_push(list, enc_new(enc_table.list[i].enc)); + for (i = 0; i < enc_table->count; ++i) { + rb_ary_push(list, enc_new(enc_table->list[i].enc)); } -} -/* locale insensitive functions */ + rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader); +} -#define ctype_test(c, ctype) \ - (rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), ctype)) +void +Init_unicode_version(void) +{ + extern const char onigenc_unicode_version_string[]; -int rb_isalnum(int c) { return ctype_test(c, ONIGENC_CTYPE_ALNUM); } -int rb_isalpha(int c) { return ctype_test(c, ONIGENC_CTYPE_ALPHA); } -int rb_isblank(int c) { return ctype_test(c, ONIGENC_CTYPE_BLANK); } -int rb_iscntrl(int c) { return ctype_test(c, ONIGENC_CTYPE_CNTRL); } -int rb_isdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_DIGIT); } -int rb_isgraph(int c) { return ctype_test(c, ONIGENC_CTYPE_GRAPH); } -int rb_islower(int c) { return ctype_test(c, ONIGENC_CTYPE_LOWER); } -int rb_isprint(int c) { return ctype_test(c, ONIGENC_CTYPE_PRINT); } -int rb_ispunct(int c) { return ctype_test(c, ONIGENC_CTYPE_PUNCT); } -int rb_isspace(int c) { return ctype_test(c, ONIGENC_CTYPE_SPACE); } -int rb_isupper(int c) { return ctype_test(c, ONIGENC_CTYPE_UPPER); } -int rb_isxdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_XDIGIT); } + VALUE str = rb_usascii_str_new_static(onigenc_unicode_version_string, + strlen(onigenc_unicode_version_string)); + OBJ_FREEZE(str); + /* The supported Unicode version. */ + rb_define_const(rb_cEncoding, "UNICODE_VERSION", str); +} -int -rb_tolower(int c) +void +Init_encodings(void) { - return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) : c; + rb_enc_init(&global_enc_table); } -int -rb_toupper(int c) +/* locale insensitive ctype functions */ + +void +rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg) { - return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) : c; + GLOBAL_ENC_TABLE_LOCKING(enc_table) { + st_foreach(enc_table->names, func, arg); + } } - |
