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

#include "buf.h"

buf* bufNew(const char *init) {
	buf* buffer;
	size_t initlen = strlen(init);
	buffer = malloc(sizeof(struct buf) + initlen + 1);
	if (buffer == NULL) return NULL;
	buffer->len = 0;
	buffer->free = initlen;
	buffer->data[buffer->len] = '\0';
	buffer = bufCat(buffer, init);
	return buffer;
}

void bufRelease(Buf buf) {
	if (buf == NULL) return;
	free(buf);
}

buf* bufCat(buf* buffer, const char *add) {
	size_t addlen = strlen(add);
	if (addlen == 0) return buffer;
	if (addlen > buffer->free) {
		size_t newlen = buffer->len + addlen;
		if (newlen < BUF_MAX_PREALLOC) newlen *= 2;
		else newlen += BUF_MAX_PREALLOC;
		buf* nbuffer = realloc(buffer, sizeof(struct buf)+ newlen + 1);
		if (nbuffer == NULL) return buffer;
		else buffer = nbuffer;
		buffer->free = newlen - buffer->len;
	}
	strcpy(&buffer->data[buffer->len], add);
	buffer->len += addlen;
	buffer->free -= addlen;
	buffer->data[buffer->len] = '\0';
	return buffer;
}

buf* bufVcatf(buf* buffer, const char *fmt, va_list ap) {
	va_list cpy;
	char staticbuf[1024], *buf = staticbuf;<commit_msg>fix a bug about realloc<commit_after>#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "buf.h"

buf* bufNew(const char *init) {
	buf* buffer;
	size_t initlen = strlen(init);
	buffer = malloc(sizeof(struct buf) + initlen + 1);
	if (buffer == NULL) return NULL;
	buffer->len = 0;
	buffer->free = initlen;
	buffer->data[buffer->len] = '\0';
	buffer = bufCat(buffer, init);
	return buffer;
}

void bufRelease(Buf buf) {
	if (buf == NULL) return;
	free(buf);
}

buf* bufCat(buf* buffer, const char *add) {
	size_t addlen = strlen(add);
	if (addlen == 0) return buffer;
	if (addlen > buffer->free) {
		size_t newlen = buffer->len + addlen;
		if (newlen < BUF_MAX_PREALLOC) newlen *= 2;
		else newlen += BUF_MAX_PREALLOC;
		buf* nbuffer = realloc(buffer, sizeof(struct buf)+ newlen + 1);
		if (nbuffer == NULL) return buffer;
		else { 
			if (buffer != nbuffer) free(buffer);
			buffer = nbuffer;
		};
		buffer->free = newlen - buffer->len;
	}
	strcpy(&buffer->data[buffer->len], add);
	buffer->len += addlen;
	buffer->free -= addlen;
	buffer->data[buffer->len] = '\0';
	return buffer;
}

buf* bufVcatf(buf* buffer, const char *fmt, va_list ap) {
	va_list cpy;
	char staticbuf[1024], *buf = staticbuf;
{'fraction_non_alphanumeric': 0.16561047739754964, 'fraction_numerical': 0.009294465568229826, 'mean_word_length': 4.227373068432671, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, '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': '1106879', 'n_tokens_mistral': 939, 'n_tokens_neox': 898, 'n_words': 241}