#include "config.h"
#ifndef NDEBUG
-static bool buf_valid(Buf* buf) {
+static bool buf_valid(Buf* buf)
+{
return (
(buf->bufsize > 0)
&& (buf->bufstart != NULL)
}
#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;
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;
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);
}
}
}
void buf_selclr(Buf* buf, int dir) {
+ require(buf != NULL);
Sel sel = selget(buf);
if (dir > 0)
sel.beg = sel.end;
}
bool buf_insel(Buf* buf, size_t off) {
+ require(buf != NULL);
return (off >= buf_selbeg(buf) && off < buf_selend(buf));
}