2742: Add str_utf8_copy that trim broken utf8 sequence at the end. r=heinrich5991 a=TsFreddie

Currently this replaces `str_copy` text input and clipboard paste in chat. Many places may need the same treatments, like steam names.

![image](https://user-images.githubusercontent.com/3797859/92263741-08b8e080-eed5-11ea-84a5-b7f070ded260.png)

Test string:
abcd今天是个好日子心想的事儿都能成今天是个好日子打开了家门咱迎春风今天是个好日子心想的事儿都能成今天是个好日子打开了家门咱迎春风今天是个好日子心想的事儿都能成今天是个好日子打

2745: Update mapres by mind r=heinrich5991 a=def-

Adapted from 0.7 for new renderer. Not sure if this makes sense :D

Co-authored-by: TsFreddie <tsfreddiewang@gmail.com>
Co-authored-by: def <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-09-06 23:12:04 +00:00 committed by GitHub
commit e3cfb23f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 58 additions and 8 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 KiB

After

Width:  |  Height:  |  Size: 172 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 239 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 125 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 KiB

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 207 KiB

After

Width:  |  Height:  |  Size: 246 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 226 KiB

After

Width:  |  Height:  |  Size: 271 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 154 B

After

Width:  |  Height:  |  Size: 376 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 160 KiB

After

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 220 KiB

View file

@ -3182,6 +3182,10 @@ int str_utf8_check(const char *str)
return 1;
}
void str_utf8_copy(char *dst, const char *src, int dst_size)
{
str_utf8_truncate(dst, dst_size, src, dst_size);
}
unsigned str_quickhash(const char *str)
{

View file

@ -1918,6 +1918,22 @@ int str_utf16le_encode(char *ptr, int chr);
*/
int str_utf8_check(const char *str);
/*
Function: str_utf8_copy
Copies a utf8 string to a buffer.
Parameters:
dst - Pointer to a buffer that shall receive the string.
src - utf8 string to be copied.
dst_size - Size of the buffer dst.
Remarks:
- The strings are treated as zero-terminated strings.
- Guarantees that dst string will contain zero-termination.
- Guarantees that dst always contains a valid utf8 string.
*/
void str_utf8_copy(char *dst, const char *src, int dst_size);
/*
Function: str_next_token
Writes the next token after str into buf, returns the rest of the string.

View file

@ -26,7 +26,7 @@ void CInput::AddEvent(char *pText, int Key, int Flags)
if(!pText)
m_aInputEvents[m_NumEvents].m_aText[0] = 0;
else
str_copy(m_aInputEvents[m_NumEvents].m_aText, pText, sizeof(m_aInputEvents[m_NumEvents].m_aText));
str_utf8_copy(m_aInputEvents[m_NumEvents].m_aText, pText, sizeof(m_aInputEvents[m_NumEvents].m_aText));
m_aInputEvents[m_NumEvents].m_InputCount = m_InputCounter;
m_NumEvents++;
}

View file

@ -168,14 +168,14 @@ bool CChat::OnInput(IInput::CEvent Event)
if(Text[i] == '\n')
{
int max = minimum(i - Begin + 1, (int)sizeof(Line));
str_copy(Line, Text + Begin, max);
str_utf8_copy(Line, Text + Begin, max);
Begin = i+1;
SayChat(Line);
while(Text[i] == '\n') i++;
}
}
int max = minimum(i - Begin + 1, (int)sizeof(Line));
str_copy(Line, Text + Begin, max);
str_utf8_copy(Line, Text + Begin, max);
Begin = i+1;
m_Input.Add(Line);
}

View file

@ -249,12 +249,16 @@ int CMenus::DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrS
{
int Offset = str_length(pStr);
int CharsLeft = StrSize - Offset - 1;
for(int i = 0; i < str_length(Text) && i < CharsLeft; i++)
char *pCur = pStr + Offset;
str_utf8_copy(pCur, Text, CharsLeft);
for(int i = 0; i < CharsLeft; i++)
{
if(Text[i] == '\n')
pStr[i + Offset] = ' ';
else
pStr[i + Offset] = Text[i];
if(pCur[i] == 0)
break;
else if(pCur[i] == '\r')
pCur[i] = ' ';
else if(pCur[i] == '\n')
pCur[i] = ' ';
}
s_AtIndex = str_length(pStr);
ReturnValue = true;

View file

@ -209,3 +209,29 @@ TEST(Str, StrCopyNum)
str_utf8_truncate(aBuf3, sizeof(aBuf3), foo, 7);
EXPECT_STREQ(aBuf3, "Foobaré");
}
TEST(Str, StrCopyUtf8)
{
const char *foo = "DDNet最好了";
char aBuf[64];
str_utf8_copy(aBuf, foo, 7);
EXPECT_STREQ(aBuf, "DDNet");
str_utf8_copy(aBuf, foo, 8);
EXPECT_STREQ(aBuf, "DDNet");
str_utf8_copy(aBuf, foo, 9);
EXPECT_STREQ(aBuf, "DDNet最");
str_utf8_copy(aBuf, foo, 10);
EXPECT_STREQ(aBuf, "DDNet最");
str_utf8_copy(aBuf, foo, 11);
EXPECT_STREQ(aBuf, "DDNet最");
str_utf8_copy(aBuf, foo, 12);
EXPECT_STREQ(aBuf, "DDNet最好");
str_utf8_copy(aBuf, foo, 13);
EXPECT_STREQ(aBuf, "DDNet最好");
str_utf8_copy(aBuf, foo, 14);
EXPECT_STREQ(aBuf, "DDNet最好");
str_utf8_copy(aBuf, foo, 15);
EXPECT_STREQ(aBuf, "DDNet最好了");
str_utf8_copy(aBuf, foo, 16);
EXPECT_STREQ(aBuf, "DDNet最好了");
}