From 3c41a04b6cedbace1406d46a20a673f59b1502de Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Wed, 2 Aug 2023 17:08:19 +0200 Subject: [PATCH] Fix encoding switches when RUBYOPT is empty or only spaces * Follow-up of dbbc3583ba432c279f07b1fa0afb0a8a9ba50c91 which broke this. --- ruby.c | 5 +++-- spec/ruby/command_line/dash_upper_u_spec.rb | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ruby.c b/ruby.c index 30e0e68360..2b2a91ecac 100644 --- a/ruby.c +++ b/ruby.c @@ -895,10 +895,11 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) ruby_features_t warn = opt->warn; int backtrace_length_limit = opt->backtrace_length_limit; - opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0; - while (ISSPACE(*s)) s++; if (!*s) return; + + opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0; + argstr = rb_str_tmp_new((len = strlen(s)) + (envopt!=0)); argary = rb_str_tmp_new(0); diff --git a/spec/ruby/command_line/dash_upper_u_spec.rb b/spec/ruby/command_line/dash_upper_u_spec.rb index d62718b095..15854e7b73 100644 --- a/spec/ruby/command_line/dash_upper_u_spec.rb +++ b/spec/ruby/command_line/dash_upper_u_spec.rb @@ -6,6 +6,13 @@ describe "ruby -U" do options: '-U').should == 'UTF-8' end + it "sets Encoding.default_internal to UTF-8 when RUBYOPT is empty or only spaces" do + ruby_exe('p Encoding.default_internal', + options: '-U', env: { 'RUBYOPT' => '' }).should == "#\n" + ruby_exe('p Encoding.default_internal', + options: '-U', env: { 'RUBYOPT' => ' ' }).should == "#\n" + end + it "does nothing different if specified multiple times" do ruby_exe('print Encoding.default_internal.name', options: '-U -U').should == 'UTF-8'