#ifndef COMMON_FUNCTIONS_H
#define COMMON_FUNCTIONS_H

#include <string>
#include <uchar.h>
#include <codecvt>
#include <locale>

inline long long mstrlen(const char16_t *s) {
	return sizeof(s) / sizeof(*s);
}

inline std::string u16stringToBytes(const std::u16string& str)
{
#if defined(_MSC_VER)
	auto p = reinterpret_cast<unsigned short const*>(str.data());
	auto out = std::wstring_convert<std::codecvt_utf16<unsigned short>, unsigned short>().to_bytes(p, p + str.size());
	return out;
#else
	std::wstring_convert<std::codecvt_utf16<char16_t>, char16_t> conv;
	return conv.to_bytes(str);
#endif
}

inline std::u16string bytesToU16string(const std::string& str)
{
#if defined(_MSC_VER)
	std::u16string out;
	auto s = std::wstring_convert<std::codecvt_utf16<unsigned short>, unsigned short>().from_bytes(str);
	auto p = reinterpret_cast<wchar_t const*>(s.data());
	out.assign(p, p + s.size());
	return out;
#else
	std::wstring_convert<std::codecvt_utf16<char16_t>, char16_t> conv;
	return conv.from_bytes(str);
#endif
}

#endif