mruby-io: refactor write buffer preparation logic

Extract buffer adjustment logic from io_write into reusable helper
function io_prepare_write. This prepares for implementing io_puts
in C while maintaining consistency in write operations.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-16 12:10:44 +09:00
parent ee4d349f2e
commit 857a1b3a0d
+13 -5
View File
@@ -1100,13 +1100,12 @@ fd_write(mrb_state *mrb, int fd, mrb_value str)
return len;
}
static mrb_value
io_write(mrb_state *mrb, mrb_value io)
/* Helper function to prepare IO object for writing by adjusting buffer state */
static void
io_prepare_write(mrb_state *mrb, struct mrb_io *fptr)
{
struct mrb_io *fptr = io_get_write_fptr(mrb, io);
int fd = io_get_write_fd(fptr);
if (fptr->buf && fptr->buf->len > 0) {
int fd = io_get_write_fd(fptr);
off_t n;
/* get current position */
@@ -1117,6 +1116,15 @@ io_write(mrb_state *mrb, mrb_value io)
if (n == -1) mrb_sys_fail(mrb, "lseek(2)");
fptr->buf->start = fptr->buf->len = 0;
}
}
static mrb_value
io_write(mrb_state *mrb, mrb_value io)
{
struct mrb_io *fptr = io_get_write_fptr(mrb, io);
int fd = io_get_write_fd(fptr);
io_prepare_write(mrb, fptr);
mrb_int len = 0;
if (mrb_get_argc(mrb) == 1) {