From bc3f72b5656fc7284135765db7b5135d25cda858 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Fri, 25 May 2012 19:33:34 -0500 Subject: [PATCH] Switched the stream << for c-strings to take a templated array param (since we never stream user-built c-strings, only string literals). For this, refactored the escape character display --- include/yaml-cpp/ostream_wrapper.h | 9 +++++---- src/emitterutils.cpp | 24 +++++++++++------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/include/yaml-cpp/ostream_wrapper.h b/include/yaml-cpp/ostream_wrapper.h index b168c1c..a6d96c5 100644 --- a/include/yaml-cpp/ostream_wrapper.h +++ b/include/yaml-cpp/ostream_wrapper.h @@ -25,9 +25,9 @@ namespace YAML const char *str() const { if(m_pStream) { - return NULL; + return 0; } else { - m_buffer[m_pos] = NULL; + m_buffer[m_pos] = '\0'; return &m_buffer[0]; } } @@ -49,8 +49,9 @@ namespace YAML bool m_comment; }; - inline ostream_wrapper& operator << (ostream_wrapper& stream, const char *str) { - stream.write(str, std::strlen(str)); + template + inline ostream_wrapper& operator << (ostream_wrapper& stream, const char (&str)[N]) { + stream.write(str, N-1); return stream; } diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 0f2800c..09780d0 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -188,24 +188,22 @@ namespace YAML void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) { static const char hexDigits[] = "0123456789abcdef"; - char escSeq[] = "\\U00000000"; + out << "\\"; int digits = 8; - if (codePoint < 0xFF) { - escSeq[1] = 'x'; + if(codePoint < 0xFF) { + out << "x"; digits = 2; - } else if (codePoint < 0xFFFF) { - escSeq[1] = 'u'; + } else if(codePoint < 0xFFFF) { + out << "u"; digits = 4; - } + } else { + out << "U"; + digits = 8; + } // Write digits into the escape sequence - int i = 2; - for (; digits > 0; --digits, ++i) { - escSeq[i] = hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF]; - } - - escSeq[i] = 0; // terminate with NUL character - out << escSeq; + for (; digits > 0; --digits) + out << hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF]; } bool WriteAliasName(ostream_wrapper& out, const std::string& str) {