<commit_before>#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <cmd.h>
#include <fd.h>
#include <set.h>
#include <txt.h>
#include <util.h>

static int cmd_insert();
static int cmd_print();
static int cmd_quit();

static size_t cursor[2] = {0, 0};

#define cmd(n, f, a) { .node = {{.key = n}}, .fun = f, .arg = a }

struct command commands[] = {
	cmd("q", cmd_quit, 0),
	cmd("i", cmd_insert, cursor),
	cmd("p", cmd_print, cursor),
};

int
cmd_insert(struct edna *edna, size_t *cursor)
{
	struct read ln[1]={0};
	int err;

	while (true) {
		err = fd_read(ln, 0);
		if (err == -1) return 0;
		if (err) return err;

		if (!ln->length) {
			return 0;
		}

		if (ln->length == 2 && !memcmp(ln->buffer, ".\n", 2)) {
			free(ln->buffer);
			return 0;
		}

		err = edna_text_insert(edna, cursor[0] + cursor[1],
		                       ln->buffer, ln->length);
		if (err) {
			free(ln->buffer);
			return 0;
		}

		cursor[0] += cursor[1];
		cursor[1] = ln->length;
	}
}

int
cmd_quit()
{
	return -1;
}

int
cmd_print(struct edna *edna, size_t *cursor)
{
	struct piece *links[2];
	size_t end = cursor[0] + cursor[1];
	size_t off = cursor[0];
	size_t min;

	links[0] = edna->chain, links[1] = 0;
	text_walk(links, cursor[0]);

	while (off < end) {
		min = umin(links[0]->length, end - off);
		write(1, links[0]->buffer, min);
		off += links[0]->length;

		text_step(links);
	}

	return 0;
}
<commit_msg>style<commit_after>#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <cmd.h>
#include <fd.h>
#include <set.h>
#include <txt.h>
#include <util.h>

static int cmd_insert();
static int cmd_print();
static int cmd_quit();

static size_t cursor[2] = {0, 0};

#define cmd(n, f, a) { .node = {{.key = n}}, .fun = f, .arg = a }

struct command commands[] = {
	cmd("q", cmd_quit, 0),
	cmd("i", cmd_insert, cursor),
	cmd("p", cmd_print, cursor),
};

int
cmd_insert(struct edna *edna, size_t *cursor)
{
	struct read ln[1]={0};
	int err;

 again:
	err = fd_read(ln, 0);
	if (err == -1) return 0;
	if (err) return err;

	if (!ln->length) {
		return 0;
	}

	if (ln->length == 2 && !memcmp(ln->buffer, ".\n", 2)) {
		free(ln->buffer);
		return 0;
	}

	err = edna_text_insert(edna, cursor[0] + cursor[1],
	                       ln->buffer, ln->length);
	if (err) {
		free(ln->buffer);
		return 0;
	}

	cursor[0] += cursor[1];
	cursor[1] = ln->length;

	goto again;
}

int
cmd_quit()
{
	return -1;
}

int
cmd_print(struct edna *edna, size_t *cursor)
{
	struct piece *links[2];
	size_t end = cursor[0] + cursor[1];
	size_t off = cursor[0];
	size_t min;

	links[0] = edna->chain, links[1] = 0;
	text_walk(links, cursor[0]);

	while (off < end) {
		min = umin(links[0]->length, end - off);
		write(1, links[0]->buffer, min);
		off += links[0]->length;

		text_step(links);
	}

	return 0;
}

{'fraction_non_alphanumeric': 0.202602884277172, 'fraction_numerical': 0.02321491382342596, 'mean_word_length': 3.423017107309487, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989932', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1202, 'n_words': 303}