Skip to content

Commit

Permalink
buffer: delete pointless buffer_init function
Browse files Browse the repository at this point in the history
lets not make the code harder to read for no reason
  • Loading branch information
rnpnr committed Jan 12, 2025
1 parent 72c26fc commit abf6384
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 48 deletions.
8 changes: 2 additions & 6 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#define BUFFER_SIZE 1024
#endif

void buffer_init(Buffer *buf) {
memset(buf, 0, sizeof *buf);
}

bool buffer_reserve(Buffer *buf, size_t size) {
/* ensure minimal buffer size, to avoid repeated realloc(3) calls */
if (size < BUFFER_SIZE)
Expand Down Expand Up @@ -45,7 +41,7 @@ void buffer_release(Buffer *buf) {
if (!buf)
return;
free(buf->data);
buffer_init(buf);
*buf = (Buffer){0};
}

void buffer_clear(Buffer *buf) {
Expand Down Expand Up @@ -179,7 +175,7 @@ const char *buffer_content0(Buffer *buf) {

char *buffer_move(Buffer *buf) {
char *data = buf->data;
buffer_init(buf);
*buf = (Buffer){0};
return data;
}

Expand Down
2 changes: 0 additions & 2 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ typedef struct {
size_t size; /**< Maximal capacity of the buffer. */
} Buffer;

/** Initialize a Buffer object. */
void buffer_init(Buffer*);
/** Release all resources, reinitialize buffer. */
void buffer_release(Buffer*);
/** Set buffer length to zero, keep allocated memory. */
Expand Down
5 changes: 2 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2154,9 +2154,8 @@ static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
if (!grapheme)
return keys;
Buffer info;
buffer_init(&info);
mbstate_t ps = { 0 };
Buffer info = {0};
mbstate_t ps = {0};
Iterator it = text_iterator_get(txt, start);
for (size_t pos = start; it.pos < end; pos = it.pos) {
if (!text_iterator_codepoint_next(&it, NULL)) {
Expand Down
19 changes: 6 additions & 13 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ static void skip_spaces(const char **s) {
}

static char *parse_until(const char **s, const char *until, const char *escchars, int type){
Buffer buf;
buffer_init(&buf);
Buffer buf = {0};
size_t len = strlen(until);
bool escaped = false;

Expand Down Expand Up @@ -633,8 +632,7 @@ static char *parse_text(const char **s, Count *count) {
return (!text && *s != before) ? strdup("") : text;
}

Buffer buf;
buffer_init(&buf);
Buffer buf = {0};
const char *start = *s + 1;
bool dot = false;

Expand Down Expand Up @@ -677,8 +675,7 @@ static bool valid_cmdname(const char *s) {
}

static char *parse_cmdname(const char **s) {
Buffer buf;
buffer_init(&buf);
Buffer buf = {0};

skip_spaces(s);
while (valid_cmdname(*s))
Expand Down Expand Up @@ -1313,8 +1310,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {

/* process text input, substitute register content for backreferences etc. */
Buffer text(Vis *vis, const char *text) {
Buffer buf;
buffer_init(&buf);
Buffer buf = {0};
for (size_t len = strcspn(text, "\\&"); *text; len = strcspn(++text, "\\&")) {
buffer_append(&buf, text, len);
text += len;
Expand Down Expand Up @@ -1747,9 +1743,7 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel
if (!win)
return false;

Buffer bufout, buferr;
buffer_init(&bufout);
buffer_init(&buferr);
Buffer bufout = {0}, buferr = {0};

int status = vis_pipe(vis, win->file, range, &argv[1], &bufout, read_into_buffer, &buferr,
read_into_buffer, false);
Expand Down Expand Up @@ -1789,8 +1783,7 @@ static bool cmd_pipein(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel
static bool cmd_pipeout(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
if (!win)
return false;
Buffer buferr;
buffer_init(&buferr);
Buffer buferr = {0};

int status = vis_pipe(vis, win->file, range, (const char*[]){ argv[1], NULL }, NULL, NULL,
&buferr, read_into_buffer, false);
Expand Down
3 changes: 1 addition & 2 deletions test/core/buffer-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ static bool compare0(Buffer *buf, const char *data) {
}

int main(int argc, char *argv[]) {
Buffer buf;
Buffer buf = {0};

plan_no_plan();

buffer_init(&buf);
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");
Expand Down
3 changes: 1 addition & 2 deletions test/fuzz/buffer-fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ static Cmd commands[] = {

int main(int argc, char *argv[]) {
char line[BUFSIZ];
Buffer buf;
buffer_init(&buf);
Buffer buf = {0};

for (;;) {
printf("> ");
Expand Down
1 change: 0 additions & 1 deletion ui-terminal-vt100.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ static bool ui_backend_init(Ui *ui) {
Buffer *buf = calloc(1, sizeof(Buffer));
if (!buf)
return false;
buffer_init(buf);
ui->ctx = buf;
return true;
}
Expand Down
5 changes: 1 addition & 4 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,7 @@ static const char *file_open_dialog(Vis *vis, const char *pattern) {
if (!is_file_pattern(pattern))
return pattern;

Buffer bufcmd, bufout, buferr;
buffer_init(&bufcmd);
buffer_init(&bufout);
buffer_init(&buferr);
Buffer bufcmd = {0}, bufout = {0}, buferr = {0};

if (!buffer_put0(&bufcmd, VIS_OPEN " ") || !buffer_append0(&bufcmd, pattern ? pattern : ""))
return NULL;
Expand Down
1 change: 0 additions & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ typedef struct {

/* a macro is just a sequence of symbolic keys as received from ui->getkey */
typedef Buffer Macro;
#define macro_init buffer_init
#define macro_release buffer_release
#define macro_reset buffer_clear
#define macro_append buffer_append0
Expand Down
12 changes: 4 additions & 8 deletions vis-registers.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ static Buffer *register_buffer(Register *reg, size_t slot) {
return buf;
if (array_resize(&reg->values, slot) && (buf = array_get(&reg->values, slot)))
return buf;
Buffer new;
buffer_init(&new);
Buffer new = {0};
if (!array_add(&reg->values, &new))
return NULL;
size_t capacity = array_capacity(&reg->values);
Expand All @@ -22,8 +21,7 @@ static Buffer *register_buffer(Register *reg, size_t slot) {
}

bool register_init(Register *reg) {
Buffer buf;
buffer_init(&buf);
Buffer buf = {0};
array_init_sized(&reg->values, sizeof(Buffer));
return array_add(&reg->values, &buf);
}
Expand Down Expand Up @@ -63,10 +61,9 @@ const char *register_slot_get(Vis *vis, Register *reg, size_t slot, size_t *len)
}
case REGISTER_CLIPBOARD:
{
Buffer buferr;
Buffer buferr = {0};
enum VisRegister id = reg - vis->registers;
const char *cmd[] = { VIS_CLIPBOARD, "--paste", "--selection", NULL, NULL };
buffer_init(&buferr);
Buffer *buf = array_get(&reg->values, slot);
if (!buf)
return NULL;
Expand Down Expand Up @@ -151,10 +148,9 @@ bool register_slot_put_range(Vis *vis, Register *reg, size_t slot, Text *txt, Fi
}
case REGISTER_CLIPBOARD:
{
Buffer buferr;
Buffer buferr = {0};
const char *cmd[] = { VIS_CLIPBOARD, "--copy", "--selection", NULL, NULL };
enum VisRegister id = reg - vis->registers;
buffer_init(&buferr);

if (id == VIS_REG_PRIMARY)
cmd[3] = "primary";
Expand Down
9 changes: 3 additions & 6 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ Vis *vis_new(void) {
array_init(&vis->bindings);
array_init(&vis->actions_user);
action_reset(&vis->action);
buffer_init(&vis->input_queue);
vis->input_queue = (Buffer){0};
if (!(vis->command_file = file_new_internal(vis, NULL)))
goto err;
if (!(vis->search_file = file_new_internal(vis, NULL)))
Expand Down Expand Up @@ -1167,8 +1167,7 @@ static void vis_keys_process(Vis *vis, size_t pos) {
void vis_keys_feed(Vis *vis, const char *input) {
if (!input)
return;
Macro macro;
macro_init(&macro);
Macro macro = {0};
if (!macro_append(&macro, input))
return;
/* use internal function, to keep Lua based tests which use undo points working */
Expand Down Expand Up @@ -1855,9 +1854,7 @@ int vis_pipe_buf(Vis *vis, const char* buf, const char *argv[],
}

static int _vis_pipe_collect(Vis *vis, File *file, Filerange *range, const char* buf, const char *argv[], char **out, char **err, bool fullscreen) {
Buffer bufout, buferr;
buffer_init(&bufout);
buffer_init(&buferr);
Buffer bufout = {0}, buferr = {0};
int status = _vis_pipe(vis, file, range, buf, argv,
&bufout, out ? read_into_buffer : NULL,
&buferr, err ? read_into_buffer : NULL,
Expand Down

0 comments on commit abf6384

Please sign in to comment.