diff --git a/ChangeLog b/ChangeLog index 3a3c601dfd..4fc128749a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Thu Dec 10 12:56:02 2009 NARUSE, Yui + + * encoding.c (enc_replicate): add Encoding#replicate(name). + + * encoding.c (enc_replicate_with_index): renamed from old + enc_replicate. + + * encoding.c (rb_enc_from_encoding_index): split from + rb_enc_from_encoding. + Thu Dec 10 09:15:00 2009 NARUSE, Yui * string.c (rb_str_inspect): CHAR_ESC_LEN should be 13. diff --git a/encoding.c b/encoding.c index 38d81b8e2f..2d50f9f8ac 100644 --- a/encoding.c +++ b/encoding.c @@ -70,26 +70,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 @@ -309,8 +313,16 @@ rb_enc_replicate(const char *name, rb_encoding *encoding) return idx; } +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); @@ -334,7 +346,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 @@ -350,8 +362,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); @@ -1490,6 +1502,7 @@ Init_Encoding(void) 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);