From: Nobuyoshi Nakada Date: 2007-09-15T15:05:21+09:00 Subject: [ruby-dev:31788] Re: "string"[/str/]=:not_string dumps core なかだです。 At Fri, 14 Sep 2007 23:47:26 +0900, Nobuyoshi Nakada wrote in [ruby-dev:31780]: > http://yowaken.dip.jp/tdiary/20070908.html#p05 の件のパッチです。 > ENCODING_*マクロはT_STRING限定ということですよね。 だけだとまずいですね。T_REGEXPとT_FILEもかな? Index: encoding.c =================================================================== --- encoding.c (revision 13443) +++ encoding.c (working copy) @@ -80,7 +80,47 @@ rb_enc_find(const char *name) } +static int +enc_capable(VALUE obj) +{ + if (IMMEDIATE_P(obj)) return Qfalse; + switch (BUILTIN_TYPE(obj)) { + case T_STRING: + case T_REGEXP: + case T_FILE: + return Qtrue; + default: + return Qfalse; + } +} + +static void +enc_check_capable(VALUE x) +{ + if (!enc_capable(x)) { + const char *etype; + + if (NIL_P(x)) { + etype = "nil"; + } + else if (FIXNUM_P(x)) { + etype = "Fixnum"; + } + else if (SYMBOL_P(x)) { + etype = "Symbol"; + } + else if (rb_special_const_p(x)) { + etype = RSTRING_PTR(rb_obj_as_string(x)); + } + else { + etype = rb_obj_classname(x); + } + rb_raise(rb_eTypeError, "wrong argument type %s (not encode capable)", etype); + } +} + void rb_enc_associate_index(VALUE obj, int idx) { + enc_check_capable(obj); if (idx < ENCODING_INLINE_MAX) { ENCODING_SET(obj, idx); @@ -118,6 +158,8 @@ int rb_enc_get_index(VALUE obj) { - int i = ENCODING_GET(obj); + int i; + enc_check_capable(obj); + i = ENCODING_GET(obj); if (i == ENCODING_INLINE_MAX) { VALUE iv; -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦