diff options
author | Nobuyoshi Nakada <[email protected]> | 2022-08-20 01:04:02 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2022-08-20 03:57:13 +0900 |
commit | 1ef49de83483e6f78bfe9c795a473ccfb29db150 () | |
tree | f6c48f28801763a95372f143b2cdea5564b96336 | |
parent | ce384ef5a95b809f248e089c1608e60753dabe45 (diff) |
[Bug #18955] format single character for `%c`
Notes: Merged: https://.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
@@ -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) end - it "raises ArgumentError if argument is an empty string" do - -> { - @method.call("%c", "") - }.should raise_error(ArgumentError) end it "supports Unicode characters" do @@ -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) 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); } 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); } len = RSTRING_LEN(str); rb_str_set_len(result, blen); if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) { @@ -893,7 +893,7 @@ class TestM17N < Test::Unit::TestCase "%s%s" % [s("\xc2\xa1"), e("\xc2\xa1")] } - "%c" % "\u3042".encode('Windows-31J') end def test_sprintf_p @@ -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(" " * (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)) |