mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[Bug #18955] format single character for %c
This commit is contained in:
parent
ce384ef5a9
commit
1ef49de834
Notes:
git
2022-08-20 03:57:47 +09:00
5 changed files with 38 additions and 18 deletions
|
@ -289,6 +289,7 @@ describe :kernel_sprintf, shared: true do
|
|||
@method.call("%c", "a").should == "a"
|
||||
end
|
||||
|
||||
ruby_version_is ""..."3.2" do
|
||||
it "raises ArgumentError if argument is a string of several characters" do
|
||||
-> {
|
||||
@method.call("%c", "abc")
|
||||
|
@ -300,6 +301,17 @@ describe :kernel_sprintf, shared: true do
|
|||
@method.call("%c", "")
|
||||
}.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
@method.call("%c", 1286).should == "Ԇ"
|
||||
|
|
|
@ -368,9 +368,17 @@ describe "String#%" do
|
|||
("%c" % 'A').should == "A"
|
||||
end
|
||||
|
||||
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
|
||||
obj = mock('A')
|
||||
|
|
11
sprintf.c
11
sprintf.c
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Reference in a new issue