ruby-changes:6861
From: nobu <ko1@a...>
Date: Wed, 6 Aug 2008 05:27:02 +0900 (JST)
Subject: [ruby-changes:6861] Ruby:r18379 (trunk): * transcode.c (str_encode): no need to duplicate first.
nobu 2008-08-06 05:26:45 +0900 (Wed, 06 Aug 2008) New Revision: 18379 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18379 Log: * transcode.c (str_encode): no need to duplicate first. Modified files: trunk/ChangeLog trunk/transcode.c Index: ChangeLog =================================================================== --- ChangeLog (revision 18378) +++ ChangeLog (revision 18379) @@ -1,3 +1,7 @@ +Wed Aug 6 05:26:42 2008 Nobuyoshi Nakada <nobu@r...> + + * transcode.c (str_encode): no need to duplicate first. + Wed Aug 6 05:08:30 2008 Nobuyoshi Nakada <nobu@r...> * array.c (rb_ary_sort_bang): reset to real class. Index: transcode.c =================================================================== --- transcode.c (revision 18378) +++ transcode.c (revision 18379) @@ -469,6 +469,24 @@ return to_encidx; } +static inline VALUE +str_encode_associate(VALUE str, int encidx) +{ + int cr = 0; + + rb_enc_associate_index(str, encidx); + + /* transcoded string never be broken. */ + if (rb_enc_asciicompat(rb_enc_from_index(encidx))) { + rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr); + } + else { + cr = ENC_CODERANGE_VALID; + } + ENC_CODERANGE_SET(str, cr); + return str; +} + /* * call-seq: * str.encode!(encoding [, options] ) => str @@ -488,21 +506,10 @@ { VALUE newstr = str; int encidx = str_transcode(argc, argv, &newstr); - int cr = 0; if (encidx < 0) return str; rb_str_shared_replace(str, newstr); - rb_enc_associate_index(str, encidx); - - /* transcoded string never be broken. */ - if (rb_enc_asciicompat(rb_enc_from_index(encidx))) { - rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr); - } - else { - cr = ENC_CODERANGE_VALID; - } - ENC_CODERANGE_SET(str, cr); - return str; + return str_encode_associate(str, encidx); } /* @@ -521,8 +528,12 @@ static VALUE str_encode(int argc, VALUE *argv, VALUE str) { - str = rb_str_dup(str); - return str_encode_bang(argc, argv, str); + VALUE newstr = str; + int encidx = str_transcode(argc, argv, &newstr); + + if (encidx < 0) return rb_str_dup(str); + RBASIC(newstr)->klass = rb_obj_class(str); + return str_encode_associate(newstr, encidx); } VALUE -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/