From 888d4c640760477cb80924fe9f244dd4ab231ad8 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 29 Sep 2019 21:58:42 -0400 Subject: [PATCH] check-in partial refactor to new style --- src/lib/buf.c | 78 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/src/lib/buf.c b/src/lib/buf.c index ce45ae8..98a84bb 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -9,7 +9,8 @@ #include "config.h" #ifndef NDEBUG -static bool buf_valid(Buf* buf) { +static bool buf_valid(Buf* buf) +{ return ( (buf->bufsize > 0) && (buf->bufstart != NULL) @@ -26,31 +27,40 @@ static bool buf_valid(Buf* buf) { } #endif -static Sel selswap(Sel sel) { +static Sel selswap(Sel sel) +{ size_t off = sel.beg; - sel.beg = sel.end, sel.end = off; + sel.beg = sel.end; + sel.end = off; return sel; } -static Sel selget(Buf* buf) { +static Sel selget(Buf* buf) +{ Sel sel = buf->selection; return (sel.end < sel.beg ? selswap(sel) : sel); } -//static void selset(Buf* buf, Sel sel) { +//static void selset(Buf* buf, Sel sel) +//{ // buf->selection = (sel.end < sel.beg ? selswap(sel) : sel); //} /* Creation, Resizing, Loading, and Saving ******************************************************************************/ -static size_t pagealign(size_t sz) { - size_t pgsize = sysconf(_SC_PAGE_SIZE), alignmask = pgsize - 1; +static size_t pagealign(size_t sz) +{ + size_t pgsize = sysconf(_SC_PAGE_SIZE); + size_t alignmask = (pgsize - 1); if (sz & alignmask) + { sz += pgsize - (sz & alignmask); + } return sz; } -static void buf_resize(Buf* buf, size_t sz) { +static void buf_resize(Buf* buf, size_t sz) +{ /* allocate the new buffer and gap */ Buf copy = *buf; copy.bufsize = sz; @@ -58,41 +68,68 @@ static void buf_resize(Buf* buf, size_t sz) { copy.bufend = copy.bufstart + copy.bufsize; copy.gapstart = copy.bufstart; copy.gapend = copy.bufend; + /* copy the data from the old buffer to the new one */ for (char* curr = buf->bufstart; curr < buf->gapstart; curr++) + { *(copy.gapstart++) = *(curr); + } for (char* curr = buf->gapend; curr < buf->bufend; curr++) + { *(copy.gapstart++) = *(curr); + } + /* free the buffer and commit the changes */ free(buf->bufstart); memcpy(buf, ©, sizeof(Buf)); } -static void buf_syncgap(Buf* buf, size_t off) { +static void buf_syncgap(Buf* buf, size_t off) +{ assert(off <= buf_end(buf)); /* If the buffer is full, resize it before syncing */ if (0 == (buf->gapend - buf->gapstart)) + { buf_resize(buf, buf->bufsize << 1); + } + /* Move the gap to the desired offset */ char* newpos = (buf->bufstart + off); if (newpos < buf->gapstart) + { while (newpos < buf->gapstart) + { *(--buf->gapend) = *(--buf->gapstart); + } + } else + { while (newpos > buf->gapstart) + { *(buf->gapstart++) = *(buf->gapend++); + } + } } -static char getb(Buf* buf, size_t off) { - if (off >= buf_end(buf)) return '\n'; // TODO: get rid of this hack +static char getb(Buf* buf, size_t off) +{ + if (off >= buf_end(buf)) + { + return '\n'; // TODO: get rid of this hack + } size_t bsz = (buf->gapstart - buf->bufstart); if (off < bsz) + { return *(buf->bufstart + off); + } else + { return *(buf->gapend + (off - bsz)); + } } -static void putb(Buf* buf, char b, Sel* p_sel) { +static void putb(Buf* buf, char b, Sel* p_sel) +{ buf_syncgap(buf, p_sel->end); *(buf->gapstart++) = b; p_sel->end = p_sel->end + 1u; @@ -100,12 +137,19 @@ static void putb(Buf* buf, char b, Sel* p_sel) { buf->status = MODIFIED; } -static void putch(Buf* buf, char b, Sel* p_sel) { - if (b == '\r') return; - if (b == '\n' && DosLineFeed) { +static void putch(Buf* buf, char b, Sel* p_sel) +{ + if (b == '\r') + { + return; + } + if (b == '\n' && DosLineFeed) + { putb(buf, '\r', p_sel); putb(buf, '\n', p_sel); - } else { + } + else + { putb(buf, b, p_sel); } } @@ -751,6 +795,7 @@ void buf_selln(Buf* buf) { } void buf_selclr(Buf* buf, int dir) { + require(buf != NULL); Sel sel = selget(buf); if (dir > 0) sel.beg = sel.end; @@ -760,6 +805,7 @@ void buf_selclr(Buf* buf, int dir) { } bool buf_insel(Buf* buf, size_t off) { + require(buf != NULL); return (off >= buf_selbeg(buf) && off < buf_selend(buf)); } -- 2.52.0