From f4b97be1f12d1e212218a4c4e152fb5ca76b7f8c Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 3 Jul 2024 17:50:04 -0600 Subject: [PATCH] dj(1): iron out Io_bufxapp --- src/dj.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/dj.c b/src/dj.c index 96085e3..3421292 100644 --- a/src/dj.c +++ b/src/dj.c @@ -75,25 +75,6 @@ Io_bufrpad(struct Io *io, int padding){ return io; } -/* Copies from the buffer in src as much as possible to the free space in the - * dest buffer, removing the copied units from src and permuting the remaining - * units in the src buffer to the start of the buffer, modifying both the src - * and dest bufuse and returning dest. */ -static struct Io* -Io_bufxapp(struct Io *dest, struct Io *src){ - int n; - - assert(dest != NULL && src != NULL); - - n = MIN(src->bufuse, dest->bs - dest->bufuse); - memcpy(dest->buf + dest->bufuse, src->buf, n); - dest->bufuse += n; - memmove(src->buf, src->buf + n, src->bs - n); - src->bufuse -= n; - - return dest; -} - /* Copies from the buffer in src to the buffer in dest no more than n units, * removing the copied units from src and permuting the remaining units in the * src buffer to the start of the buffer, modifying both the src and dest @@ -338,7 +319,16 @@ int main(int argc, char *argv[]){ int t; if(io[1].bs > io[0].bs){ - Io_bufxapp(&io[1], &io[0]); + int n; + + /* copy from ibuf as much as possible to the obuf */ + memcpy(io[1].buf + io[1].bufuse, io[0].buf, + (n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))); + io[1].bufuse += n; + /* permute out the copied units */ + memmove(io[0].buf, io[0].buf + n, io[0].bs - n); + io[0].bufuse -= n; + if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) continue; /* we could write more */ }else