diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-08-20 01:04:02 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-08-20 03:57:13 +0900 |
commit | 1ef49de83483e6f78bfe9c795a473ccfb29db150 (patch) | |
tree | f6c48f28801763a95372f143b2cdea5564b96336 | |
parent | ce384ef5a95b809f248e089c1608e60753dabe45 (diff) |
[Bug #18955] format single character for `%c`
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6258
-rw-r--r-- | spec/ruby/core/kernel/shared/sprintf.rb | 28 | ||||
-rw-r--r-- | spec/ruby/core/string/modulo_spec.rb | 12 | ||||
-rw-r--r-- | sprintf.c | 11 | ||||
-rw-r--r-- | test/ruby/test_m17n.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_sprintf.rb | 3 |
5 files changed, 38 insertions, 18 deletions
diff --git a/spec/ruby/core/kernel/shared/sprintf.rb b/spec/ruby/core/kernel/shared/sprintf.rb index 84d472b0d1..59f5ab0036 100644 --- a/spec/ruby/core/kernel/shared/sprintf.rb +++ b/spec/ruby/core/kernel/shared/sprintf.rb @@ -289,16 +289,28 @@ describe :kernel_sprintf, shared: true do @method.call("%c", "a").should == "a" end - it "raises ArgumentError if argument is a string of several characters" do - -> { - @method.call("%c", "abc") - }.should raise_error(ArgumentError) + ruby_version_is ""..."3.2" do + it "raises ArgumentError if argument is a string of several characters" do + -> { + @method.call("%c", "abc") + }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if argument is an empty string" do + -> { + @method.call("%c", "") + }.should raise_error(ArgumentError) + end end - it "raises ArgumentError if argument is an empty string" do - -> { - @method.call("%c", "") - }.should raise_error(ArgumentError) + ruby_version_is "3.2" do + it "displays only the first character if argument is a string of several characters" do + @method.call("%c", "abc").should == "a" + end + + it "displays no characters if argument is an empty string" do + @method.call("%c", "").should == "" + end end it "supports Unicode characters" do diff --git a/spec/ruby/core/string/modulo_spec.rb b/spec/ruby/core/string/modulo_spec.rb index 99c1694417..bf96a82874 100644 --- a/spec/ruby/core/string/modulo_spec.rb +++ b/spec/ruby/core/string/modulo_spec.rb @@ -368,8 +368,16 @@ describe "String#%" do ("%c" % 'A').should == "A" end - it "raises an exception for multiple character strings as argument for %c" do - -> { "%c" % 'AA' }.should raise_error(ArgumentError) + ruby_version_is ""..."3.2" do + it "raises an exception for multiple character strings as argument for %c" do + -> { "%c" % 'AA' }.should raise_error(ArgumentError) + end + end + + ruby_version_is "3.2" do + it "supports only the first character as argument for %c" do + ("%c" % 'AA').should == "A" + end end it "calls to_str on argument for %c formats" do @@ -441,12 +441,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) tmp = rb_check_string_type(val); if (!NIL_P(tmp)) { - rb_encoding *valenc = rb_enc_get(tmp); - if (rb_enc_strlen(RSTRING_PTR(tmp), RSTRING_END(tmp), valenc) != 1) { - rb_raise(rb_eArgError, "%%c requires a character"); - } - c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, valenc); - RB_GC_GUARD(tmp); + flags |= FPREC; + prec = 1; + str = tmp; + goto format_s1; } else { c = NUM2INT(val); @@ -488,6 +486,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) else { str = rb_obj_as_string(arg); } + format_s1: len = RSTRING_LEN(str); rb_str_set_len(result, blen); if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index 2c6fcee004..a50507a528 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -893,7 +893,7 @@ class TestM17N < Test::Unit::TestCase "%s%s" % [s("\xc2\xa1"), e("\xc2\xa1")] } - "%c" % "\u3042".encode('Windows-31J') + assert_equal("\u3042".encode('Windows-31J'), "%c" % "\u3042\u3044".encode('Windows-31J')) end def test_sprintf_p diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb index b05f4f3e44..618e67264a 100644 --- a/test/ruby/test_sprintf.rb +++ b/test/ruby/test_sprintf.rb @@ -362,7 +362,8 @@ class TestSprintf < Test::Unit::TestCase def test_char assert_equal("a", sprintf("%c", 97)) assert_equal("a", sprintf("%c", ?a)) - assert_raise(ArgumentError) { sprintf("%c", sprintf("%c%c", ?a, ?a)) } + assert_equal("a", sprintf("%c", "a")) + assert_equal("a", sprintf("%c", sprintf("%c%c", ?a, ?a))) assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%c", ?a)) assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%-1c", ?a)) assert_equal(" " * BSIZ + "a", sprintf("%#{ BSIZ + 1 }c", ?a)) |