summaryrefslogtreecommitdiff
path: root/src/gtest-printers.cc
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-12-02 23:28:38 +0000
committerzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-12-02 23:28:38 +0000
commita8c1fdfb00d6f1751e119f5632a38b0498a8c1a7 (patch)
treea27bcfbc297ba0f3f3b2b6413166b587f4c8b351 /src/gtest-printers.cc
parent7707f53780847ec97b17df3e3ade2a4efc1516b1 (diff)
downloadgtest-a8c1fdfb00d6f1751e119f5632a38b0498a8c1a7.tar.gz
gtest-a8c1fdfb00d6f1751e119f5632a38b0498a8c1a7.tar.bz2
gtest-a8c1fdfb00d6f1751e119f5632a38b0498a8c1a7.tar.xz
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
Diffstat (limited to 'src/gtest-printers.cc')
-rw-r--r--src/gtest-printers.cc41
1 files changed, 29 insertions, 12 deletions
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<wchar_t>(c, os);
+ return PrintAsCharLiteralTo<wchar_t>(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<unsigned char>(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<unsigned char>(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<char>(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 << "\"";
}