Skip to content

Commit

Permalink
buffer: clear out one line functions
Browse files Browse the repository at this point in the history
Buffer is fully exposed to the program, no need to rely on the
linker to optimize useless code.
  • Loading branch information
rnpnr committed Jan 12, 2025
1 parent abf6384 commit 0f31ef5
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 70 deletions.
18 changes: 1 addition & 17 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ void buffer_release(Buffer *buf) {
*buf = (Buffer){0};
}

void buffer_clear(Buffer *buf) {
buf->len = 0;
}

bool buffer_put(Buffer *buf, const void *data, size_t len) {
if (!buffer_reserve(buf, len))
return false;
Expand Down Expand Up @@ -140,7 +136,7 @@ bool buffer_appendf(Buffer *buf, const char *fmt, ...) {
}

bool buffer_printf(Buffer *buf, const char *fmt, ...) {
buffer_clear(buf);
buf->len = 0;
va_list ap;
va_start(ap, fmt);
bool ret = buffer_vappendf(buf, fmt, ap);
Expand All @@ -155,18 +151,6 @@ size_t buffer_length0(Buffer *buf) {
return len;
}

size_t buffer_length(Buffer *buf) {
return buf->len;
}

size_t buffer_capacity(Buffer *buf) {
return buf->size;
}

const char *buffer_content(Buffer *buf) {
return buf->data;
}

const char *buffer_content0(Buffer *buf) {
if (buf->len == 0 || !buffer_terminate(buf))
return "";
Expand Down
13 changes: 0 additions & 13 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ typedef struct {

/** Release all resources, reinitialize buffer. */
void buffer_release(Buffer*);
/** Set buffer length to zero, keep allocated memory. */
void buffer_clear(Buffer*);
/** Reserve space to store at least ``size`` bytes.*/
bool buffer_reserve(Buffer*, size_t size);
/** Reserve space for at least ``len`` *more* bytes. */
Expand Down Expand Up @@ -54,22 +52,11 @@ bool buffer_printf(Buffer*, const char *fmt, ...) __attribute__((format(printf,
bool buffer_appendf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
/** Return length of a buffer without trailing NUL byte. */
size_t buffer_length0(Buffer*);
/** Return length of a buffer including possible NUL byte. */
size_t buffer_length(Buffer*);
/** Return current maximal capacity in bytes of this buffer. */
size_t buffer_capacity(Buffer*);
/**
* Get pointer to buffer data.
* Guaranteed to return a NUL terminated string even if buffer is empty.
*/
const char *buffer_content0(Buffer*);
/**
* Get pointer to buffer data.
* @rst
* .. warning:: Might be NULL, if empty. Might not be NUL terminated.
* @endrst
*/
const char *buffer_content(Buffer*);
/**
* Borrow underlying buffer data.
* @rst
Expand Down
26 changes: 10 additions & 16 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1349,35 +1349,29 @@ static bool cmd_insert(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel
if (!win)
return false;
Buffer buf = text(vis, argv[1]);
size_t len = buffer_length(&buf);
char *data = buffer_move(&buf);
bool ret = sam_insert(win, sel, range->start, data, len, cmd->count.start);
bool ret = sam_insert(win, sel, range->start, buf.data, buf.len, cmd->count.start);
if (!ret)
free(data);
free(buf.data);
return ret;
}

static bool cmd_append(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
if (!win)
return false;
Buffer buf = text(vis, argv[1]);
size_t len = buffer_length(&buf);
char *data = buffer_move(&buf);
bool ret = sam_insert(win, sel, range->end, data, len, cmd->count.start);
bool ret = sam_insert(win, sel, range->end, buf.data, buf.len, cmd->count.start);
if (!ret)
free(data);
free(buf.data);
return ret;
}

static bool cmd_change(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
if (!win)
return false;
Buffer buf = text(vis, argv[1]);
size_t len = buffer_length(&buf);
char *data = buffer_move(&buf);
bool ret = sam_change(win, sel, range, data, len, cmd->count.start);
bool ret = sam_change(win, sel, range, buf.data, buf.len, cmd->count.start);
if (!ret)
free(data);
free(buf.data);
return ret;
}

Expand Down Expand Up @@ -1751,10 +1745,10 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel
if (vis->interrupted) {
vis_info_show(vis, "Command cancelled");
} else if (status == 0) {
size_t len = buffer_length(&bufout);
char *data = buffer_move(&bufout);
if (!sam_change(win, sel, range, data, len, 1))
free(data);
if (!sam_change(win, sel, range, bufout.data, bufout.len, 1)) {
free(bufout.data);
bufout.data = 0;
}
} else {
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
}
Expand Down
30 changes: 15 additions & 15 deletions test/core/buffer-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ int main(int argc, char *argv[]) {

plan_no_plan();

ok(buffer_content(&buf) == NULL && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Initialization");
ok(buffer_insert(&buf, 0, "foo", 0) && buffer_content(&buf) == NULL &&
buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Insert zero length data");
ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Initialization");
ok(buffer_insert(&buf, 0, "foo", 0) && buf.data == NULL &&
buf.len == 0 && buf.size == 0, "Insert zero length data");
ok(!buffer_insert0(&buf, 1, "foo"), "Insert string at invalid position");

ok(buffer_insert0(&buf, 0, "") && compare0(&buf, ""), "Insert empty string");
Expand All @@ -36,51 +36,51 @@ int main(int argc, char *argv[]) {
ok(buffer_append0(&buf, "baz") && compare0(&buf, "foobarbaz"), "Append string");

buffer_release(&buf);
ok(buf.data == NULL && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Release");
ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Release");

ok(buffer_insert(&buf, 0, "foo", 0) && compare(&buf, "", 0), "Insert zero length data");
ok(buffer_insert(&buf, 0, "foo", 3) && compare(&buf, "foo", 3), "Insert data at start");
ok(buffer_insert(&buf, 1, "l", 1) && compare(&buf, "floo", 4), "Insert data in middle");
ok(buffer_insert(&buf, 4, "r", 1) && compare(&buf, "floor", 5), "Insert data at end");

size_t cap = buffer_capacity(&buf);
buffer_clear(&buf);
ok(buf.data && buffer_length(&buf) == 0 && buffer_capacity(&buf) == cap, "Clear");
size_t cap = buf.size;
buf.len = 0;
ok(buf.data && buf.len == 0 && buf.size == cap, "Clear");

ok(buffer_put(&buf, "foo", 0) && compare(&buf, "", 0), "Put zero length data");
ok(buffer_put(&buf, "bar", 3) && compare(&buf, "bar", 3), "Put data");

ok(buffer_prepend(&buf, "foo\0", 4) && compare(&buf, "foo\0bar", 7), "Prepend data");
ok(buffer_append(&buf, "\0baz", 4) && compare(&buf, "foo\0bar\0baz", 11), "Append data");

ok(buffer_grow(&buf, cap+1) && compare(&buf, "foo\0bar\0baz", 11) && buffer_capacity(&buf) >= cap+1, "Grow");
ok(buffer_grow(&buf, cap+1) && compare(&buf, "foo\0bar\0baz", 11) && buf.size >= cap+1, "Grow");

const char *content = buffer_content(&buf);
const char *content = buf.data;
char *data = buffer_move(&buf);
ok(data == content && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0 && buffer_content(&buf) == NULL, "Move");
ok(buffer_append0(&buf, "foo") && buffer_content(&buf) != data, "Modify after move");
ok(data == content && buf.len == 0 && buf.size == 0 && buf.data == NULL, "Move");
ok(buffer_append0(&buf, "foo") && buf.data != data, "Modify after move");
free(data);

skip_if(TIS_INTERPRETER, 1, "vsnprintf not supported") {

ok(buffer_printf(&buf, "Test: %d\n", 42) && compare0(&buf, "Test: 42\n"), "Set formatted");
ok(buffer_printf(&buf, "%d\n", 42) && compare0(&buf, "42\n"), "Set formatted overwrite");
buffer_clear(&buf);
buf.len = 0;

ok(buffer_printf(&buf, "%s", "") && compare0(&buf, ""), "Set formatted empty string");
buffer_clear(&buf);
buf.len = 0;

bool append = true;
for (int i = 1; i <= 10; i++)
append &= buffer_appendf(&buf, "%d", i);
ok(append && compare0(&buf, "12345678910"), "Append formatted");
buffer_clear(&buf);
buf.len = 0;

append = true;
for (int i = 1; i <= 10; i++)
append &= buffer_appendf(&buf, "%s", "");
ok(append && compare0(&buf, ""), "Append formatted empty string");
buffer_clear(&buf);
buf.len = 0;
}

buffer_release(&buf);
Expand Down
4 changes: 2 additions & 2 deletions ui-terminal-vt100.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void cursor_visible(bool visible) {

static void ui_term_backend_blit(Ui *tui) {
Buffer *buf = tui->ctx;
buffer_clear(buf);
buf->len = 0;
CellAttr attr = CELL_ATTR_NORMAL;
CellColor fg = CELL_COLOR_DEFAULT, bg = CELL_COLOR_DEFAULT;
int w = tui->width, h = tui->height;
Expand Down Expand Up @@ -152,7 +152,7 @@ static void ui_term_backend_blit(Ui *tui) {
cell++;
}
}
output(buffer_content(buf), buffer_length0(buf));
output(buf->data, buffer_length0(buf));
}

static void ui_term_backend_clear(Ui *tui) { }
Expand Down
1 change: 0 additions & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ typedef struct {
/* a macro is just a sequence of symbolic keys as received from ui->getkey */
typedef Buffer Macro;
#define macro_release buffer_release
#define macro_reset buffer_clear
#define macro_append buffer_append0

typedef struct { /** collects all information until an operator is executed */
Expand Down
2 changes: 1 addition & 1 deletion vis-operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ bool vis_operator(Vis *vis, enum VisOperator id, ...) {
case VIS_OP_REPLACE:
{
Macro *macro = macro_get(vis, VIS_REG_DOT);
macro_reset(macro);
macro->len = 0;
macro_append(macro, va_arg(ap, char*));
vis->action.arg.s = macro->data;
break;
Expand Down
6 changes: 3 additions & 3 deletions vis-registers.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const char *register_slot_get(Vis *vis, Register *reg, size_t slot, size_t *len)
Buffer *buf = array_get(&reg->values, slot);
if (!buf)
return NULL;
buffer_clear(buf);
buf->len = 0;

if (id == VIS_REG_PRIMARY)
cmd[3] = "primary";
Expand Down Expand Up @@ -270,8 +270,8 @@ Array vis_register_get(Vis *vis, enum VisRegister id) {
for (size_t i = 0; i < len; i++) {
Buffer *buf = array_get(&reg->values, i);
TextString string = {
.data = buffer_content(buf),
.len = buffer_length(buf),
.data = buf->data,
.len = buf->len,
};
array_add(&data, &string);
}
Expand Down
4 changes: 2 additions & 2 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ void macro_operator_record(Vis *vis) {
if (vis->macro_operator)
return;
vis->macro_operator = macro_get(vis, VIS_MACRO_OPERATOR);
macro_reset(vis->macro_operator);
vis->macro_operator->len = 0;
}

void macro_operator_stop(Vis *vis) {
Expand All @@ -1370,7 +1370,7 @@ bool vis_macro_record(Vis *vis, enum VisRegister id) {
if (vis->recording || !macro)
return false;
if (!(VIS_REG_A <= id && id <= VIS_REG_Z))
macro_reset(macro);
macro->len = 0;
vis->recording = macro;
vis_event_emit(vis, VIS_EVENT_WIN_STATUS, vis->win);
return true;
Expand Down

0 comments on commit 0f31ef5

Please sign in to comment.