Merge branch 'master' of http://github.com/oy/teeworlds into Cleanup

This commit is contained in:
GreYFoXGTi 2010-10-01 13:08:13 +02:00
commit e885fcb8a2
4 changed files with 58 additions and 1 deletions

View file

@ -1454,6 +1454,24 @@ int str_utf8_decode(const char **ptr)
} }
int str_utf8_check(const char *str)
{
while(*str)
{
if((*str&0x80) == 0x0)
str++;
else if((*str&0xE0) == 0xC0 && (*(str+1)&0xC0) == 0x80)
str += 2;
else if((*str&0xF0) == 0xE0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80)
str += 3;
else if((*str&0xF8) == 0xF0 && (*(str+1)&0xC0) == 0x80 && (*(str+2)&0xC0) == 0x80 && (*(str+3)&0xC0) == 0x80)
str += 4;
else
return 0;
}
return 1;
}
unsigned str_quickhash(const char *str) unsigned str_quickhash(const char *str)
{ {

View file

@ -1147,6 +1147,22 @@ int str_utf8_decode(const char **ptr);
*/ */
int str_utf8_encode(char *ptr, int chr); int str_utf8_encode(char *ptr, int chr);
/*
Function: str_utf8_check
Checks if a strings contains just valid utf8 characters.
Parameters:
str - Pointer to a possible utf8 string.
Returns:
0 - invalid characters found.
1 - only valid characters found.
Remarks:
- The string is treated as zero-terminated utf8 string.
*/
int str_utf8_check(const char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -396,7 +396,28 @@ static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData, int
CStrVariableData *pData = (CStrVariableData *)pUserData; CStrVariableData *pData = (CStrVariableData *)pUserData;
if(pResult->NumArguments()) if(pResult->NumArguments())
str_copy(pData->m_pStr, pResult->GetString(0), pData->m_MaxSize); {
const char *pString = pResult->GetString(0);
if(!str_utf8_check(pString))
{
char Temp[4];
int Length = 0;
while(*pString)
{
int Size = str_utf8_encode(Temp, static_cast<const unsigned char>(*pString++));
if(Length+Size < pData->m_MaxSize)
{
mem_copy(pData->m_pStr+Length, &Temp, Size);
Length += Size;
}
else
break;
}
pData->m_pStr[Length] = 0;
}
else
str_copy(pData->m_pStr, pString, pData->m_MaxSize);
}
else else
{ {
char aBuf[1024]; char aBuf[1024];

View file

@ -51,6 +51,8 @@ char *CLineReader::Get()
if(m_aBuffer[m_BufferPos] == '\n' || m_aBuffer[m_BufferPos] == '\r') if(m_aBuffer[m_BufferPos] == '\n' || m_aBuffer[m_BufferPos] == '\r')
{ {
// line found // line found
if(m_aBuffer[m_BufferPos] == '\r' && m_BufferPos+1 < m_BufferSize && m_aBuffer[m_BufferPos+1] == '\n')
m_aBuffer[m_BufferPos++] = 0;
m_aBuffer[m_BufferPos] = 0; m_aBuffer[m_BufferPos] = 0;
m_BufferPos++; m_BufferPos++;
return &m_aBuffer[LineStart]; return &m_aBuffer[LineStart];