mirror of https://github.com/google/googletest.git
Merge pull request #5046 from UditDewan:fix/print-invalid-char-code-units
PiperOrigin-RevId: 968678073 Change-Id: I669221b694231e385acb9a0bf6640e6f1c319e1e
This commit is contained in:
commit
7260682388
|
|
@ -522,17 +522,14 @@ inline void PrintTo(bool x, ::std::ostream* os) {
|
||||||
GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
|
GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
|
||||||
|
|
||||||
GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
|
GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
|
||||||
inline void PrintTo(char16_t c, ::std::ostream* os) {
|
|
||||||
// TODO(b/418738869): Incorrect for values not representing valid codepoints.
|
// Overloads for the UTF-8 and UTF-16 code unit types. A code unit that
|
||||||
// Also see https://github.com/google/googletest/issues/4762.
|
// encodes a code point all by itself is printed with the U+XXXX notation;
|
||||||
PrintTo(static_cast<char32_t>(c), os);
|
// one that does not (any non-ASCII char8_t, and the UTF-16 surrogates) is
|
||||||
}
|
// printed as a code unit instead, the way wchar_t is.
|
||||||
|
GTEST_API_ void PrintTo(char16_t c, ::std::ostream* os);
|
||||||
#ifdef __cpp_lib_char8_t
|
#ifdef __cpp_lib_char8_t
|
||||||
inline void PrintTo(char8_t c, ::std::ostream* os) {
|
GTEST_API_ void PrintTo(char8_t c, ::std::ostream* os);
|
||||||
// TODO(b/418738869): Incorrect for values not representing valid codepoints.
|
|
||||||
// Also see https://github.com/google/googletest/issues/4762.
|
|
||||||
PrintTo(static_cast<char32_t>(c), os);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// gcc/clang __{u,}int128_t
|
// gcc/clang __{u,}int128_t
|
||||||
|
|
|
||||||
|
|
@ -286,6 +286,28 @@ void PrintTo(char32_t c, ::std::ostream* os) {
|
||||||
<< static_cast<uint32_t>(c);
|
<< static_cast<uint32_t>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// char8_t and char16_t hold code units, not code points, so the U+XXXX
|
||||||
|
// notation only applies to the values that encode a code point on their own.
|
||||||
|
// The rest -- non-ASCII char8_t, which are always part of a multi-unit UTF-8
|
||||||
|
// sequence, and the UTF-16 surrogates -- are printed as code units.
|
||||||
|
void PrintTo(char16_t c, ::std::ostream* os) {
|
||||||
|
if (c >= 0xD800 && c <= 0xDFFF) {
|
||||||
|
PrintCharAndCodeTo(c, os);
|
||||||
|
} else {
|
||||||
|
PrintTo(static_cast<char32_t>(c), os);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cpp_lib_char8_t
|
||||||
|
void PrintTo(char8_t c, ::std::ostream* os) {
|
||||||
|
if (c >= 0x80) {
|
||||||
|
PrintCharAndCodeTo(c, os);
|
||||||
|
} else {
|
||||||
|
PrintTo(static_cast<char32_t>(c), os);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// gcc/clang __{u,}int128_t
|
// gcc/clang __{u,}int128_t
|
||||||
#if defined(__SIZEOF_INT128__)
|
#if defined(__SIZEOF_INT128__)
|
||||||
void PrintTo(__uint128_t v, ::std::ostream* os) {
|
void PrintTo(__uint128_t v, ::std::ostream* os) {
|
||||||
|
|
|
||||||
|
|
@ -396,12 +396,24 @@ TEST(PrintCharTest, UnsignedChar) {
|
||||||
EXPECT_EQ("'b' (98, 0x62)", Print(static_cast<unsigned char>('b')));
|
EXPECT_EQ("'b' (98, 0x62)", Print(static_cast<unsigned char>('b')));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrintCharTest, Char16) { EXPECT_EQ("U+0041", Print(u'A')); }
|
TEST(PrintCharTest, Char16) {
|
||||||
|
EXPECT_EQ("U+0041", Print(u'A'));
|
||||||
|
EXPECT_EQ("U+754C", Print(u'界'));
|
||||||
|
// Surrogates are not code points, so they are printed as code units.
|
||||||
|
EXPECT_EQ("u'\\xD800' (55296)", Print(static_cast<char16_t>(0xD800)));
|
||||||
|
EXPECT_EQ("u'\\xDFFF' (57343)", Print(static_cast<char16_t>(0xDFFF)));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PrintCharTest, Char32) { EXPECT_EQ("U+0041", Print(U'A')); }
|
TEST(PrintCharTest, Char32) { EXPECT_EQ("U+0041", Print(U'A')); }
|
||||||
|
|
||||||
#ifdef __cpp_lib_char8_t
|
#ifdef __cpp_lib_char8_t
|
||||||
TEST(PrintCharTest, Char8) { EXPECT_EQ("U+0041", Print(u8'A')); }
|
TEST(PrintCharTest, Char8) {
|
||||||
|
EXPECT_EQ("U+0041", Print(u8'A'));
|
||||||
|
// Only ASCII code units encode a code point on their own; the rest are
|
||||||
|
// printed as code units.
|
||||||
|
EXPECT_EQ("u8'\\x80' (128)", Print(static_cast<char8_t>(0x80)));
|
||||||
|
EXPECT_EQ("u8'\\xFF' (255)", Print(static_cast<char8_t>(0xFF)));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Tests printing other simple, built-in types.
|
// Tests printing other simple, built-in types.
|
||||||
|
|
@ -456,7 +468,7 @@ TEST(PrintBuiltInTypeTest, Integer) {
|
||||||
#ifdef __cpp_lib_char8_t
|
#ifdef __cpp_lib_char8_t
|
||||||
EXPECT_EQ("U+0000",
|
EXPECT_EQ("U+0000",
|
||||||
Print(std::numeric_limits<char8_t>::min())); // char8_t
|
Print(std::numeric_limits<char8_t>::min())); // char8_t
|
||||||
EXPECT_EQ("U+00FF",
|
EXPECT_EQ("u8'\\xFF' (255)",
|
||||||
Print(std::numeric_limits<char8_t>::max())); // char8_t
|
Print(std::numeric_limits<char8_t>::max())); // char8_t
|
||||||
#endif
|
#endif
|
||||||
EXPECT_EQ("U+0000",
|
EXPECT_EQ("U+0000",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue