#include "emitterutils.h" #include "exp.h" #include "indentation.h" #include "exceptions.h" #include "stringsource.h" #include #include namespace YAML { namespace Utils { namespace { bool IsPrintable(char ch) { return (0x20 <= ch && ch <= 0x7E); } bool IsValidPlainScalar(const std::string& str, bool inFlow) { // first check the start const RegEx& start = (inFlow ? Exp::PlainScalarInFlow : Exp::PlainScalar); if(!start.Matches(str)) return false; // and check the end for plain whitespace (which can't be faithfully kept in a plain scalar) if(!str.empty() && *str.rbegin() == ' ') return false; // then check until something is disallowed const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment) || (!Exp::Printable) || Exp::Break || Exp::Tab; StringCharSource buffer(str.c_str(), str.size()); while(buffer) { if(disallowed.Matches(buffer)) return false; ++buffer; } return true; } } bool WriteString(ostream& out, const std::string& str, bool inFlow) { if(IsValidPlainScalar(str, inFlow)) { out << str; return true; } else return WriteDoubleQuotedString(out, str); } bool WriteSingleQuotedString(ostream& out, const std::string& str) { out << "'"; for(std::size_t i=0;i(ch); out << str.str(); } } out << "\""; return true; } bool WriteLiteralString(ostream& out, const std::string& str, int indent) { out << "|\n"; out << IndentTo(indent); for(std::size_t i=0;i