add str_utf8_stats to base system

This commit is contained in:
Robert Müller 2021-11-24 23:24:46 +01:00
parent 2376de500d
commit 84ecbdb9d0
3 changed files with 71 additions and 0 deletions

View file

@ -3454,6 +3454,23 @@ void str_utf8_copy(char *dst, const char *src, int dst_size)
str_utf8_truncate(dst, dst_size, src, dst_size);
}
void str_utf8_stats(const char *str, int max_size, int max_count, int *size, int *count)
{
*size = 0;
*count = 0;
while(str[*size] && *size < max_size && *count < max_count)
{
int new_size = str_utf8_forward(str, *size);
if(new_size != *size)
{
if(new_size >= max_size || *count >= max_count)
break;
*size = new_size;
++(*count);
}
}
}
unsigned str_quickhash(const char *str)
{
unsigned hash = 5381;

View file

@ -2089,6 +2089,23 @@ int str_utf8_check(const char *str);
*/
void str_utf8_copy(char *dst, const char *src, int dst_size);
/*
Function: str_utf8_stats
Determines the byte size and utf8 character count of a string.
Parameters:
str - Pointer to the string.
max_size - Maximum number of bytes to count.
max_count - Maximum number of utf8 characters to count.
size - Pointer to store size (number of non-zero bytes) of the string.
count - Pointer to store count of utf8 characters of the string.
Remarks:
- Assumes nothing about the encoding of the string.
It's the users responsibility to make sure the bounds are aligned.
*/
void str_utf8_stats(const char *str, int max_size, int max_count, int *size, int *count);
/*
Function: str_next_token
Writes the next token after str into buf, returns the rest of the string.

View file

@ -255,6 +255,43 @@ TEST(Str, StrCopyUtf8)
EXPECT_STREQ(aBuf, "DDNet最好了");
}
TEST(Str, Utf8Stats)
{
int Size, Count;
str_utf8_stats("abc", 4, 3, &Size, &Count);
EXPECT_EQ(Size, 3);
EXPECT_EQ(Count, 3);
str_utf8_stats("abc", 2, 3, &Size, &Count);
EXPECT_EQ(Size, 1);
EXPECT_EQ(Count, 1);
str_utf8_stats("", 1, 0, &Size, &Count);
EXPECT_EQ(Size, 0);
EXPECT_EQ(Count, 0);
str_utf8_stats("abcde", 6, 5, &Size, &Count);
EXPECT_EQ(Size, 5);
EXPECT_EQ(Count, 5);
str_utf8_stats("любовь", 13, 6, &Size, &Count);
EXPECT_EQ(Size, 12);
EXPECT_EQ(Count, 6);
str_utf8_stats("abc愛", 7, 4, &Size, &Count);
EXPECT_EQ(Size, 6);
EXPECT_EQ(Count, 4);
str_utf8_stats("abc愛", 6, 4, &Size, &Count);
EXPECT_EQ(Size, 3);
EXPECT_EQ(Count, 3);
str_utf8_stats("любовь", 13, 3, &Size, &Count);
EXPECT_EQ(Size, 6);
EXPECT_EQ(Count, 3);
}
TEST(Str, StrTime)
{
char aBuf[32] = "foobar";