Check the room including the space to fill

This commit is contained in:
Nobuyoshi Nakada 2022-08-21 20:06:39 +09:00
parent 0cc989696e
commit b043dd9c5d
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6

View File

@ -460,14 +460,16 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
blen += n; blen += n;
} }
else if ((flags & FMINUS)) { else if ((flags & FMINUS)) {
CHECK(n); --width;
CHECK(n + (width > 0 ? width : 0));
rb_enc_mbcput(c, &buf[blen], enc); rb_enc_mbcput(c, &buf[blen], enc);
blen += n; blen += n;
if (width > 1) FILL(' ', width-1); if (width > 0) FILL_(' ', width);
} }
else { else {
if (width > 1) FILL(' ', width-1); --width;
CHECK(n); CHECK(n + (width > 0 ? width : 0));
if (width > 0) FILL_(' ', width);
rb_enc_mbcput(c, &buf[blen], enc); rb_enc_mbcput(c, &buf[blen], enc);
blen += n; blen += n;
} }
@ -512,16 +514,16 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
/* need to adjust multi-byte string pos */ /* need to adjust multi-byte string pos */
if ((flags&FWIDTH) && (width > slen)) { if ((flags&FWIDTH) && (width > slen)) {
width -= (int)slen; width -= (int)slen;
CHECK(len + width);
if (!(flags&FMINUS)) { if (!(flags&FMINUS)) {
FILL(' ', width); FILL_(' ', width);
width = 0; width = 0;
} }
CHECK(len);
memcpy(&buf[blen], RSTRING_PTR(str), len); memcpy(&buf[blen], RSTRING_PTR(str), len);
RB_GC_GUARD(str); RB_GC_GUARD(str);
blen += len; blen += len;
if (flags&FMINUS) { if (flags&FMINUS) {
FILL(' ', width); FILL_(' ', width);
} }
rb_enc_associate(result, enc); rb_enc_associate(result, enc);
break; break;