From a8c1fdfb00d6f1751e119f5632a38b0498a8c1a7 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 2 Dec 2010 23:28:38 +0000 Subject: Makes gtest print string literals correctly when it contains \x escape sequences. Contributed by Yair Chuchem. git-svn-id: http://googletest.googlecode.com/svn/trunk@525 861a406c-534a-0410-8894-cb66d6ee9925 --- src/gtest-printers.cc | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'src/gtest-printers.cc') diff --git a/src/gtest-printers.cc b/src/gtest-printers.cc index 147f8b2..c85d582 100644 --- a/src/gtest-printers.cc +++ b/src/gtest-printers.cc @@ -194,25 +194,25 @@ static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { return kSpecialEscape; } -// Prints a char as if it's part of a string literal, escaping it when -// necessary. -static void PrintAsWideStringLiteralTo(wchar_t c, ostream* os) { +// Prints a char c as if it's part of a string literal, escaping it when +// necessary; returns how c was formatted. +static CharFormat PrintAsWideStringLiteralTo(wchar_t c, ostream* os) { switch (c) { case L'\'': *os << "'"; - break; + return kAsIs; case L'"': *os << "\\\""; - break; + return kSpecialEscape; default: - PrintAsCharLiteralTo(c, os); + return PrintAsCharLiteralTo(c, os); } } -// Prints a char as if it's part of a string literal, escaping it when -// necessary. -static void PrintAsNarrowStringLiteralTo(char c, ostream* os) { - PrintAsWideStringLiteralTo(static_cast(c), os); +// Prints a char c as if it's part of a string literal, escaping it when +// necessary; returns how c was formatted. +static CharFormat PrintAsNarrowStringLiteralTo(char c, ostream* os) { + return PrintAsWideStringLiteralTo(static_cast(c), os); } // Prints a wide or narrow character c and its code. '\0' is printed @@ -263,8 +263,16 @@ void PrintTo(wchar_t wc, ostream* os) { // and may not be null-terminated. static void PrintCharsAsStringTo(const char* begin, size_t len, ostream* os) { *os << "\""; + bool is_previous_hex = false; for (size_t index = 0; index < len; ++index) { - PrintAsNarrowStringLiteralTo(begin[index], os); + const char cur = begin[index]; + if (is_previous_hex && IsXDigit(cur)) { + // Previous character is of '\x..' form and this character can be + // interpreted as another hexadecimal digit in its number. Break string to + // disambiguate. + *os << "\" \""; + } + is_previous_hex = PrintAsNarrowStringLiteralTo(cur, os) == kHexEscape; } *os << "\""; } @@ -280,8 +288,17 @@ void UniversalPrintArray(const char* begin, size_t len, ostream* os) { static void PrintWideCharsAsStringTo(const wchar_t* begin, size_t len, ostream* os) { *os << "L\""; + bool is_previous_hex = false; for (size_t index = 0; index < len; ++index) { - PrintAsWideStringLiteralTo(begin[index], os); + const wchar_t cur = begin[index]; + if (is_previous_hex && 0 <= cur && cur < 128 && + IsXDigit(static_cast(cur))) { + // Previous character is of '\x..' form and this character can be + // interpreted as another hexadecimal digit in its number. Break string to + // disambiguate. + *os << "\" L\""; + } + is_previous_hex = PrintAsWideStringLiteralTo(cur, os) == kHexEscape; } *os << "\""; } -- cgit v1.2.3