summaryrefslogtreecommitdiff
path: root/encoding.c
diff options
context:
space:
mode:
Diffstat (limited to 'encoding.c')
-rw-r--r--encoding.c72
1 files changed, 53 insertions, 19 deletions
diff --git a/encoding.c b/encoding.c
index 34e89d08d8..2816cd0002 100644
--- a/encoding.c
+++ b/encoding.c
@@ -69,26 +69,30 @@ enc_new(rb_encoding *encoding)
return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, encoding);
}
-VALUE
-rb_enc_from_encoding(rb_encoding *encoding)
+static VALUE
+rb_enc_from_encoding_index(int idx)
{
VALUE list, enc;
- int idx;
- if (!encoding) return Qnil;
- idx = ENC_TO_ENCINDEX(encoding);
if (!(list = rb_encoding_list)) {
- rb_bug("rb_enc_from_encoding(%d\"%s\"): no rb_encoding_list",
- idx, rb_enc_name(encoding));
+ rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
}
enc = rb_ary_entry(list, idx);
if (NIL_P(enc)) {
- rb_bug("rb_enc_from_encoding(%d\"%s\"): not created yet",
- idx, rb_enc_name(encoding));
+ rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
}
return enc;
}
+VALUE
+rb_enc_from_encoding(rb_encoding *encoding)
+{
+ int idx;
+ if (!encoding) return Qnil;
+ idx = ENC_TO_ENCINDEX(encoding);
+ return rb_enc_from_encoding_index(idx);
+}
+
static int enc_autoload(rb_encoding *);
static int
@@ -311,8 +315,25 @@ rb_enc_replicate(const char *name, rb_encoding *encoding)
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(int idx, const char *name, rb_encoding *origenc)
+enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx)
{
if (idx < 0) {
idx = enc_register(name, origenc);
@@ -336,7 +357,7 @@ rb_encdb_replicate(const char *name, const char *orig)
if (origidx < 0) {
origidx = enc_register(orig, 0);
}
- return enc_replicate(idx, name, rb_enc_from_index(origidx));
+ return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx);
}
int
@@ -352,8 +373,8 @@ rb_define_dummy_encoding(const char *name)
int
rb_encdb_dummy(const char *name)
{
- int index = enc_replicate(rb_enc_registered(name), name,
- rb_ascii8bit_encoding());
+ int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(),
+ rb_enc_registered(name));
rb_encoding *enc = enc_table.list[index].enc;
ENC_SET_DUMMY(enc);
@@ -666,7 +687,7 @@ rb_enc_associate_index(VALUE obj, int idx)
{
/* enc_check_capable(obj);*/
if (rb_enc_get_index(obj) == idx)
- return obj;
+ return obj;
if (SPECIAL_CONST_P(obj)) {
rb_raise(rb_eArgError, "cannot set encoding");
}
@@ -1118,7 +1139,7 @@ rb_locale_encoding(void)
}
static int
-rb_filesystem_encindex(void)
+enc_set_filesystem_encoding(void)
{
int idx;
#if defined NO_LOCALE_CHARMAP
@@ -1131,11 +1152,19 @@ rb_filesystem_encindex(void)
#elif defined __APPLE__
idx = rb_utf8_encindex();
#else
- idx = rb_locale_encindex();
+ idx = rb_enc_to_index(rb_default_external_encoding());
#endif
- if (rb_enc_registered("filesystem") < 0) enc_alias_internal("filesystem", idx);
+ enc_alias_internal("filesystem", idx);
+ return idx;
+}
+static int
+rb_filesystem_encindex(void)
+{
+ int idx = rb_enc_registered("filesystem");
+ if (idx < 0)
+ idx = enc_set_filesystem_encoding();
return idx;
}
@@ -1150,6 +1179,8 @@ struct default_encoding {
rb_encoding *enc;
};
+static struct default_encoding default_external = {0};
+
static int
enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
{
@@ -1171,11 +1202,12 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha
enc_alias_internal(name, def->index);
}
+ if (def == &default_external)
+ enc_set_filesystem_encoding();
+
return overridden;
}
-static struct default_encoding default_external = {0};
-
rb_encoding *
rb_default_external_encoding(void)
{
@@ -1480,12 +1512,14 @@ InitVM_Encoding(void)
rb_cEncoding = rb_define_class("Encoding", rb_cObject);
rb_undef_alloc_func(rb_cEncoding);
+ rb_undef_method(CLASS_OF(rb_cEncoding), "new");
rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
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);