#pragma once
#define min(a,b) (((a) < (b)) ? (a) : (b))
#include <stddef.h>

struct String {

	size_t length;
	char* data;

	constexpr String() : length(0), data(0) {};
	constexpr String(size_t length, char* data) : length(length), data(data) {};
	template<size_t _len> constexpr String(const char(&data)[_len]) : length(_len - 1), data((char*)data) {};

	char operator[](int index) {
		if (index < 0) {
			return 0;
		}
		if (index < length) {
			return data[index];
		}
		return 0;
	}
};

inline String operator""_str(const char* str, size_t length) {
	return { length, (char*)str };
}

inline bool operator==(String a, String b) {
	if (a.length != b.length)
		return false;

	if (a.data == b.data)
		return true;

	for (int i = 0; i < a.length; i++)
		if (a[i] != b[i])
			return false;

	return true;
}

inline void advance(String& s, int num = 1) {
	int to_advance = min(s.length, num);
	s.data = s.data + to_advance;
	s.length = s.length - to_advance;
}

inline bool starts_with(String s, String start) {
	if (s.length < start.length) {
		return false;
	}
	for (int i = 0; i < start.length; i++) {
		if (s[i] != start[i]) {
			return false;
		}
	}
	return true;
}

template <typename T>
inline T read(String& file) {
	if (file.length < sizeof(T))
		return {};
	T value = *(T*)file.data;
	advance(file, sizeof(T));
	return value;
}