| 249 | // also properly escaped using the standard C++ escape sequence. |
| 250 | template <typename Char> |
| 251 | void PrintCharAndCodeTo(Char c, ostream* os) { |
| 252 | // First, print c as a literal in the most readable form we can find. |
| 253 | *os << GetCharWidthPrefix(c) << "'"; |
| 254 | const CharFormat format = PrintAsCharLiteralTo(c, os); |
| 255 | *os << "'"; |
| 256 | |
| 257 | // To aid user debugging, we also print c's code in decimal, unless |
| 258 | // it's 0 (in which case c was printed as '\\0', making the code |
| 259 | // obvious). |
| 260 | if (c == 0) return; |
| 261 | *os << " (" << static_cast<int>(c); |
| 262 | |
| 263 | // For more convenience, we print c's code again in hexadecimal, |
| 264 | // unless c was already printed in the form '\x##' or the code is in |
| 265 | // [1, 9]. |
| 266 | if (format == kHexEscape || (1 <= c && c <= 9)) { |
| 267 | // Do nothing. |
| 268 | } else { |
| 269 | *os << ", 0x" << String::FormatHexInt(static_cast<int>(c)); |
| 270 | } |
| 271 | *os << ")"; |
| 272 | } |
| 273 | |
| 274 | void PrintTo(unsigned char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } |
| 275 | void PrintTo(signed char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } |
no test coverage detected