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

This commit is contained in:
Jesse Beder
2012-05-25 19:33:34 -05:00
parent 772969270d
commit bc3f72b565
2 changed files with 16 additions and 17 deletions

View File

@@ -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<std::size_t N>
inline ostream_wrapper& operator << (ostream_wrapper& stream, const char (&str)[N]) {
stream.write(str, N-1);
return stream;
}

View File

@@ -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) {