[Bug #18955] format single character for %c

This commit is contained in:
Nobuyoshi Nakada 2022-08-20 01:04:02 +09:00
parent ce384ef5a9
commit 1ef49de834
Notes: git 2022-08-20 03:57:47 +09:00
5 changed files with 38 additions and 18 deletions

View file

@ -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) {