[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
@ -289,16 +289,28 @@ describe :kernel_sprintf, shared: true do
|
|||||||
@method.call("%c", "a").should == "a"
|
@method.call("%c", "a").should == "a"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises ArgumentError if argument is a string of several characters" do
|
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)
|
@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
|
end
|
||||||
|
|
||||||
it "raises ArgumentError if argument is an empty string" do
|
ruby_version_is "3.2" do
|
||||||
-> {
|
it "displays only the first character if argument is a string of several characters" do
|
||||||
@method.call("%c", "")
|
@method.call("%c", "abc").should == "a"
|
||||||
}.should raise_error(ArgumentError)
|
end
|
||||||
|
|
||||||
|
it "displays no characters if argument is an empty string" do
|
||||||
|
@method.call("%c", "").should == ""
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "supports Unicode characters" do
|
it "supports Unicode characters" do
|
||||||
|
@ -368,8 +368,16 @@ describe "String#%" do
|
|||||||
("%c" % 'A').should == "A"
|
("%c" % 'A').should == "A"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an exception for multiple character strings as argument for %c" do
|
ruby_version_is ""..."3.2" do
|
||||||
-> { "%c" % 'AA' }.should raise_error(ArgumentError)
|
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
|
end
|
||||||
|
|
||||||
it "calls to_str on argument for %c formats" do
|
it "calls to_str on argument for %c formats" do
|
||||||
|
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);
|
tmp = rb_check_string_type(val);
|
||||||
if (!NIL_P(tmp)) {
|
if (!NIL_P(tmp)) {
|
||||||
rb_encoding *valenc = rb_enc_get(tmp);
|
flags |= FPREC;
|
||||||
if (rb_enc_strlen(RSTRING_PTR(tmp), RSTRING_END(tmp), valenc) != 1) {
|
prec = 1;
|
||||||
rb_raise(rb_eArgError, "%%c requires a character");
|
str = tmp;
|
||||||
}
|
goto format_s1;
|
||||||
c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, valenc);
|
|
||||||
RB_GC_GUARD(tmp);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
c = NUM2INT(val);
|
c = NUM2INT(val);
|
||||||
@ -488,6 +486,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||||||
else {
|
else {
|
||||||
str = rb_obj_as_string(arg);
|
str = rb_obj_as_string(arg);
|
||||||
}
|
}
|
||||||
|
format_s1:
|
||||||
len = RSTRING_LEN(str);
|
len = RSTRING_LEN(str);
|
||||||
rb_str_set_len(result, blen);
|
rb_str_set_len(result, blen);
|
||||||
if (coderange != ENC_CODERANGE_BROKEN && scanned < 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")]
|
"%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
|
end
|
||||||
|
|
||||||
def test_sprintf_p
|
def test_sprintf_p
|
||||||
|
@ -362,7 +362,8 @@ class TestSprintf < Test::Unit::TestCase
|
|||||||
def test_char
|
def test_char
|
||||||
assert_equal("a", sprintf("%c", 97))
|
assert_equal("a", sprintf("%c", 97))
|
||||||
assert_equal("a", sprintf("%c", ?a))
|
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) + "%c", ?a))
|
||||||
assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%-1c", ?a))
|
assert_equal(" " * (BSIZ - 1) + "a", sprintf(" " * (BSIZ - 1) + "%-1c", ?a))
|
||||||
assert_equal(" " * BSIZ + "a", sprintf("%#{ BSIZ + 1 }c", ?a))
|
assert_equal(" " * BSIZ + "a", sprintf("%#{ BSIZ + 1 }c", ?a))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user