
#include "../common.h"
#include "../print.h"
#include "iden.h"


struct iden identbl[MAX_IDEN];

static size_t identbl_len = 0;


static bool strneq(const char* l, const char* r, size_t n) {
	for(size_t i = 0; i < n; i++) {
		if(r[i] != l[i])
			return false;
	}

	return true;
}

size_t register_iden(const char* data, size_t len, uint64_t hash) {
	size_t i;
	for(i = 0; i < identbl_len; i++) {
		if(hash == identbl[i].hash &&
		   identbl[i].len == len &&
		   strneq(data, identbl[i].data, len)) {
				return i;
		}
	}

	if(identbl_len >= MAX_IDEN) {
		print("Out of space for identifiers!");
		hang();
		return 0;
	}

	identbl_len++;
	identbl[i].hash = hash;
	identbl[i].len = len;
	memcpy(identbl[i].data, data, len);

	return i;
}
