mruby-io (io_read): small refactoring; ref #6118

If `outbuf` is `nil` we allocate a buffer string, if `outbuf` is a
string, we resize it to zero length. In the function `io_read`, this
condition is done twice, so we refactor out to `io_reset_outbuf()`.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-12-19 08:14:19 +09:00
parent 2e24c94873
commit 2e8bede6ea
+15 -15
View File
@@ -1684,6 +1684,19 @@ io_read_all(mrb_state *mrb, struct mrb_io *fptr, mrb_value outbuf)
}
}
static mrb_value
io_reset_outbuf(mrb_state *mrb, mrb_value outbuf, mrb_int len)
{
if (mrb_nil_p(outbuf)) {
outbuf = mrb_str_new(mrb, NULL, 0);
}
else {
mrb_str_modify(mrb, mrb_str_ptr(outbuf));
RSTR_SET_LEN(mrb_str_ptr(outbuf), 0);
}
return outbuf;
}
static mrb_value
io_read(mrb_state *mrb, mrb_value io)
{
@@ -1704,25 +1717,12 @@ io_read(mrb_state *mrb, mrb_value io)
mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative length %d given", length);
}
if (length == 0) {
if (mrb_nil_p(outbuf)) {
outbuf = mrb_str_new(mrb, NULL, 0);
}
else {
mrb_str_modify(mrb, mrb_str_ptr(outbuf));
RSTR_SET_LEN(mrb_str_ptr(outbuf), 0);
}
return outbuf;
return io_reset_outbuf(mrb, outbuf, 0);
}
}
}
if (mrb_nil_p(outbuf)) {
outbuf = mrb_str_new_capa(mrb, MRB_IO_BUF_SIZE);
}
else {
mrb_str_modify(mrb, mrb_str_ptr(outbuf));
RSTR_SET_LEN(mrb_str_ptr(outbuf), 0);
}
outbuf = io_reset_outbuf(mrb, outbuf, MRB_IO_BUF_SIZE);
if (!length_given) { /* read as much as possible */
return io_read_all(mrb, fptr, outbuf);
}