ruby-changes:34824
From: nobu <ko1@a...>
Date: Wed, 23 Jul 2014 16:20:51 +0900 (JST)
Subject: [ruby-changes:34824] nobu:r46907 (trunk): time.c: unnecessary encoding
nobu 2014-07-23 16:20:39 +0900 (Wed, 23 Jul 2014) New Revision: 46907 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46907 Log: time.c: unnecessary encoding * time.c (time_zone_name): remove unnecessary encoding and conversion if it is 7bit-ascii only. Modified files: trunk/test/ruby/test_time.rb trunk/time.c Index: time.c =================================================================== --- time.c (revision 46906) +++ time.c (revision 46907) @@ -4198,6 +4198,16 @@ time_isdst(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L4198 return tobj->vtm.isdst ? Qtrue : Qfalse; } +static VALUE +time_zone_name(const char *zone) +{ + VALUE name = rb_str_new_cstr(zone); + if (!rb_enc_str_asciionly_p(name)) { + name = rb_external_str_with_enc(name, rb_locale_encoding()); + } + return name; +} + /* * call-seq: * time.zone -> string @@ -4220,11 +4230,12 @@ time_zone(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L4230 MAKE_TM(time, tobj); if (TIME_UTC_P(tobj)) { - return rb_obj_untaint(rb_locale_str_new_cstr("UTC")); + return rb_usascii_str_new_cstr("UTC"); } if (tobj->vtm.zone == NULL) return Qnil; - return rb_obj_untaint(rb_locale_str_new_cstr(tobj->vtm.zone)); + + return time_zone_name(tobj->vtm.zone); } /* @@ -4693,7 +4704,7 @@ time_mdump(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L4704 rb_ivar_set(str, id_offset, off); } if (tobj->vtm.zone) { - rb_ivar_set(str, id_zone, rb_locale_str_new_cstr(tobj->vtm.zone)); + rb_ivar_set(str, id_zone, time_zone_name(tobj->vtm.zone)); } return str; } Index: test/ruby/test_time.rb =================================================================== --- test/ruby/test_time.rb (revision 46906) +++ test/ruby/test_time.rb (revision 46907) @@ -520,6 +520,7 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L520 def assert_zone_encoding(time) zone = time.zone assert_predicate(zone, :valid_encoding?) + return if zone.ascii_only? enc = Encoding.default_internal || Encoding.find('locale') assert_equal(enc, zone.encoding) end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/