mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
106 lines
2.8 KiB
C++
106 lines
2.8 KiB
C++
#include "core/stdafx.h"
|
|
|
|
FORCEINLINE unsigned char tolower_fast(unsigned char c)
|
|
{
|
|
if ((c >= 'A') && (c <= 'Z'))
|
|
return c + ('a' - 'A');
|
|
return c;
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Finds a string in another string with a case insensitive test
|
|
//-----------------------------------------------------------------------------
|
|
char const* V_stristr(char const* pStr, char const* pSearch)
|
|
{
|
|
AssertValidStringPtr(reinterpret_cast<const TCHAR*>(pStr));
|
|
AssertValidStringPtr(reinterpret_cast<const TCHAR*>(pSearch));
|
|
|
|
if (!pStr || !pSearch)
|
|
return 0;
|
|
|
|
char const* pLetter = pStr;
|
|
|
|
// Check the entire string
|
|
while (*pLetter != 0)
|
|
{
|
|
// Skip over non-matches
|
|
if (tolower_fast((unsigned char)*pLetter) == tolower_fast((unsigned char)*pSearch))
|
|
{
|
|
// Check for match
|
|
char const* pMatch = pLetter + 1;
|
|
char const* pTest = pSearch + 1;
|
|
while (*pTest != 0)
|
|
{
|
|
// We've run off the end; don't bother.
|
|
if (*pMatch == 0)
|
|
return 0;
|
|
|
|
if (tolower_fast((unsigned char)*pMatch) != tolower_fast((unsigned char)*pTest))
|
|
break;
|
|
|
|
++pMatch;
|
|
++pTest;
|
|
}
|
|
|
|
// Found a match!
|
|
if (*pTest == 0)
|
|
return pLetter;
|
|
}
|
|
|
|
++pLetter;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
char* V_stristr(char* pStr, char const* pSearch)
|
|
{
|
|
AssertValidStringPtr(reinterpret_cast<const TCHAR*>(pStr));
|
|
AssertValidStringPtr(reinterpret_cast<const TCHAR*>(pSearch));
|
|
|
|
return (char*)V_stristr((char const*)pStr, pSearch);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: Converts a UTF8 string into a unicode string
|
|
//-----------------------------------------------------------------------------
|
|
int V_UTF8ToUnicode(const char* pUTF8, wchar_t* pwchDest, int cubDestSizeInBytes)
|
|
{
|
|
Assert(cubDestSizeInBytes >= sizeof(*pwchDest));
|
|
pwchDest[0] = 0;
|
|
if (!pUTF8)
|
|
return 0;
|
|
#ifdef _WIN32
|
|
int cchResult = MultiByteToWideChar(CP_UTF8, 0, pUTF8, -1, pwchDest, cubDestSizeInBytes / sizeof(wchar_t));
|
|
#elif POSIX
|
|
int cchResult = mbstowcs(pwchDest, pUTF8, cubDestSizeInBytes / sizeof(wchar_t));
|
|
#endif
|
|
pwchDest[(cubDestSizeInBytes / sizeof(wchar_t)) - 1] = 0;
|
|
return cchResult;
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: Converts a unicode string into a UTF8 (standard) string
|
|
//-----------------------------------------------------------------------------
|
|
int V_UnicodeToUTF8(const wchar_t* pUnicode, char* pUTF8, int cubDestSizeInBytes)
|
|
{
|
|
if (cubDestSizeInBytes > 0)
|
|
{
|
|
pUTF8[0] = 0;
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
int cchResult = WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUTF8, cubDestSizeInBytes, NULL, NULL);
|
|
#elif POSIX
|
|
int cchResult = 0;
|
|
if (pUnicode && pUTF8)
|
|
cchResult = wcstombs(pUTF8, pUnicode, cubDestSizeInBytes);
|
|
#endif
|
|
|
|
if (cubDestSizeInBytes > 0)
|
|
{
|
|
pUTF8[cubDestSizeInBytes - 1] = 0;
|
|
}
|
|
|
|
return cchResult;
|
|
} |