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

#include "ring-buffer.h"

#include "minmax.h"

#include "array-utils.h"

// same as ring_buffer_available()
static inline unsigned rbavail( ring_buffer_t *rb ) {
	unsigned r;

	if ( NULL == rb ) {
		r = 0;
		goto out;
	}

	// assert( rb->capacity >= rb->len );
	r = rb->capacity - rb->len;

out:
	return r;
}

// the index (not the offset) of the tail element
static inline unsigned rbtail( ring_buffer_t *rb ) {
	unsigned r;

	if ( NULL == rb ) {
		r = 0;
		goto out;
	}

	if ( 0 == rb->len ) {
		r = rb->head;
	} else {
		r = ( rb->head + rb->len - 1 ) % rb->capacity;
	}

out:
	return r;
}

static int      ring_buffer_peek( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_read( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_write( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_send( ring_buffer_t *out, ring_buffer_t *in );
static int      ring_buffer_skip( ring_buffer_t *rb, unsigned data_len );
static unsigned ring_buffer_size( ring_buffer_t *rb );
static unsigned ring_buffer_available( ring_buffer_t *rb );
static void     ring_buffer_reset( ring_buffer_t *rb );
static void     ring_buffer_realign( ring_buffer_t *rb );

int ring_buffer_init( ring_buffer_t *rb, unsigned capacity, void *buffer ) {
	int r;

	if ( NULL == rb || NULL == buffer ) {
		r = -EINVAL;
		goto out;
	}

	*( (unsigned *) & rb->capacity ) = capacity;
	rb->head = 0;
	rb->len = 0;

	rb->peek = ring_buffer_peek;
	rb->read = ring_buffer_read;
	rb->write = ring_buffer_write;
	rb->send = ring_buffer_send;
	rb->skip = ring_buffer_skip;
	rb->size = ring_buffer_size;
	rb->available = ring_buffer_available;
	rb->reset = ring_buffer_reset;
	rb->realign = ring_buffer_realign;

	rb->buffer = buffer;

	r = EXIT_SUCCESS;

out:
	return r;
}

static int ring_buffer_peek( ring_buffer_t *rb, void *data, unsigned data_len ) {
	int r;
	unsigned t1, t2;

	if ( NULL == rb || NULL == data ) {
		r = -EINVAL;
		goto out;
	}

	r = min( rb->len, data_len );
	if ( 0 == r ) {
		goto out;
	}
	if ( rb->head + r >= rb->capacity ) {
		t1 = rb->capacity - rb->head;
		memcpy( data, & rb->buffer[ rb->head ], t1 );
		t2 = r - t1;
		memcpy( & ( (uint8_t *) data )[ t1 ], & rb->buffer[ 0 ], t2 );
	} else {
		memcpy( data, rb->buffer, r );
	}

out:
	return r;
}

static int ring_buffer_read( ring_buffer_t *rb, void *data, unsigned data_len ) {
	int r;
	r = ring_buffer_peek( rb, data, data_len );
	if ( r > 0 ) {
		ring_buffer_skip( rb, r );
	}
	return r;<commit_msg>Fix bug in peek.<commit_after>#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "ring-buffer.h"

#include "minmax.h"

#include "array-utils.h"

// same as ring_buffer_available()
static inline unsigned rbavail( ring_buffer_t *rb ) {
	unsigned r;

	if ( NULL == rb ) {
		r = 0;
		goto out;
	}

	// assert( rb->capacity >= rb->len );
	r = rb->capacity - rb->len;

out:
	return r;
}

// the index (not the offset) of the tail element
static inline unsigned rbtail( ring_buffer_t *rb ) {
	unsigned r;

	if ( NULL == rb ) {
		r = 0;
		goto out;
	}

	if ( 0 == rb->len ) {
		r = rb->head;
	} else {
		r = ( rb->head + rb->len - 1 ) % rb->capacity;
	}

out:
	return r;
}

static int      ring_buffer_peek( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_read( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_write( ring_buffer_t *rb, void *data, unsigned data_len );
static int      ring_buffer_send( ring_buffer_t *out, ring_buffer_t *in );
static int      ring_buffer_skip( ring_buffer_t *rb, unsigned data_len );
static unsigned ring_buffer_size( ring_buffer_t *rb );
static unsigned ring_buffer_available( ring_buffer_t *rb );
static void     ring_buffer_reset( ring_buffer_t *rb );
static void     ring_buffer_realign( ring_buffer_t *rb );

int ring_buffer_init( ring_buffer_t *rb, unsigned capacity, void *buffer ) {
	int r;

	if ( NULL == rb || NULL == buffer ) {
		r = -EINVAL;
		goto out;
	}

	*( (unsigned *) & rb->capacity ) = capacity;
	rb->head = 0;
	rb->len = 0;

	rb->peek = ring_buffer_peek;
	rb->read = ring_buffer_read;
	rb->write = ring_buffer_write;
	rb->send = ring_buffer_send;
	rb->skip = ring_buffer_skip;
	rb->size = ring_buffer_size;
	rb->available = ring_buffer_available;
	rb->reset = ring_buffer_reset;
	rb->realign = ring_buffer_realign;

	rb->buffer = buffer;

	r = EXIT_SUCCESS;

out:
	return r;
}

static int ring_buffer_peek( ring_buffer_t *rb, void *data, unsigned data_len ) {
	int r;
	unsigned t1, t2;

	if ( NULL == rb || NULL == data ) {
		r = -EINVAL;
		goto out;
	}

	r = min( rb->len, data_len );
	if ( 0 == r ) {
		goto out;
	}
	if ( rb->head + r >= rb->capacity ) {
		t1 = rb->capacity - rb->head;
		memcpy( data, & rb->buffer[ rb->head ], t1 );
		t2 = r - t1;
		memcpy( & ( (uint8_t *) data )[ t1 ], & rb->buffer[ 0 ], t2 );
	} else {
		memcpy( data, & rb->buffer[ rb->head ], r );
	}

out:
	return r;
}

static int ring_buffer_read( ring_buffer_t *rb, void *data, unsigned data_len ) {
	int r;
	r = ring_buffer_peek( rb, data, data_len );
	if ( r > 0 ) {
		ring_buffer_skip( rb, r );
	}
	return r;
{'fraction_non_alphanumeric': 0.14521954001140466, 'fraction_numerical': 0.006842805550275613, 'mean_word_length': 3.3886572143452875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, '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': '6221773', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2027, 'n_words': 557}