aboutsummaryrefslogtreecommitdiff
path: root/mp/src/tier1/strtools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mp/src/tier1/strtools.cpp')
-rw-r--r--mp/src/tier1/strtools.cpp94
1 files changed, 22 insertions, 72 deletions
diff --git a/mp/src/tier1/strtools.cpp b/mp/src/tier1/strtools.cpp
index ad301750..2c5f72fa 100644
--- a/mp/src/tier1/strtools.cpp
+++ b/mp/src/tier1/strtools.cpp
@@ -1251,7 +1251,7 @@ bool Q_RemoveAllEvilCharacters( char *pch )
int cch = Q_strlen( pch );
int cubDest = (cch + 1 ) * sizeof( wchar_t );
wchar_t *pwch = (wchar_t *)stackalloc( cubDest );
- int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest );
+ int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest ) / sizeof( wchar_t );
bool bStrippedWhitespace = false;
@@ -1289,8 +1289,13 @@ bool Q_RemoveAllEvilCharacters( char *pch )
//-----------------------------------------------------------------------------
bool Q_StripPrecedingAndTrailingWhitespaceW( wchar_t *pwch )
{
- // duplicate on stack
int cch = Q_wcslen( pwch );
+
+ // Early out and don't convert if we don't have any chars or leading/trailing ws.
+ if ( ( cch < 1 ) || ( !iswspace( pwch[ 0 ] ) && !iswspace( pwch[ cch - 1 ] ) ) )
+ return false;
+
+ // duplicate on stack
int cubDest = ( cch + 1 ) * sizeof( wchar_t );
wchar_t *pwchT = (wchar_t *)stackalloc( cubDest );
Q_wcsncpy( pwchT, pwch, cubDest );
@@ -1340,11 +1345,16 @@ bool Q_AggressiveStripPrecedingAndTrailingWhitespaceW( wchar_t *pwch )
//-----------------------------------------------------------------------------
bool Q_StripPrecedingAndTrailingWhitespace( char *pch )
{
- // convert to unicode
int cch = Q_strlen( pch );
+
+ // Early out and don't convert if we don't have any chars or leading/trailing ws.
+ if ( ( cch < 1 ) || ( !isspace( (unsigned char)pch[ 0 ] ) && !isspace( (unsigned char)pch[ cch - 1 ] ) ) )
+ return false;
+
+ // convert to unicode
int cubDest = (cch + 1 ) * sizeof( wchar_t );
wchar_t *pwch = (wchar_t *)stackalloc( cubDest );
- int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest );
+ int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest ) / sizeof( wchar_t );
bool bStrippedWhitespace = false;
pwch = StripWhitespaceWorker( cwch-1, pwch, &bStrippedWhitespace, false /* not aggressive */ );
@@ -1367,7 +1377,7 @@ bool Q_AggressiveStripPrecedingAndTrailingWhitespace( char *pch )
int cch = Q_strlen( pch );
int cubDest = (cch + 1 ) * sizeof( wchar_t );
wchar_t *pwch = (wchar_t *)stackalloc( cubDest );
- int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest );
+ int cwch = Q_UTF8ToUnicode( pch, pwch, cubDest ) / sizeof( wchar_t );
bool bStrippedWhitespace = false;
pwch = StripWhitespaceWorker( cwch-1, pwch, &bStrippedWhitespace, true /* is aggressive */ );
@@ -1381,72 +1391,10 @@ bool Q_AggressiveStripPrecedingAndTrailingWhitespace( char *pch )
return bStrippedWhitespace;
}
-
-//-----------------------------------------------------------------------------
-// Purpose: Converts a UTF8 string into a unicode string
-//-----------------------------------------------------------------------------
-int V_UTF8ToUnicode( const char *pUTF8, wchar_t *pwchDest, int cubDestSizeInBytes )
-{
- // pwchDest can be null to allow for getting the length of the string
- if ( cubDestSizeInBytes > 0 )
- {
- AssertValidWritePtr(pwchDest);
- pwchDest[0] = 0;
- }
-
- if ( !pUTF8 )
- return 0;
-
- AssertValidStringPtr(pUTF8);
-
-#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) ) + 1;
-#endif
-
- if ( cubDestSizeInBytes > 0 )
- {
- 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 )
-{
- //AssertValidStringPtr(pUTF8, cubDestSizeInBytes); // no, we are sometimes pasing in NULL to fetch the length of the buffer needed.
- AssertValidReadPtr(pUnicode);
-
- 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 ) + 1;
-#endif
-
- if ( cubDestSizeInBytes > 0 )
- {
- pUTF8[cubDestSizeInBytes - 1] = 0;
- }
-
- return cchResult;
-}
-
-
//-----------------------------------------------------------------------------
// Purpose: Converts a ucs2 string to a unicode (wchar_t) one, no-op on win32
//-----------------------------------------------------------------------------
-int V_UCS2ToUnicode( const ucs2 *pUCS2, wchar_t *pUnicode, int cubDestSizeInBytes )
+int _V_UCS2ToUnicode( const ucs2 *pUCS2, wchar_t *pUnicode, int cubDestSizeInBytes )
{
Assert( cubDestSizeInBytes >= sizeof( *pUnicode ) );
AssertValidWritePtr(pUnicode);
@@ -1455,7 +1403,7 @@ int V_UCS2ToUnicode( const ucs2 *pUCS2, wchar_t *pUnicode, int cubDestSizeInByte
pUnicode[0] = 0;
#ifdef _WIN32
int cchResult = V_wcslen( pUCS2 );
- Q_memcpy( pUnicode, pUCS2, cubDestSizeInBytes );
+ V_memcpy( pUnicode, pUCS2, cubDestSizeInBytes );
#else
iconv_t conv_t = iconv_open( "UCS-4LE", "UCS-2LE" );
int cchResult = -1;
@@ -1486,7 +1434,7 @@ int V_UCS2ToUnicode( const ucs2 *pUCS2, wchar_t *pUnicode, int cubDestSizeInByte
//-----------------------------------------------------------------------------
// Purpose: Converts a wchar_t string into a UCS2 string -noop on windows
//-----------------------------------------------------------------------------
-int V_UnicodeToUCS2( const wchar_t *pUnicode, int cubSrcInBytes, char *pUCS2, int cubDestSizeInBytes )
+int _V_UnicodeToUCS2( const wchar_t *pUnicode, int cubSrcInBytes, char *pUCS2, int cubDestSizeInBytes )
{
#ifdef _WIN32
// Figure out which buffer is smaller and convert from bytes to character
@@ -1512,6 +1460,8 @@ int V_UnicodeToUCS2( const wchar_t *pUnicode, int cubSrcInBytes, char *pUCS2, in
else
cchResult = cubSrcInBytes / sizeof( wchar_t );
}
+#else
+ #error Must be implemented for this platform
#endif
return cchResult;
}
@@ -1520,7 +1470,7 @@ int V_UnicodeToUCS2( const wchar_t *pUnicode, int cubSrcInBytes, char *pUCS2, in
//-----------------------------------------------------------------------------
// Purpose: Converts a ucs-2 (windows wchar_t) string into a UTF8 (standard) string
//-----------------------------------------------------------------------------
-int V_UCS2ToUTF8( const ucs2 *pUCS2, char *pUTF8, int cubDestSizeInBytes )
+int _V_UCS2ToUTF8( const ucs2 *pUCS2, char *pUTF8, int cubDestSizeInBytes )
{
AssertValidStringPtr(pUTF8, cubDestSizeInBytes);
AssertValidReadPtr(pUCS2);
@@ -1574,7 +1524,7 @@ int V_UCS2ToUTF8( const ucs2 *pUCS2, char *pUTF8, int cubDestSizeInBytes )
//-----------------------------------------------------------------------------
// Purpose: Converts a UTF8 to ucs-2 (windows wchar_t)
//-----------------------------------------------------------------------------
-int V_UTF8ToUCS2( const char *pUTF8, int cubSrcInBytes, ucs2 *pUCS2, int cubDestSizeInBytes )
+int _V_UTF8ToUCS2( const char *pUTF8, int cubSrcInBytes, ucs2 *pUCS2, int cubDestSizeInBytes )
{
Assert( cubDestSizeInBytes >= sizeof(pUCS2[0]) );
AssertValidStringPtr(pUTF8, cubDestSizeInBytes);