From 2e8bede6eae5414628d50feb48eebbf7ec664726 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 19 Dec 2023 08:14:19 +0900 Subject: [PATCH] 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()`. --- mrbgems/mruby-io/src/io.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index 76a51c305..e70d2ab73 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -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); }