summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-30 02:56:30 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-30 02:56:30 (GMT)
commitf9d5843fd92d1cbc41eef8f2bcf54ad23326f01d (patch)
tree6a76898c015972f2e3fb1c3bdbf558fb35946c93 /hash.c
parent8458e709ab4fd1c095b6281fe74f44e4dd4090d5 (diff)
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
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c24
1 files changed, 7 insertions, 17 deletions
diff --git a/hash.c b/hash.c
index c47f21e..d8d08e5 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