<commit_before>#include "text.h"

#include <stdlib.h>
#include <string.h>

#define GAPSIZE(t) (t->text_start - t->gap_start)
#define NBYTES(t) ((t->size - t->text_start) + (t->gap_start))

#define RESIZE_BY 20

Text *new_document(void)
{
	Text *beg = calloc(1, sizeof(Text));
	return beg;
}

void resize_gap(Text *text, size_t new_gap)
{
	size_t gap = GAPSIZE(text);

	// resize_gap can't downsize
	if (new_gap <= gap)
		return;

	size_t expand_by = new_gap - gap;
	size_t old_size = text->size;
	size_t new_size = text->size + expand_by;
	text->size = new_size;
	size_t old_start = text->text_start;
	text->text_start += expand_by;
	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, new_size);
	memcpy(text->buf, old_buf, text->gap_start);
	memcpy(text->buf + text->text_start, old_buf + old_start, old_size - old_start);
	free(old_buf);
}

void add_character(Text *text, char c)
{
	size_t freespace = GAPSIZE(text);
	if (freespace <= 1)
		resize_gap(text, RESIZE_BY);
	text->buf[text->gap_start++] = c;
}

void add_string(Text *text, char *buf, size_t len)
{
	size_t freespace = GAPSIZE(text);
	if (freespace < len + 1)
		resize_gap(text, RESIZE_BY + len);
	memcpy(text->buf + text->gap_start, buf, len);
	text->gap_start += len;
}

void move_cursor(size_t pos)
{
	;
}

void move_cursor_bol(Text *text)
{
	int i = text->gap_start-1;
	for (; i >= 0; i = text->gap_start-1) {
		if (text->buf[i] == '\n') break;
		move_left(text);
	}
}

void move_cursor_eol(Text *text)
{
	int i = text->text_start;
	for (; text->size - text->text_start > 0; i = text->text_start) {
		if (text->buf[i] == '\n') break;
		move_right(text);
	}
}

void move_cursor_to_beg(Text *text)
{
	size_t gap = GAPSIZE(text);

	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, text->size);
	memcpy(text->buf + gap, old_buf, text->gap_start);
	memcpy(text->buf + gap + text->gap_start, old_buf + text->text_start, text->size - text->text_start);
	text->gap_start = 0;
	text->text_start = gap;

	free(old_buf);
}

void move_cursor_to_end(Text *text)
{
	size_t gap = GAPSIZE(text);

	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, text->size + 1);
	memcpy(text->buf, old_buf, text->gap_start);
	memcpy(text->buf + text->gap_start, old_buf + text->text_start, text->size - text->text_start);<commit_msg>Increase buffer calloc size in one call<commit_after>#include "text.h"

#include <stdlib.h>
#include <string.h>

#define GAPSIZE(t) (t->text_start - t->gap_start)
#define NBYTES(t) ((t->size - t->text_start) + (t->gap_start))

#define RESIZE_BY 20

Text *new_document(void)
{
	Text *beg = calloc(1, sizeof(Text));
	return beg;
}

void resize_gap(Text *text, size_t new_gap)
{
	size_t gap = GAPSIZE(text);

	// resize_gap can't downsize
	if (new_gap <= gap)
		return;

	size_t expand_by = new_gap - gap;
	size_t old_size = text->size;
	size_t new_size = text->size + expand_by;
	text->size = new_size;
	size_t old_start = text->text_start;
	text->text_start += expand_by;
	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, new_size);
	memcpy(text->buf, old_buf, text->gap_start);
	memcpy(text->buf + text->text_start, old_buf + old_start, old_size - old_start);
	free(old_buf);
}

void add_character(Text *text, char c)
{
	size_t freespace = GAPSIZE(text);
	if (freespace <= 1)
		resize_gap(text, RESIZE_BY);
	text->buf[text->gap_start++] = c;
}

void add_string(Text *text, char *buf, size_t len)
{
	size_t freespace = GAPSIZE(text);
	if (freespace < len + 1)
		resize_gap(text, RESIZE_BY + len);
	memcpy(text->buf + text->gap_start, buf, len);
	text->gap_start += len;
}

void move_cursor(size_t pos)
{
	;
}

void move_cursor_bol(Text *text)
{
	int i = text->gap_start-1;
	for (; i >= 0; i = text->gap_start-1) {
		if (text->buf[i] == '\n') break;
		move_left(text);
	}
}

void move_cursor_eol(Text *text)
{
	int i = text->text_start;
	for (; text->size - text->text_start > 0; i = text->text_start) {
		if (text->buf[i] == '\n') break;
		move_right(text);
	}
}

void move_cursor_to_beg(Text *text)
{
	size_t gap = GAPSIZE(text);

	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, text->size + 1);
	memcpy(text->buf + gap, old_buf, text->gap_start);
	memcpy(text->buf + gap + text->gap_start, old_buf + text->text_start, text->size - text->text_start);
	text->gap_start = 0;
	text->text_start = gap;

	free(old_buf);
}

void move_cursor_to_end(Text *text)
{
	size_t gap = GAPSIZE(text);

	char *old_buf = text->buf;
	text->buf = (char*)calloc(1, text->size + 1);
	memcpy(text->buf, old_buf, text->gap_start);
	memcpy(text->buf + text->gap_start, old_buf + text->text_start, text->size - text->text_start);
{'fraction_non_alphanumeric': 0.17497831743278405, 'fraction_numerical': 0.006287944492627927, 'mean_word_length': 4.459171597633136, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 113, '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': '2377947', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1918, 'n_words': 404}