Avoid `mrb_funcall()` unless absolutely necessary; ref #3722

As a result, `#chr` is not called for ch < 0x80, so we need to
update the "invalid chr" test.
parent 3554a41a
......@@ -701,17 +701,27 @@ retry:
tmp = mrb_check_string_type(mrb, val);
if (!mrb_nil_p(tmp)) {
if (mrb_fixnum(mrb_funcall(mrb, tmp, "size", 0)) != 1 ) {
if (RSTRING_LEN(tmp) != 1) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "%c requires a character");
}
}
else if (mrb_fixnum_p(val)) {
tmp = mrb_funcall(mrb, val, "chr", 0);
mrb_int n = mrb_fixnum(val);
if (n < 0x80) {
char buf[1];
buf[0] = (char)n;
tmp = mrb_str_new(mrb, buf, 1);
}
else {
tmp = mrb_funcall(mrb, val, "chr", 0);
mrb_check_type(mrb, tmp, MRB_TT_STRING);
}
}
else {
mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid character");
}
mrb_check_type(mrb, tmp, MRB_TT_STRING);
c = RSTRING_PTR(tmp);
n = RSTRING_LEN(tmp);
if (!(flags & FWIDTH)) {
......
......@@ -75,7 +75,7 @@ assert("String#% with invalid chr") do
end
assert_raise TypeError do
"%c" % 0
"%c" % 0x80
end
ensure
class Fixnum
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment