2018-03-06 17:41:18 +00:00
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
|
|
|
|
|
TEST(Str, Dist)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("aaa", "aaa"), 0);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("123", "123"), 0);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("", ""), 0);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("a", "b"), 1);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("", "aaa"), 3);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("123", ""), 3);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("ä", ""), 1);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("Hëllö", "Hello"), 2);
|
|
|
|
|
// https://en.wikipedia.org/w/index.php?title=Levenshtein_distance&oldid=828480025#Example
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("kitten", "sitting"), 3);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("flaw", "lawn"), 2);
|
|
|
|
|
EXPECT_EQ(str_utf8_dist("saturday", "sunday"), 3);
|
|
|
|
|
}
|
2018-03-14 01:27:15 +00:00
|
|
|
|
|
|
|
|
|
TEST(Str, Utf8Isspace)
|
|
|
|
|
{
|
2018-03-14 02:04:41 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_isspace(0x200b)); // Zero-width space
|
2018-03-14 01:27:15 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_isspace(' '));
|
|
|
|
|
EXPECT_FALSE(str_utf8_isspace('a'));
|
2018-03-14 02:04:41 +00:00
|
|
|
|
// Control characters.
|
|
|
|
|
for(char c = 0; c < 0x20; c++)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_utf8_isspace(c));
|
|
|
|
|
}
|
2018-03-14 01:27:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, Utf8SkipWhitespaces)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_STREQ(str_utf8_skip_whitespaces("abc"), "abc");
|
|
|
|
|
EXPECT_STREQ(str_utf8_skip_whitespaces("abc "), "abc ");
|
|
|
|
|
EXPECT_STREQ(str_utf8_skip_whitespaces(" abc"), "abc");
|
2018-03-14 02:04:41 +00:00
|
|
|
|
EXPECT_STREQ(str_utf8_skip_whitespaces("\xe2\x80\x8b abc"), "abc");
|
2018-03-14 01:27:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, Utf8TrimRight)
|
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
|
char A1[] = "abc";
|
|
|
|
|
str_utf8_trim_right(A1);
|
|
|
|
|
EXPECT_STREQ(A1, "abc");
|
|
|
|
|
char A2[] = " abc";
|
|
|
|
|
str_utf8_trim_right(A2);
|
|
|
|
|
EXPECT_STREQ(A2, " abc");
|
|
|
|
|
char A3[] = "abc ";
|
|
|
|
|
str_utf8_trim_right(A3);
|
|
|
|
|
EXPECT_STREQ(A3, "abc");
|
|
|
|
|
char A4[] = "abc \xe2\x80\x8b";
|
|
|
|
|
str_utf8_trim_right(A4);
|
|
|
|
|
EXPECT_STREQ(A4, "abc");
|
2018-03-14 01:27:15 +00:00
|
|
|
|
}
|
2018-03-20 20:11:44 +00:00
|
|
|
|
|
|
|
|
|
TEST(Str, Utf8CompConfusables)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("abc", "abc") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("rn", "m") == 0);
|
2022-10-30 10:43:10 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("m", "rn") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("rna", "ma") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("ma", "rna") == 0);
|
|
|
|
|
EXPECT_FALSE(str_utf8_comp_confusable("mA", "rna") == 0);
|
|
|
|
|
EXPECT_FALSE(str_utf8_comp_confusable("ma", "rnA") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("arn", "am") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("am", "arn") == 0);
|
|
|
|
|
EXPECT_FALSE(str_utf8_comp_confusable("Am", "arn") == 0);
|
|
|
|
|
EXPECT_FALSE(str_utf8_comp_confusable("am", "Arn") == 0);
|
2018-03-20 20:11:44 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("l", "ӏ") == 0); // CYRILLIC SMALL LETTER PALOCHKA
|
2021-07-27 00:43:04 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("i", "¡") == 0); // INVERTED EXCLAMATION MARK
|
2018-03-20 20:11:44 +00:00
|
|
|
|
EXPECT_FALSE(str_utf8_comp_confusable("o", "x") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_confusable("aceiou", "ąçęįǫų") == 0);
|
|
|
|
|
}
|
2018-07-25 08:23:07 +00:00
|
|
|
|
|
2022-11-01 22:32:58 +00:00
|
|
|
|
TEST(Str, Utf8ToSkeleton)
|
|
|
|
|
{
|
|
|
|
|
int aBuf[32];
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("abc", aBuf, 0), 0);
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("", aBuf, std::size(aBuf)), 0);
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("abc", aBuf, std::size(aBuf)), 3);
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'a');
|
|
|
|
|
EXPECT_EQ(aBuf[1], 'b');
|
|
|
|
|
EXPECT_EQ(aBuf[2], 'c');
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("m", aBuf, std::size(aBuf)), 2);
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'r');
|
|
|
|
|
EXPECT_EQ(aBuf[1], 'n');
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("rn", aBuf, std::size(aBuf)), 2);
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'r');
|
|
|
|
|
EXPECT_EQ(aBuf[1], 'n');
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("ӏ", aBuf, std::size(aBuf)), 1); // CYRILLIC SMALL LETTER PALOCHKA
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'i');
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("¡", aBuf, std::size(aBuf)), 1); // INVERTED EXCLAMATION MARK
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'i');
|
|
|
|
|
EXPECT_EQ(str_utf8_to_skeleton("ąçęįǫų", aBuf, std::size(aBuf)), 6);
|
|
|
|
|
EXPECT_EQ(aBuf[0], 'a');
|
|
|
|
|
EXPECT_EQ(aBuf[1], 'c');
|
|
|
|
|
EXPECT_EQ(aBuf[2], 'e');
|
|
|
|
|
EXPECT_EQ(aBuf[3], 'i');
|
|
|
|
|
EXPECT_EQ(aBuf[4], 'o');
|
|
|
|
|
EXPECT_EQ(aBuf[5], 'u');
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 22:49:20 +00:00
|
|
|
|
TEST(Str, Utf8ToLower)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_utf8_tolower('A') == 'a');
|
|
|
|
|
EXPECT_TRUE(str_utf8_tolower('z') == 'z');
|
|
|
|
|
EXPECT_TRUE(str_utf8_tolower(192) == 224); // À -> à
|
|
|
|
|
EXPECT_TRUE(str_utf8_tolower(7882) == 7883); // Ị -> ị
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase("ÖlÜ", "ölü") == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase("ÜlÖ", "ölü") > 0); // ü > ö
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase("ÖlÜ", "ölüa") < 0); // NULL < a
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase("ölüa", "ÖlÜ") > 0); // a < NULL
|
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
|
#if(CHAR_MIN < 0)
|
2019-04-02 15:19:20 +00:00
|
|
|
|
const char a[2] = {CHAR_MIN, 0};
|
2019-01-07 22:49:20 +00:00
|
|
|
|
const char b[2] = {0, 0};
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase(a, b) > 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase(b, a) < 0);
|
2019-04-02 15:19:20 +00:00
|
|
|
|
#endif
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
2019-02-10 18:13:55 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase_num("ÖlÜ", "ölüa", 5) == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase_num("ÖlÜ", "ölüa", 6) != 0);
|
2019-01-07 22:49:20 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase_num("a", "z", 0) == 0);
|
|
|
|
|
EXPECT_TRUE(str_utf8_comp_nocase_num("a", "z", 1) != 0);
|
|
|
|
|
|
|
|
|
|
const char str[] = "ÄÖÜ";
|
|
|
|
|
EXPECT_TRUE(str_utf8_find_nocase(str, "ä") == str);
|
2020-09-26 19:41:58 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_find_nocase(str, "ö") == str + 2);
|
|
|
|
|
EXPECT_TRUE(str_utf8_find_nocase(str, "ü") == str + 4);
|
2019-01-07 22:49:20 +00:00
|
|
|
|
EXPECT_TRUE(str_utf8_find_nocase(str, "z") == NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 01:05:07 +00:00
|
|
|
|
TEST(Str, Utf8FixTruncation)
|
|
|
|
|
{
|
|
|
|
|
char aaBuf[][32] = {
|
|
|
|
|
"",
|
|
|
|
|
"\xff",
|
|
|
|
|
"abc",
|
|
|
|
|
"abc\xff",
|
|
|
|
|
"blub\xffxyz",
|
|
|
|
|
"привет Наташа\xff",
|
|
|
|
|
"до свидания\xffОлег",
|
|
|
|
|
};
|
|
|
|
|
const char *apExpected[] = {
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"abc",
|
|
|
|
|
"abc",
|
|
|
|
|
"blub\xffxyz",
|
|
|
|
|
"привет Наташа",
|
|
|
|
|
"до свидания\xffОлег",
|
|
|
|
|
};
|
2022-03-30 13:16:19 +00:00
|
|
|
|
for(unsigned i = 0; i < std::size(aaBuf); i++)
|
2021-12-20 01:05:07 +00:00
|
|
|
|
{
|
|
|
|
|
EXPECT_EQ(str_utf8_fix_truncation(aaBuf[i]), str_length(apExpected[i]));
|
|
|
|
|
EXPECT_STREQ(aaBuf[i], apExpected[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 08:23:07 +00:00
|
|
|
|
TEST(Str, Startswith)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_startswith("abcdef", "abc"));
|
|
|
|
|
EXPECT_FALSE(str_startswith("abc", "abcdef"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_startswith("xyz", ""));
|
|
|
|
|
EXPECT_FALSE(str_startswith("", "xyz"));
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_startswith("house", "home"));
|
|
|
|
|
EXPECT_FALSE(str_startswith("blackboard", "board"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_startswith("поплавать", "по"));
|
|
|
|
|
EXPECT_FALSE(str_startswith("плавать", "по"));
|
|
|
|
|
|
2018-07-26 12:04:44 +00:00
|
|
|
|
static const char ABCDEFG[] = "abcdefg";
|
2018-07-25 08:23:07 +00:00
|
|
|
|
static const char ABC[] = "abc";
|
2018-07-26 12:04:44 +00:00
|
|
|
|
EXPECT_EQ(str_startswith(ABCDEFG, ABC) - ABCDEFG, str_length(ABC));
|
2018-07-25 08:23:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 10:42:45 +00:00
|
|
|
|
TEST(Str, StartswithNocase)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_startswith_nocase("Abcdef", "abc"));
|
|
|
|
|
EXPECT_FALSE(str_startswith_nocase("aBc", "abcdef"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_startswith_nocase("xYz", ""));
|
|
|
|
|
EXPECT_FALSE(str_startswith_nocase("", "xYz"));
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_startswith_nocase("house", "home"));
|
|
|
|
|
EXPECT_FALSE(str_startswith_nocase("Blackboard", "board"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_startswith_nocase("поплавать", "по"));
|
|
|
|
|
EXPECT_FALSE(str_startswith_nocase("плавать", "по"));
|
|
|
|
|
|
|
|
|
|
static const char ABCDEFG[] = "aBcdefg";
|
|
|
|
|
static const char ABC[] = "abc";
|
|
|
|
|
EXPECT_EQ(str_startswith_nocase(ABCDEFG, ABC) - ABCDEFG, str_length(ABC));
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 08:23:07 +00:00
|
|
|
|
TEST(Str, Endswith)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_endswith("abcdef", "def"));
|
|
|
|
|
EXPECT_FALSE(str_endswith("def", "abcdef"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_endswith("xyz", ""));
|
|
|
|
|
EXPECT_FALSE(str_endswith("", "xyz"));
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_endswith("rhyme", "mine"));
|
|
|
|
|
EXPECT_FALSE(str_endswith("blackboard", "black"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_endswith("люди", "юди"));
|
|
|
|
|
EXPECT_FALSE(str_endswith("люди", "любовь"));
|
2018-07-26 12:04:44 +00:00
|
|
|
|
|
|
|
|
|
static const char ABCDEFG[] = "abcdefg";
|
|
|
|
|
static const char DEFG[] = "defg";
|
|
|
|
|
EXPECT_EQ(str_endswith(ABCDEFG, DEFG) - ABCDEFG,
|
|
|
|
|
str_length(ABCDEFG) - str_length(DEFG));
|
2018-07-25 08:23:07 +00:00
|
|
|
|
}
|
2018-08-19 10:12:11 +00:00
|
|
|
|
|
2019-02-10 16:13:13 +00:00
|
|
|
|
TEST(StrFormat, Positional)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[256];
|
|
|
|
|
|
|
|
|
|
// normal
|
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%s %s", "first", "second");
|
|
|
|
|
EXPECT_STREQ(aBuf, "first second");
|
|
|
|
|
|
|
|
|
|
// normal with positional arguments
|
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%1$s %2$s", "first", "second");
|
|
|
|
|
EXPECT_STREQ(aBuf, "first second");
|
|
|
|
|
|
|
|
|
|
// reverse
|
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%2$s %1$s", "first", "second");
|
|
|
|
|
EXPECT_STREQ(aBuf, "second first");
|
|
|
|
|
|
|
|
|
|
// duplicate
|
|
|
|
|
str_format(aBuf, sizeof(aBuf), "%1$s %1$s %2$d %1$s %2$d", "str", 1);
|
|
|
|
|
EXPECT_STREQ(aBuf, "str str 1 str 1");
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 10:42:45 +00:00
|
|
|
|
TEST(Str, EndswithNocase)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_TRUE(str_endswith_nocase("abcdef", "deF"));
|
|
|
|
|
EXPECT_FALSE(str_endswith_nocase("def", "abcdef"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_endswith_nocase("xyz", ""));
|
|
|
|
|
EXPECT_FALSE(str_endswith_nocase("", "xyz"));
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_endswith_nocase("rhyme", "minE"));
|
|
|
|
|
EXPECT_FALSE(str_endswith_nocase("blackboard", "black"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_endswith_nocase("люди", "юди"));
|
|
|
|
|
EXPECT_FALSE(str_endswith_nocase("люди", "любовь"));
|
|
|
|
|
|
|
|
|
|
static const char ABCDEFG[] = "abcdefG";
|
|
|
|
|
static const char DEFG[] = "defg";
|
|
|
|
|
EXPECT_EQ(str_endswith_nocase(ABCDEFG, DEFG) - ABCDEFG,
|
|
|
|
|
str_length(ABCDEFG) - str_length(DEFG));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 14:18:35 +00:00
|
|
|
|
TEST(Str, HexEncode)
|
|
|
|
|
{
|
|
|
|
|
char aOut[64];
|
|
|
|
|
const char *pData = "ABCD";
|
|
|
|
|
str_hex(aOut, sizeof(aOut), pData, 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex(aOut, sizeof(aOut), pData, 1);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 ");
|
|
|
|
|
str_hex(aOut, sizeof(aOut), pData, 2);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 42 ");
|
|
|
|
|
str_hex(aOut, sizeof(aOut), pData, 3);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 42 43 ");
|
|
|
|
|
str_hex(aOut, sizeof(aOut), pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 42 43 44 ");
|
2023-01-17 23:09:07 +00:00
|
|
|
|
|
2022-09-29 14:18:35 +00:00
|
|
|
|
str_hex(aOut, 1, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex(aOut, 2, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex(aOut, 3, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex(aOut, 4, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 ");
|
|
|
|
|
str_hex(aOut, 5, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 ");
|
|
|
|
|
str_hex(aOut, 6, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 ");
|
|
|
|
|
str_hex(aOut, 7, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 42 ");
|
|
|
|
|
str_hex(aOut, 8, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "41 42 ");
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 23:09:07 +00:00
|
|
|
|
TEST(Str, HexEncodeCstyle)
|
|
|
|
|
{
|
|
|
|
|
char aOut[128];
|
|
|
|
|
const char *pData = "ABCD";
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 1);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 2);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 3);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42, 0x43");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42, 0x43, 0x44");
|
|
|
|
|
|
|
|
|
|
str_hex_cstyle(aOut, 1, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 2, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 3, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 4, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 5, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 6, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "");
|
|
|
|
|
str_hex_cstyle(aOut, 7, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41");
|
|
|
|
|
str_hex_cstyle(aOut, 12, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41");
|
|
|
|
|
str_hex_cstyle(aOut, 13, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42");
|
|
|
|
|
str_hex_cstyle(aOut, 14, pData, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42");
|
|
|
|
|
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4, 1);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41,\n0x42,\n0x43,\n0x44");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4, 2);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42,\n0x43, 0x44");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4, 3);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42, 0x43,\n0x44");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4, 4);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42, 0x43, 0x44");
|
|
|
|
|
str_hex_cstyle(aOut, sizeof(aOut), pData, 4, 500);
|
|
|
|
|
EXPECT_STREQ(aOut, "0x41, 0x42, 0x43, 0x44");
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-19 10:12:11 +00:00
|
|
|
|
TEST(Str, HexDecode)
|
|
|
|
|
{
|
|
|
|
|
char aOut[5] = {'a', 'b', 'c', 'd', 0};
|
2020-09-26 19:41:58 +00:00
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 0, ""), 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "abcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 0, " "), 2);
|
|
|
|
|
EXPECT_STREQ(aOut, "abcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 1, "1"), 2);
|
|
|
|
|
EXPECT_STREQ(aOut + 1, "bcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 1, "41"), 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "Abcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 1, "4x"), 1);
|
|
|
|
|
EXPECT_STREQ(aOut + 1, "bcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 1, "x1"), 1);
|
|
|
|
|
EXPECT_STREQ(aOut + 1, "bcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 1, "411"), 2);
|
|
|
|
|
EXPECT_STREQ(aOut + 1, "bcd");
|
|
|
|
|
EXPECT_EQ(str_hex_decode(aOut, 4, "41424344"), 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "ABCD");
|
2018-08-19 10:12:11 +00:00
|
|
|
|
}
|
2019-02-06 12:30:47 +00:00
|
|
|
|
|
2021-07-16 00:55:27 +00:00
|
|
|
|
void StrBase64Str(char *pBuffer, int BufferSize, const char *pString)
|
|
|
|
|
{
|
|
|
|
|
str_base64(pBuffer, BufferSize, pString, str_length(pString));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, Base64)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[128];
|
|
|
|
|
str_base64(aBuf, sizeof(aBuf), "\0", 1);
|
|
|
|
|
EXPECT_STREQ(aBuf, "AA==");
|
|
|
|
|
str_base64(aBuf, sizeof(aBuf), "\0\0", 2);
|
|
|
|
|
EXPECT_STREQ(aBuf, "AAA=");
|
|
|
|
|
str_base64(aBuf, sizeof(aBuf), "\0\0\0", 3);
|
|
|
|
|
EXPECT_STREQ(aBuf, "AAAA");
|
|
|
|
|
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "");
|
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/w/index.php?title=Base64&oldid=1033503483#Output_padding
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "pleasure.");
|
|
|
|
|
EXPECT_STREQ(aBuf, "cGxlYXN1cmUu");
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "leasure.");
|
|
|
|
|
EXPECT_STREQ(aBuf, "bGVhc3VyZS4=");
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "easure.");
|
|
|
|
|
EXPECT_STREQ(aBuf, "ZWFzdXJlLg==");
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "asure.");
|
|
|
|
|
EXPECT_STREQ(aBuf, "YXN1cmUu");
|
|
|
|
|
StrBase64Str(aBuf, sizeof(aBuf), "sure.");
|
|
|
|
|
EXPECT_STREQ(aBuf, "c3VyZS4=");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, Base64Decode)
|
|
|
|
|
{
|
|
|
|
|
char aOut[17];
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), ""), 0);
|
|
|
|
|
EXPECT_STREQ(aOut, "XXXXXXXXXXXXXXXX");
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/w/index.php?title=Base64&oldid=1033503483#Output_padding
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "cGxlYXN1cmUu"), 9);
|
|
|
|
|
EXPECT_STREQ(aOut, "pleasure.XXXXXXX");
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "bGVhc3VyZS4="), 8);
|
|
|
|
|
EXPECT_STREQ(aOut, "leasure.XXXXXXXX");
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "ZWFzdXJlLg=="), 7);
|
|
|
|
|
EXPECT_STREQ(aOut, "easure.XXXXXXXXX");
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "YXN1cmUu"), 6);
|
|
|
|
|
EXPECT_STREQ(aOut, "asure.XXXXXXXXXX");
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "c3VyZS4="), 5);
|
|
|
|
|
EXPECT_STREQ(aOut, "sure.XXXXXXXXXXX");
|
|
|
|
|
str_copy(aOut, "XXXXXXXXXXXXXXXX", sizeof(aOut));
|
|
|
|
|
EXPECT_EQ(str_base64_decode(aOut, sizeof(aOut), "////"), 3);
|
|
|
|
|
EXPECT_STREQ(aOut, "\xff\xff\xffXXXXXXXXXXXXX");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, Base64DecodeError)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[128];
|
|
|
|
|
// Wrong padding.
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "A"), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "AA"), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "AAA"), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "A==="), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "=AAA"), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "===="), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "AAA=AAAA"), 0);
|
|
|
|
|
// Invalid characters.
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "----"), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "AAAA "), 0);
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "AAA "), 0);
|
|
|
|
|
// Invalid padding values.
|
|
|
|
|
EXPECT_LT(str_base64_decode(aBuf, sizeof(aBuf), "//=="), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 09:46:29 +00:00
|
|
|
|
TEST(Str, Tokenize)
|
|
|
|
|
{
|
|
|
|
|
char aTest[] = "GER,RUS,ZAF,BRA,CAN";
|
2022-06-30 22:36:32 +00:00
|
|
|
|
const char *apOut[] = {"GER", "RUS", "ZAF", "BRA", "CAN"};
|
2019-03-05 09:46:29 +00:00
|
|
|
|
char aBuf[4];
|
|
|
|
|
|
|
|
|
|
int n = 0;
|
2022-06-30 22:36:32 +00:00
|
|
|
|
for(const char *pTok = aTest; (pTok = str_next_token(pTok, ",", aBuf, sizeof(aBuf)));)
|
|
|
|
|
EXPECT_STREQ(apOut[n++], aBuf);
|
2019-03-05 09:46:29 +00:00
|
|
|
|
|
|
|
|
|
char aTest2[] = "";
|
2019-03-11 11:54:31 +00:00
|
|
|
|
EXPECT_EQ(str_next_token(aTest2, ",", aBuf, sizeof(aBuf)), nullptr);
|
2019-04-28 13:32:14 +00:00
|
|
|
|
|
|
|
|
|
char aTest3[] = "+b";
|
|
|
|
|
str_next_token(aTest3, "+", aBuf, sizeof(aBuf));
|
|
|
|
|
EXPECT_STREQ(aBuf, "b");
|
2019-03-05 09:46:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 12:30:47 +00:00
|
|
|
|
TEST(Str, InList)
|
|
|
|
|
{
|
|
|
|
|
char aTest[] = "GER,RUS,ZAF,BRA,CAN";
|
|
|
|
|
EXPECT_TRUE(str_in_list(aTest, ",", "GER"));
|
|
|
|
|
EXPECT_TRUE(str_in_list(aTest, ",", "RUS"));
|
|
|
|
|
EXPECT_TRUE(str_in_list(aTest, ",", "ZAF"));
|
|
|
|
|
EXPECT_TRUE(str_in_list(aTest, ",", "BRA"));
|
|
|
|
|
EXPECT_TRUE(str_in_list(aTest, ",", "CAN"));
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_in_list(aTest, ",", "CHN"));
|
|
|
|
|
EXPECT_FALSE(str_in_list(aTest, ",", "R,R"));
|
2019-02-13 14:11:09 +00:00
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(str_in_list("abc,xyz", ",", "abcdef"));
|
|
|
|
|
EXPECT_FALSE(str_in_list("", ",", ""));
|
|
|
|
|
EXPECT_FALSE(str_in_list("", ",", "xyz"));
|
2019-04-29 20:55:12 +00:00
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_in_list("FOO,,BAR", ",", ""));
|
2020-09-06 14:29:20 +00:00
|
|
|
|
EXPECT_TRUE(str_in_list("abc,,def", ",", "def"));
|
2019-02-06 12:30:47 +00:00
|
|
|
|
}
|
2019-03-20 18:09:23 +00:00
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, Format)
|
2019-03-20 18:09:23 +00:00
|
|
|
|
{
|
|
|
|
|
char aBuf[4];
|
|
|
|
|
EXPECT_EQ(str_format(aBuf, 4, "%d:", 9), 2);
|
|
|
|
|
EXPECT_STREQ(aBuf, "9:");
|
|
|
|
|
EXPECT_EQ(str_format(aBuf, 4, "%d: ", 9), 3);
|
|
|
|
|
EXPECT_STREQ(aBuf, "9: ");
|
|
|
|
|
EXPECT_EQ(str_format(aBuf, 4, "%d: ", 99), 3);
|
|
|
|
|
EXPECT_STREQ(aBuf, "99:");
|
|
|
|
|
}
|
2019-04-16 00:24:24 +00:00
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, FormatTruncate)
|
2021-12-20 01:34:02 +00:00
|
|
|
|
{
|
|
|
|
|
const char *pStr = "DDNet最好了";
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_format(aBuf, 7, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet");
|
|
|
|
|
str_format(aBuf, 8, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet");
|
|
|
|
|
str_format(aBuf, 9, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
|
|
|
|
str_format(aBuf, 10, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
|
|
|
|
str_format(aBuf, 11, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
|
|
|
|
str_format(aBuf, 12, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
|
|
|
|
str_format(aBuf, 13, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
|
|
|
|
str_format(aBuf, 14, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
|
|
|
|
str_format(aBuf, 15, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好了");
|
|
|
|
|
str_format(aBuf, 16, "%s", pStr);
|
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好了");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 11:53:05 +00:00
|
|
|
|
TEST(Str, TrimWords)
|
|
|
|
|
{
|
|
|
|
|
const char *pStr1 = "aa bb ccc dddd eeeee";
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 0), "aa bb ccc dddd eeeee");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 1), "bb ccc dddd eeeee");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 2), "ccc dddd eeeee");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 3), "dddd eeeee");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 4), "eeeee");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 5), "");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr1, 100), "");
|
|
|
|
|
const char *pStr2 = " aaa bb ";
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr2, 0), "aaa bb ");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr2, 1), "bb ");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr2, 2), "");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr2, 100), "");
|
|
|
|
|
const char *pStr3 = "\n\naa bb\t\tccc\r\n\r\ndddd";
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 0), "aa bb\t\tccc\r\n\r\ndddd");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 1), "bb\t\tccc\r\n\r\ndddd");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 2), "ccc\r\n\r\ndddd");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 3), "dddd");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 4), "");
|
|
|
|
|
EXPECT_STREQ(str_trim_words(pStr3, 100), "");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, CopyNum)
|
2019-04-16 00:24:24 +00:00
|
|
|
|
{
|
2022-06-30 22:36:32 +00:00
|
|
|
|
const char *pFoo = "Foobaré";
|
2019-04-16 00:24:24 +00:00
|
|
|
|
char aBuf[64];
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, 3, pFoo, 1);
|
2019-04-16 00:24:24 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "F");
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, 3, pFoo, 2);
|
2019-04-16 00:24:24 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "Fo");
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, 3, pFoo, 3);
|
2019-04-16 00:24:24 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "Fo");
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, sizeof(aBuf), pFoo, 6);
|
2019-04-16 00:24:24 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "Foobar");
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, sizeof(aBuf), pFoo, 7);
|
2020-08-02 11:45:20 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "Foobaré");
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf, sizeof(aBuf), pFoo, 0);
|
2021-01-22 18:17:56 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
2020-08-02 11:45:20 +00:00
|
|
|
|
|
|
|
|
|
char aBuf2[8];
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf2, sizeof(aBuf2), pFoo, 7);
|
2020-08-02 11:45:20 +00:00
|
|
|
|
EXPECT_STREQ(aBuf2, "Foobar");
|
|
|
|
|
char aBuf3[9];
|
2022-06-30 22:36:32 +00:00
|
|
|
|
str_utf8_truncate(aBuf3, sizeof(aBuf3), pFoo, 7);
|
2020-08-02 11:45:20 +00:00
|
|
|
|
EXPECT_STREQ(aBuf3, "Foobaré");
|
2019-04-16 00:24:24 +00:00
|
|
|
|
}
|
2020-09-06 22:57:21 +00:00
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, Copy)
|
2020-09-06 22:57:21 +00:00
|
|
|
|
{
|
2021-12-20 01:34:02 +00:00
|
|
|
|
const char *pStr = "DDNet最好了";
|
2020-09-06 22:57:21 +00:00
|
|
|
|
char aBuf[64];
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 7);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 8);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 9);
|
2020-09-06 23:03:40 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 10);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 11);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 12);
|
2020-09-06 23:03:40 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 13);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 14);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 15);
|
2020-09-06 23:03:40 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好了");
|
2021-12-20 01:34:02 +00:00
|
|
|
|
str_copy(aBuf, pStr, 16);
|
2020-09-06 22:57:21 +00:00
|
|
|
|
EXPECT_STREQ(aBuf, "DDNet最好了");
|
|
|
|
|
}
|
2020-10-18 20:44:02 +00:00
|
|
|
|
|
2021-11-24 22:24:46 +00:00
|
|
|
|
TEST(Str, Utf8Stats)
|
|
|
|
|
{
|
Port line input and IME support from 0.7
Port the line input (UI edit boxes, chat, console) and Input Method Editor (IME) support from upstream. Closes #4397.
General
------------------------------
Fix issues with the text input. Closes #4346. Closes #4524.
Word skipping (when holding Ctrl) is overhauled to be consistent with the Windows / Firefox experience that I took as reference.
Improve usability by not blinking (i.e. always rendering) the caret shortly after is has been moved.
UI text input
------------------------------
Fix inconsistent mouse-based left and right scrolling (closes #4347).
Support smooth left and right scrolling.
Chat
------------------------------
Support keyboard-based text selection of the chat input.
Mouse-based selection could be support in the future when we decide to add something like an ingame UI cursor.
Support smooth up and down scrolling of the chat input, removing the old hack that offsets the input string to simulate scrolling.
Console
------------------------------
Also support mouse-based text selection of the command input.
Only text from either the command input or the console log can be selected at the same time. This ensures that Ctrl+C will always copy the text that is currently visually selected in the console.
Check for Ctrl+C input event in event handler instead of in render function, to hopefully fix the issue that copying does not work sometimes (closes #5974 until further notice).
When Ctrl+C is used to copy text from the console log, the selection is cleared. This should make it more clear when text was copied from the log.
Fix an issue that was preventing the console log selection from being cleared, when all log lines are selected.
Remove Ctrl+A/E hotkeys that move cursor to beginning/end respectively. Ctrl+A now selectes all text like for all other inputs. Home and End keys can still be used to go the beginning and end.
Remove Ctrl+U/K hotkeys that clear everything before/after the cursor respectively. Hold shift and use Home/End to select everything instead.
IME support
------------------------------
Render list of IME candidates in the client on Windows, so the candidate list can also be viewed in fullscreen mode. There is no API available to retrieve a candidate list on the other operating systems.
Improve composition rendering by underlining the composition text instead of putting it in square brackets.
Track active input globally to properly activate and deactivate IME through the SDL functions.
Closes #1030. Closes #1008.
Password rendering
------------------------------
Fix rendering of passwords containing unicode. Instead of rendering one star character for each UTF-8 `char`, render on star for every unicode codepoint.
Show the composition text also for passwords. Without seeing the composition text it's hard to type a password containing those characters. The candidate window exposes the composition anyway. If you don't want to expose your password this way, e.g. while streaming, you could:
1. Use a latin password and switch off the IME for the password input with the IME hotkey.
2. Blank your screen with an external program while you are streaming and entering passwords.
3. Use binds to authenticate in rcon or to set the server browser password.
Refactoring
------------------------------
Move all text input logic and general rendering to `CLineInput`.
A `CLineInput` is associated with a particular `char` buffer given as a pointer either in the constructor or with `SetBuffer`. The maximum byte size of the buffer must also be specified. The maximum length in unicode codepoints can also be specified separately (e.g. on upstream, name are limited by the number of unicode codepoints instead).
Add `CLineInputBuffered`, which is a `CLineInput` that own a `char` buffer of a fixed size, which is specified as a template argument. As `CLineInput` does not own a buffer anymore, this reduces duplicate code for line inputs that need their own buffer.
Add `CLineInputNumber` which has additional convenience functions to consider the text as an `int` or `float`, to reduce duplicate code in those cases. In the future we could also add an input filter function so that only numbers can be entered in the number input.
Add `CLineInput::SetClipboardLineCallback` to handle the case that multiple lines of text are pasted into a lineinput. This reduces duplicate code, as this behavior was previously implemented separately for chat and console. The behavior is also fixed to be consistent with the console on Windows, so the first line being pasted edits the current input text and then sends it instead of being sent on its own without the existing input text.
Add `CalcFontSizeAndBoundingBox` to UI to reduce duplicate code. Expose `CalcAlignedCursorPos` as static member function to reuse it for line input.
Dispatch input events to UI inputs through the event handler instead of storing them in a duplicate buffer.
Use `size_t` for line input cursor position, length etc. and for `str_utf8_stats`.
Add `IButtonColorFunction` to UI to describe a functions that defines colors for the Default, Active and Hovered states of UI elements. Add some default button color functions. Use button color function to reduce duplicate code in scrollbar rendering.
Use `vec2` instead of two `floats` to represent the mouse positions in the text renderer.
Remove `CaretPosition` again, as it does not calculate the correct Y position near line breaks due to the wrapping being different when not rendering the entire string. Instead, calculate the exact caret position when rending a text container and store the caret position in the text cursor for later use.
IME usage guide (Windows)
------------------------------
1. Install the respective language and the Microsoft-IME keyboard (e.g. for Chinese, Japanese or Korean).
2. Launch the game (or a text editor to first try out the IME). Note that Windows may track the input language separately for every application. You can change this in the Windows input settings so the input language is changed globally.
2. Switch the input language using the hotkey Windows+Space or another hotkey that you configured in the Windows input settings (Alt+Shift is the default, but you should consider disabling it, to avoid accidentally changing the input language while playing).
3. Switch from Latin/English input mode to the respective asian input mode.
- Chinese: Use Ctrl+Space to switch between English and Chinese input mode. You can change this hotkey in the IME's settings.
- Japanese: Use Ctrl+Space to switch between Alphanumeric and Hiragana/Katakana input mode. You can change this hotkey in the IME's settings.
- Korean: Use Right Alt to switch between English and Hangul input mode. You cannot change this hotkey as of yet.
- Note that the input mode is also tracked per application, but there is no setting to change this behavior as far as I know, so you'll need to switch for every application separately.
4. Start typing. The underlined text is the current composition text. While a composition is active, you can only edit the composition text. Confirm the composition with Space or by selecting a candidate from the candidate list with the arrow keys. Cancel the composition with Escape or by using Backspace to delete the composition text. Note that not all languages offer a candidate list.
SDL version-specific issues
------------------------------
- 2.26.5, 2.24.2, 2.0.22: IME candidates work. But there are minor bugs when moving the composition cursor.
- 2.0.18, 2.0.20: IME candidates work.
- 2.0.16 (our current version): IME candidates cannot be determined with Windows API. Windows tries to draw the composition window like before, so this does not work in fullscreen mode.
- 2.0.8 (upstream 0.7): IME candidates work. But this SDL version is too old for us.
2023-01-03 21:28:38 +00:00
|
|
|
|
size_t Size, Count;
|
2021-11-24 22:24:46 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, Time)
|
2020-10-18 20:44:02 +00:00
|
|
|
|
{
|
|
|
|
|
char aBuf[32] = "foobar";
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_DAYS, aBuf, 0), -1);
|
|
|
|
|
EXPECT_STREQ(aBuf, "foobar");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_MINS_CENTISECS + 1, aBuf, sizeof(aBuf)), -1);
|
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(-123456, TIME_MINS_CENTISECS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "00:00.00");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(INT64_MAX, TIME_DAYS, aBuf, sizeof(aBuf)), 23);
|
|
|
|
|
EXPECT_STREQ(aBuf, "1067519911673d 00:09:18");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_DAYS, aBuf, sizeof(aBuf)), 5);
|
|
|
|
|
EXPECT_STREQ(aBuf, "20:34");
|
|
|
|
|
EXPECT_EQ(str_time(1234567, TIME_DAYS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "03:25:45");
|
|
|
|
|
EXPECT_EQ(str_time(12345678, TIME_DAYS, aBuf, sizeof(aBuf)), 11);
|
|
|
|
|
EXPECT_STREQ(aBuf, "1d 10:17:36");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_HOURS, aBuf, sizeof(aBuf)), 5);
|
|
|
|
|
EXPECT_STREQ(aBuf, "20:34");
|
|
|
|
|
EXPECT_EQ(str_time(1234567, TIME_HOURS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "03:25:45");
|
|
|
|
|
EXPECT_EQ(str_time(12345678, TIME_HOURS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "34:17:36");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_MINS, aBuf, sizeof(aBuf)), 5);
|
|
|
|
|
EXPECT_STREQ(aBuf, "20:34");
|
|
|
|
|
EXPECT_EQ(str_time(1234567, TIME_MINS, aBuf, sizeof(aBuf)), 6);
|
|
|
|
|
EXPECT_STREQ(aBuf, "205:45");
|
|
|
|
|
EXPECT_EQ(str_time(12345678, TIME_MINS, aBuf, sizeof(aBuf)), 7);
|
|
|
|
|
EXPECT_STREQ(aBuf, "2057:36");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_HOURS_CENTISECS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "20:34.56");
|
|
|
|
|
EXPECT_EQ(str_time(1234567, TIME_HOURS_CENTISECS, aBuf, sizeof(aBuf)), 11);
|
|
|
|
|
EXPECT_STREQ(aBuf, "03:25:45.67");
|
|
|
|
|
EXPECT_EQ(str_time(12345678, TIME_HOURS_CENTISECS, aBuf, sizeof(aBuf)), 11);
|
|
|
|
|
EXPECT_STREQ(aBuf, "34:17:36.78");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time(123456, TIME_MINS_CENTISECS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "20:34.56");
|
|
|
|
|
EXPECT_EQ(str_time(1234567, TIME_MINS_CENTISECS, aBuf, sizeof(aBuf)), 9);
|
|
|
|
|
EXPECT_STREQ(aBuf, "205:45.67");
|
|
|
|
|
EXPECT_EQ(str_time(12345678, TIME_MINS_CENTISECS, aBuf, sizeof(aBuf)), 10);
|
|
|
|
|
EXPECT_STREQ(aBuf, "2057:36.78");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-11 21:40:32 +00:00
|
|
|
|
TEST(Str, TimeFloat)
|
2020-10-18 20:44:02 +00:00
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
EXPECT_EQ(str_time_float(123456.78, TIME_DAYS, aBuf, sizeof(aBuf)), 11);
|
|
|
|
|
EXPECT_STREQ(aBuf, "1d 10:17:36");
|
2021-01-08 10:21:37 +00:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time_float(12.16, TIME_HOURS_CENTISECS, aBuf, sizeof(aBuf)), 8);
|
|
|
|
|
EXPECT_STREQ(aBuf, "00:12.16");
|
2023-03-23 08:46:50 +00:00
|
|
|
|
|
|
|
|
|
EXPECT_EQ(str_time_float(22.995, TIME_MINS, aBuf, sizeof(aBuf)), 5);
|
|
|
|
|
EXPECT_STREQ(aBuf, "00:22");
|
2020-10-18 20:44:02 +00:00
|
|
|
|
}
|
2022-09-11 21:39:16 +00:00
|
|
|
|
|
|
|
|
|
TEST(Str, HasCc)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_FALSE(str_has_cc(""));
|
|
|
|
|
EXPECT_FALSE(str_has_cc("a"));
|
|
|
|
|
EXPECT_FALSE(str_has_cc("Merhaba dünya!"));
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\n"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\r"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\t"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("a\n"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("a\rb"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\tb"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\n\n"));
|
2022-09-28 22:10:39 +00:00
|
|
|
|
EXPECT_TRUE(str_has_cc("\x1C"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\x1D"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\x1E"));
|
|
|
|
|
EXPECT_TRUE(str_has_cc("\x1F"));
|
2022-09-11 21:39:16 +00:00
|
|
|
|
}
|
2022-09-28 22:00:37 +00:00
|
|
|
|
|
2022-09-28 22:11:05 +00:00
|
|
|
|
TEST(Str, SanitizeCc)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_copy(aBuf, "");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
|
|
|
|
str_copy(aBuf, "a");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a");
|
|
|
|
|
str_copy(aBuf, "Merhaba dünya!");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "Merhaba dünya!");
|
|
|
|
|
|
|
|
|
|
str_copy(aBuf, "\n");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\r");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\t");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "a\n");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a ");
|
|
|
|
|
str_copy(aBuf, "a\rb");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a b");
|
|
|
|
|
str_copy(aBuf, "\tb");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " b");
|
|
|
|
|
str_copy(aBuf, "\n\n");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1C");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1D");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1E");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1F");
|
|
|
|
|
str_sanitize_cc(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 22:15:53 +00:00
|
|
|
|
TEST(Str, Sanitize)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_copy(aBuf, "");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
|
|
|
|
str_copy(aBuf, "a");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a");
|
|
|
|
|
str_copy(aBuf, "Merhaba dünya!");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "Merhaba dünya!");
|
|
|
|
|
str_copy(aBuf, "\n");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "\n");
|
|
|
|
|
str_copy(aBuf, "\r");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "\r");
|
|
|
|
|
str_copy(aBuf, "\t");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "\t");
|
|
|
|
|
str_copy(aBuf, "a\n");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a\n");
|
|
|
|
|
str_copy(aBuf, "a\rb");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "a\rb");
|
|
|
|
|
str_copy(aBuf, "\tb");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "\tb");
|
|
|
|
|
str_copy(aBuf, "\n\n");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "\n\n");
|
|
|
|
|
|
|
|
|
|
str_copy(aBuf, "\x1C");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1D");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1E");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
str_copy(aBuf, "\x1F");
|
|
|
|
|
str_sanitize(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, " ");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 21:29:55 +00:00
|
|
|
|
TEST(Str, CleanWhitespaces)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_copy(aBuf, "aa bb ccc dddd eeeee");
|
|
|
|
|
str_clean_whitespaces(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "aa bb ccc dddd eeeee");
|
|
|
|
|
str_copy(aBuf, " ");
|
|
|
|
|
str_clean_whitespaces(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "");
|
|
|
|
|
str_copy(aBuf, " aa");
|
|
|
|
|
str_clean_whitespaces(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "aa");
|
|
|
|
|
str_copy(aBuf, "aa ");
|
|
|
|
|
str_clean_whitespaces(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "aa");
|
|
|
|
|
str_copy(aBuf, " aa bb ccc dddd eeeee ");
|
|
|
|
|
str_clean_whitespaces(aBuf);
|
|
|
|
|
EXPECT_STREQ(aBuf, "aa bb ccc dddd eeeee");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 14:41:03 +00:00
|
|
|
|
TEST(Str, SkipToWhitespace)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_copy(aBuf, "");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf);
|
|
|
|
|
str_copy(aBuf, " a");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf);
|
|
|
|
|
str_copy(aBuf, "aaaa b");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf + 4);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf + 4);
|
|
|
|
|
str_copy(aBuf, "aaaa\n\nb");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf + 4);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf + 4);
|
|
|
|
|
str_copy(aBuf, "aaaa\r\rb");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf + 4);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf + 4);
|
|
|
|
|
str_copy(aBuf, "aaaa\t\tb");
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace(aBuf), aBuf + 4);
|
|
|
|
|
EXPECT_EQ(str_skip_to_whitespace_const(aBuf), aBuf + 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(Str, SkipWhitespaces)
|
|
|
|
|
{
|
|
|
|
|
char aBuf[64];
|
|
|
|
|
str_copy(aBuf, "");
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces(aBuf), aBuf);
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces_const(aBuf), aBuf);
|
|
|
|
|
str_copy(aBuf, "aaaa");
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces(aBuf), aBuf);
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces_const(aBuf), aBuf);
|
|
|
|
|
str_copy(aBuf, " \n\r\taaaa");
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces(aBuf), aBuf + 4);
|
|
|
|
|
EXPECT_EQ(str_skip_whitespaces_const(aBuf), aBuf + 4);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 22:00:37 +00:00
|
|
|
|
TEST(Str, CompFilename)
|
|
|
|
|
{
|
|
|
|
|
EXPECT_EQ(str_comp_filenames("a", "a"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("a", "b"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("b", "a"), 0);
|
|
|
|
|
EXPECT_EQ(str_comp_filenames("A", "a"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("A", "b"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("b", "A"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("a", "B"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("B", "a"), 0);
|
2022-12-30 00:11:57 +00:00
|
|
|
|
EXPECT_EQ(str_comp_filenames("1A", "1a"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("1a", "1B"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("1B", "1a"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("1a", "1b"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("1b", "1a"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("12a", "1B"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("1B", "12a"), 0);
|
2023-01-02 20:20:49 +00:00
|
|
|
|
EXPECT_GT(str_comp_filenames("10a", "1B"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("1B", "10a"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("10a", "00B"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("00B", "10a"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("10a", "09B"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("09B", "10a"), 0);
|
2022-09-28 22:00:37 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("abc", "abcd"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("abcd", "abc"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("abc2", "abcd1"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("abcd1", "abc2"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("abc50", "abcd"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("abcd", "abc50"), 0);
|
|
|
|
|
EXPECT_EQ(str_comp_filenames("file0", "file0"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file0", "file1"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file1", "file0"), 0);
|
2022-12-30 00:11:57 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("file1", "file09"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file09", "file1"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file1", "file009"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file009", "file1"), 0);
|
2023-01-02 20:20:49 +00:00
|
|
|
|
EXPECT_GT(str_comp_filenames("file10", "file00"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file00", "file10"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file10", "file09"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file09", "file10"), 0);
|
2022-09-28 22:00:37 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("file13", "file37"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file37", "file13"), 0);
|
2022-12-30 00:11:57 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("file1.ext", "file09.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file09.ext", "file1.ext"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file1.ext", "file009.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file009.ext", "file1.ext"), 0);
|
|
|
|
|
EXPECT_EQ(str_comp_filenames("file0.ext", "file0.ext"), 0);
|
2022-09-28 22:00:37 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("file13.ext", "file37.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file37.ext", "file13.ext"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("FILE13.EXT", "file37.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file37.ext", "FILE13.EXT"), 0);
|
2023-01-02 20:20:49 +00:00
|
|
|
|
EXPECT_GT(str_comp_filenames("file10.ext", "file00.ext"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file00.ext", "file10.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file10.ext", "file09.ext"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file09.ext", "file10.ext"), 0);
|
2022-09-28 22:00:37 +00:00
|
|
|
|
EXPECT_LT(str_comp_filenames("file42", "file1337"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file1337", "file42"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file42.ext", "file1337.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file1337.ext", "file42.ext"), 0);
|
|
|
|
|
EXPECT_GT(str_comp_filenames("file4414520", "file2055"), 0);
|
|
|
|
|
EXPECT_LT(str_comp_filenames("file4414520", "file205523151812419"), 0);
|
|
|
|
|
}
|
2022-09-29 08:33:39 +00:00
|
|
|
|
|
|
|
|
|
TEST(Str, RightChar)
|
|
|
|
|
{
|
|
|
|
|
const char *pStr = "a bb ccc dddd eeeee";
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'a'), pStr);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'b'), pStr + 3);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'c'), pStr + 7);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'd'), pStr + 12);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, ' '), pStr + 19);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'e'), pStr + 24);
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, '\0'), pStr + str_length(pStr));
|
|
|
|
|
EXPECT_EQ(str_rchr(pStr, 'y'), nullptr);
|
|
|
|
|
}
|
2022-09-29 08:41:22 +00:00
|
|
|
|
|
|
|
|
|
TEST(Str, CountChar)
|
|
|
|
|
{
|
|
|
|
|
const char *pStr = "a bb ccc dddd eeeee";
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'a'), 1);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'b'), 2);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'c'), 3);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'd'), 4);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'e'), 5);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, ' '), 10);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, '\0'), 0);
|
|
|
|
|
EXPECT_EQ(str_countchr(pStr, 'y'), 0);
|
|
|
|
|
}
|
2023-05-02 15:36:01 +00:00
|
|
|
|
|
|
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
|
|
|
|
TEST(Str, WindowsUtf8WideConversion)
|
|
|
|
|
{
|
|
|
|
|
const char *apUtf8Strings[] = {
|
|
|
|
|
"",
|
|
|
|
|
"abc",
|
|
|
|
|
"a bb ccc dddd eeeee",
|
|
|
|
|
"öüä",
|
|
|
|
|
"привет Наташа",
|
|
|
|
|
"ąçęįǫų",
|
|
|
|
|
"DDNet最好了",
|
|
|
|
|
"aβい🐘"};
|
|
|
|
|
const wchar_t *apWideStrings[] = {
|
|
|
|
|
L"",
|
|
|
|
|
L"abc",
|
|
|
|
|
L"a bb ccc dddd eeeee",
|
|
|
|
|
L"öüä",
|
|
|
|
|
L"привет Наташа",
|
|
|
|
|
L"ąçęįǫų",
|
|
|
|
|
L"DDNet最好了",
|
|
|
|
|
L"aβい🐘"};
|
|
|
|
|
static_assert(std::size(apUtf8Strings) == std::size(apWideStrings));
|
|
|
|
|
for(size_t i = 0; i < std::size(apUtf8Strings); i++)
|
|
|
|
|
{
|
|
|
|
|
const std::string ConvertedUtf8 = windows_wide_to_utf8(apWideStrings[i]);
|
|
|
|
|
const std::wstring ConvertedWide = windows_utf8_to_wide(apUtf8Strings[i]);
|
|
|
|
|
EXPECT_STREQ(ConvertedUtf8.c_str(), apUtf8Strings[i]);
|
|
|
|
|
EXPECT_STREQ(ConvertedWide.c_str(), apWideStrings[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|