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.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-06-28 12:03:53 +09:00
parent 3554a41a54
commit f0abd4241f
2 changed files with 14 additions and 4 deletions
+13 -3
View File
@@ -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)) {
+1 -1
View File
@@ -75,7 +75,7 @@ assert("String#% with invalid chr") do
end
assert_raise TypeError do
"%c" % 0
"%c" % 0x80
end
ensure
class Fixnum