<commit_before>#include "stdafx.h"
#include "string.h"
#include <algorithm>
#include <locale>
#include <codecvt>

wstring formatV(wstring szMsg, va_list argList)
{
	int len = _vscwprintf(szMsg.c_str(), argList);
	if (0 != len)
	{
		len++;
		LPWSTR buffer = new wchar_t[len];
		memset(buffer, 0, sizeof(wchar_t)* len);
		if (_vsnwprintf_s(buffer, len, _TRUNCATE, szMsg.c_str(), argList) > 0)
		{
			wstring szOut(buffer);
			delete[] buffer;
			return szOut;
		}

		delete[] buffer;
	}

	return L"";
}

wstring format(const LPWSTR szMsg, ...)
{
	va_list argList;
	va_start(argList, szMsg);
	wstring ret = formatV(szMsg, argList);
	va_end(argList);
	return ret;
}

wstring loadstring(DWORD dwID)
{
	wstring fmtString;
	LPWSTR buffer = 0;
	size_t len = ::LoadStringW(NULL, dwID, (PWCHAR)&buffer, 0);

	if (len)
	{
		fmtString.assign(buffer, len);
	}

	return fmtString;
}

wstring formatmessageV(wstring szMsg, va_list argList)
{
		LPWSTR buffer = NULL;
		DWORD dw = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, szMsg.c_str(), 0, 0, (LPWSTR)&buffer, 0, &argList);
		if (dw)
		{
			wstring ret = wstring(buffer);
			(void)LocalFree(buffer);
			return ret;
		}

	return L"";
}

wstring formatmessage(DWORD dwID, ...)
{
	va_list argList;
	va_start(argList, dwID);
	wstring ret = formatmessageV(loadstring(dwID), argList);
	va_end(argList);
	return ret;
}

wstring formatmessage(wstring szMsg, ...)
{
	va_list argList;
	va_start(argList, szMsg);
	wstring ret = formatmessageV(szMsg, argList);
	va_end(argList);
	return ret;
}

LPTSTR wstringToLPTSTR(wstring src)
{
	LPTSTR dst = NULL;
#ifdef UNICODE
	size_t cch = src.length();
	if (!cch) return NULL;

	cch++; // Null terminator
	dst = new WCHAR[cch];
	if (dst)
	{
		memcpy(dst, src.c_str(), cch * sizeof(WCHAR));
	}
#else
	HRESULT hRes = S_OK;
	EC_H(UnicodeToAnsi(src.c_str(), &dst));
#endif

	return dst;
}

CString wstringToCString(wstring src)
{
	CString dst;
#ifdef UNICODE
	dst = src.c_str();
#else
	dst.Format("%ws", src.c_str());
#endif
	return dst;
}

wstring LPCTSTRToWstring(LPCTSTR src)
{
#ifdef UNICODE
	return src;
#else
	return LPCSTRToWstring(src);
#endif
}

wstring LPCSTRToWstring(LPCSTR src)
{<commit_msg>Problem: NULL pointer in unicode Fix: Don't convert null to wstring<commit_after>#include "stdafx.h"
#include "string.h"
#include <algorithm>
#include <locale>
#include <codecvt>

wstring formatV(wstring szMsg, va_list argList)
{
	int len = _vscwprintf(szMsg.c_str(), argList);
	if (0 != len)
	{
		len++;
		LPWSTR buffer = new wchar_t[len];
		memset(buffer, 0, sizeof(wchar_t)* len);
		if (_vsnwprintf_s(buffer, len, _TRUNCATE, szMsg.c_str(), argList) > 0)
		{
			wstring szOut(buffer);
			delete[] buffer;
			return szOut;
		}

		delete[] buffer;
	}

	return L"";
}

wstring format(const LPWSTR szMsg, ...)
{
	va_list argList;
	va_start(argList, szMsg);
	wstring ret = formatV(szMsg, argList);
	va_end(argList);
	return ret;
}

wstring loadstring(DWORD dwID)
{
	wstring fmtString;
	LPWSTR buffer = 0;
	size_t len = ::LoadStringW(NULL, dwID, (PWCHAR)&buffer, 0);

	if (len)
	{
		fmtString.assign(buffer, len);
	}

	return fmtString;
}

wstring formatmessageV(wstring szMsg, va_list argList)
{
		LPWSTR buffer = NULL;
		DWORD dw = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, szMsg.c_str(), 0, 0, (LPWSTR)&buffer, 0, &argList);
		if (dw)
		{
			wstring ret = wstring(buffer);
			(void)LocalFree(buffer);
			return ret;
		}

	return L"";
}

wstring formatmessage(DWORD dwID, ...)
{
	va_list argList;
	va_start(argList, dwID);
	wstring ret = formatmessageV(loadstring(dwID), argList);
	va_end(argList);
	return ret;
}

wstring formatmessage(wstring szMsg, ...)
{
	va_list argList;
	va_start(argList, szMsg);
	wstring ret = formatmessageV(szMsg, argList);
	va_end(argList);
	return ret;
}

LPTSTR wstringToLPTSTR(wstring src)
{
	LPTSTR dst = NULL;
#ifdef UNICODE
	size_t cch = src.length();
	if (!cch) return NULL;

	cch++; // Null terminator
	dst = new WCHAR[cch];
	if (dst)
	{
		memcpy(dst, src.c_str(), cch * sizeof(WCHAR));
	}
#else
	HRESULT hRes = S_OK;
	EC_H(UnicodeToAnsi(src.c_str(), &dst));
#endif

	return dst;
}

CString wstringToCString(wstring src)
{
	CString dst;
#ifdef UNICODE
	dst = src.c_str();
#else
	dst.Format("%ws", src.c_str());
#endif
	return dst;
}

wstring LPCTSTRToWstring(LPCTSTR src)
{
#ifdef UNICODE
	if (!src) return L"";
	return src;
#else
	return LPCSTRToWstring(src);
#endif
}

wstring LPCSTRToWstring(LPCSTR src)
{
{'fraction_non_alphanumeric': 0.1423463687150838, 'fraction_numerical': 0.003575418994413408, 'mean_word_length': 4.680203045685279, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '7072613', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1848, 'n_words': 422}