From f9d5843fd92d1cbc41eef8f2bcf54ad23326f01d Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 30 Jul 2017 02:56:30 +0000 Subject: hash.c: refactor env_enc_str_new * hash.c (env_enc_str_new): convert to the expected encoding without intermediate string, and set econv flags if default internal encoding is set too. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- hash.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/hash.c b/hash.c index c47f21ee9f..d8d08e5b36 100644 --- a/hash.c +++ b/hash.c @@ -3168,27 +3168,17 @@ extern char **environ; #define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0) #endif -#ifdef _WIN32 -static VALUE -env_str_transcode(VALUE str, rb_encoding *enc) -{ - rb_encoding *internal = rb_default_internal_encoding(); - if (!internal) { - return rb_str_conv_enc_opts(str, NULL, enc, - ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE, - Qnil); - } - else { - return rb_external_str_with_enc(str, rb_utf8_encoding()); - } -} -#endif - static VALUE env_enc_str_new(const char *ptr, long len, rb_encoding *enc) { #ifdef _WIN32 - VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), enc); + rb_encoding *internal = rb_default_internal_encoding(); + const int ecflags = ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE; + rb_encoding *utf8 = rb_utf8_encoding(); + VALUE str = rb_enc_str_new(NULL, 0, (internal ? internal : enc)); + if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, utf8, ecflags, Qnil))) { + rb_str_initialize(str, ptr, len, utf8); + } #else VALUE str = rb_external_str_new_with_enc(ptr, len, enc); #endif -- cgit v1.2.3