From c97a8423911be84bf4de58cd5f7c8cafdc795273 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 5 Aug 2008 20:31:59 +0000 Subject: [PATCH] * encoding.c (rb_to_encoding_index, rb_to_encoding): check if the name is ascii compatible as well as Encoding.find. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++- encoding.c | 43 ++++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 18047b1265..08befcccfe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Wed Aug 6 05:26:42 2008 Nobuyoshi Nakada +Wed Aug 6 05:31:54 2008 Nobuyoshi Nakada + + * encoding.c (rb_to_encoding_index, rb_to_encoding): check if the name + is ascii compatible as well as Encoding.find. * transcode.c (str_encode): no need to duplicate first. diff --git a/encoding.c b/encoding.c index db6cd31ec9..e0f2bc9154 100644 --- a/encoding.c +++ b/encoding.c @@ -121,22 +121,33 @@ rb_to_encoding_index(VALUE enc) else if (NIL_P(enc = rb_check_string_type(enc))) { return -1; } - else { - return rb_enc_find_index(StringValueCStr(enc)); + if (!rb_enc_asciicompat(rb_enc_get(enc))) { + return -1; } + return rb_enc_find_index(StringValueCStr(enc)); +} + +static rb_encoding * +to_encoding(VALUE enc) +{ + int idx; + + StringValue(enc); + if (!rb_enc_asciicompat(rb_enc_get(enc))) { + rb_raise(rb_eArgError, "invalid name encoding (non ASCII)"); + } + idx = rb_enc_find_index(StringValueCStr(enc)); + if (idx < 0) { + rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); + } + return rb_enc_from_index(idx); } rb_encoding * rb_to_encoding(VALUE enc) { - int idx; - - idx = enc_check_encoding(enc); - if (idx >= 0) return RDATA(enc)->data; - if ((idx = rb_enc_find_index(StringValueCStr(enc))) < 0) { - rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); - } - return rb_enc_from_index(idx); + if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data; + return to_encoding(enc); } void @@ -838,17 +849,7 @@ enc_list(VALUE klass) static VALUE enc_find(VALUE klass, VALUE enc) { - int idx; - - StringValue(enc); - if (!rb_enc_asciicompat(rb_enc_get(enc))) { - rb_raise(rb_eArgError, "invalid name encoding (non ASCII)"); - } - idx = rb_enc_find_index(StringValueCStr(enc)); - if (idx < 0) { - rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc)); - } - return rb_enc_from_encoding(rb_enc_from_index(idx)); + return rb_enc_from_encoding(to_encoding(enc)); } /*