diff options
author | antonyzhilin <antonyzhilin@yandex-team.com> | 2024-07-05 19:56:10 +0300 |
---|---|---|
committer | antonyzhilin <antonyzhilin@yandex-team.com> | 2024-07-05 20:06:51 +0300 |
commit | 92272084057d874f12ce24f5a7950b867f7d50c2 (patch) | |
tree | 80fd973f61edfd5c1961c6dd024096b5f2822a5e /contrib/libs/yaml-cpp/src/emitterutils.cpp | |
parent | ada19b071f15ecd5d6784db9fd710f68bcccf310 (diff) | |
download | ydb-92272084057d874f12ce24f5a7950b867f7d50c2.tar.gz |
feat contrib: update yaml-cpp to 0.8.0
6e6348bacf1f4cc2d24d90954c63619ba9bee1f0
Diffstat (limited to 'contrib/libs/yaml-cpp/src/emitterutils.cpp')
-rw-r--r-- | contrib/libs/yaml-cpp/src/emitterutils.cpp | 112 |
1 files changed, 63 insertions, 49 deletions
diff --git a/contrib/libs/yaml-cpp/src/emitterutils.cpp b/contrib/libs/yaml-cpp/src/emitterutils.cpp index 147738ad8a..6cdf6de7e2 100644 --- a/contrib/libs/yaml-cpp/src/emitterutils.cpp +++ b/contrib/libs/yaml-cpp/src/emitterutils.cpp @@ -1,3 +1,4 @@ +#include <algorithm> #include <iomanip> #include <sstream> @@ -8,8 +9,8 @@ #include "regeximpl.h" #include "stringsource.h" #include "yaml-cpp/binary.h" // IWYU pragma: keep -#include "yaml-cpp/ostream_wrapper.h" #include "yaml-cpp/null.h" +#include "yaml-cpp/ostream_wrapper.h" namespace YAML { namespace Utils { @@ -134,12 +135,12 @@ void WriteCodePoint(ostream_wrapper& out, int codePoint) { if (codePoint < 0 || codePoint > 0x10FFFF) { codePoint = REPLACEMENT_CHARACTER; } - if (codePoint < 0x7F) { + if (codePoint <= 0x7F) { out << static_cast<char>(codePoint); - } else if (codePoint < 0x7FF) { + } else if (codePoint <= 0x7FF) { out << static_cast<char>(0xC0 | (codePoint >> 6)) << static_cast<char>(0x80 | (codePoint & 0x3F)); - } else if (codePoint < 0xFFFF) { + } else if (codePoint <= 0xFFFF) { out << static_cast<char>(0xE0 | (codePoint >> 12)) << static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)) << static_cast<char>(0x80 | (codePoint & 0x3F)); @@ -173,13 +174,13 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, // then check until something is disallowed static const RegEx& disallowed_flow = - Exp::EndScalarInFlow() || (Exp::BlankOrBreak() + Exp::Comment()) || - Exp::NotPrintable() || Exp::Utf8_ByteOrderMark() || Exp::Break() || - Exp::Tab(); + Exp::EndScalarInFlow() | (Exp::BlankOrBreak() + Exp::Comment()) | + Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() | + Exp::Tab() | Exp::Ampersand(); static const RegEx& disallowed_block = - Exp::EndScalar() || (Exp::BlankOrBreak() + Exp::Comment()) || - Exp::NotPrintable() || Exp::Utf8_ByteOrderMark() || Exp::Break() || - Exp::Tab(); + Exp::EndScalar() | (Exp::BlankOrBreak() + Exp::Comment()) | + Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() | + Exp::Tab() | Exp::Ampersand(); const RegEx& disallowed = flowType == FlowType::Flow ? disallowed_flow : disallowed_block; @@ -199,15 +200,10 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) { // TODO: check for non-printable characters? - for (std::size_t i = 0; i < str.size(); i++) { - if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) { - return false; - } - if (str[i] == '\n') { - return false; - } - } - return true; + return std::none_of(str.begin(), str.end(), [=](char ch) { + return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) || + (ch == '\n'); + }); } bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, @@ -217,28 +213,39 @@ bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType, } // TODO: check for non-printable characters? - for (std::size_t i = 0; i < str.size(); i++) { - if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) { - return false; - } - } - return true; + return std::none_of(str.begin(), str.end(), [=](char ch) { + return (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))); + }); +} + +std::pair<uint16_t, uint16_t> EncodeUTF16SurrogatePair(int codePoint) { + const uint32_t leadOffset = 0xD800 - (0x10000 >> 10); + + return { + leadOffset | (codePoint >> 10), + 0xDC00 | (codePoint & 0x3FF), + }; } -void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint) { +void WriteDoubleQuoteEscapeSequence(ostream_wrapper& out, int codePoint, StringEscaping::value stringEscapingStyle) { static const char hexDigits[] = "0123456789abcdef"; out << "\\"; int digits = 8; - if (codePoint < 0xFF) { + if (codePoint < 0xFF && stringEscapingStyle != StringEscaping::JSON) { out << "x"; digits = 2; } else if (codePoint < 0xFFFF) { out << "u"; digits = 4; - } else { + } else if (stringEscapingStyle != StringEscaping::JSON) { out << "U"; digits = 8; + } else { + auto surrogatePair = EncodeUTF16SurrogatePair(codePoint); + WriteDoubleQuoteEscapeSequence(out, surrogatePair.first, stringEscapingStyle); + WriteDoubleQuoteEscapeSequence(out, surrogatePair.second, stringEscapingStyle); + return; } // Write digits into the escape sequence @@ -258,7 +265,7 @@ bool WriteAliasName(ostream_wrapper& out, const std::string& str) { } return true; } -} +} // namespace StringFormat::value ComputeStringFormat(const std::string& str, EMITTER_MANIP strFormat, @@ -310,7 +317,7 @@ bool WriteSingleQuotedString(ostream_wrapper& out, const std::string& str) { } bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, - bool escapeNonAscii) { + StringEscaping::value stringEscaping) { out << "\""; int codePoint; for (std::string::const_iterator i = str.begin(); @@ -334,16 +341,19 @@ bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, case '\b': out << "\\b"; break; + case '\f': + out << "\\f"; + break; default: if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) { // Control characters and non-breaking space - WriteDoubleQuoteEscapeSequence(out, codePoint); + WriteDoubleQuoteEscapeSequence(out, codePoint, stringEscaping); } else if (codePoint == 0xFEFF) { // Byte order marks (ZWNS) should be // escaped (YAML 1.2, sec. 5.2) - WriteDoubleQuoteEscapeSequence(out, codePoint); - } else if (escapeNonAscii && codePoint > 0x7E) { - WriteDoubleQuoteEscapeSequence(out, codePoint); + WriteDoubleQuoteEscapeSequence(out, codePoint, stringEscaping); + } else if (stringEscaping == StringEscaping::NonAscii && codePoint > 0x7E) { + WriteDoubleQuoteEscapeSequence(out, codePoint, stringEscaping); } else { WriteCodePoint(out, codePoint); } @@ -356,37 +366,41 @@ bool WriteDoubleQuotedString(ostream_wrapper& out, const std::string& str, bool WriteLiteralString(ostream_wrapper& out, const std::string& str, std::size_t indent) { out << "|\n"; - out << IndentTo(indent); int codePoint; for (std::string::const_iterator i = str.begin(); GetNextCodePointAndAdvance(codePoint, i, str.end());) { if (codePoint == '\n') { - out << "\n" << IndentTo(indent); + out << "\n"; } else { + out<< IndentTo(indent); WriteCodePoint(out, codePoint); } } return true; } -bool WriteChar(ostream_wrapper& out, char ch) { +bool WriteChar(ostream_wrapper& out, char ch, StringEscaping::value stringEscapingStyle) { if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')) { out << ch; } else if (ch == '\"') { - out << "\"\\\"\""; + out << R"("\"")"; } else if (ch == '\t') { - out << "\"\\t\""; + out << R"("\t")"; } else if (ch == '\n') { - out << "\"\\n\""; + out << R"("\n")"; } else if (ch == '\b') { - out << "\"\\b\""; + out << R"("\b")"; + } else if (ch == '\r') { + out << R"("\r")"; + } else if (ch == '\f') { + out << R"("\f")"; } else if (ch == '\\') { - out << "\"\\\\\""; - } else if ((0x20 <= ch && ch <= 0x7e) || ch == ' ') { + out << R"("\\")"; + } else if (0x20 <= ch && ch <= 0x7e) { out << "\"" << ch << "\""; } else { out << "\""; - WriteDoubleQuoteEscapeSequence(out, ch); + WriteDoubleQuoteEscapeSequence(out, ch, stringEscapingStyle); out << "\""; } return true; @@ -401,8 +415,8 @@ bool WriteComment(ostream_wrapper& out, const std::string& str, for (std::string::const_iterator i = str.begin(); GetNextCodePointAndAdvance(codePoint, i, str.end());) { if (codePoint == '\n') { - out << "\n" << IndentTo(curIndent) << "#" - << Indentation(postCommentIndent); + out << "\n" + << IndentTo(curIndent) << "#" << Indentation(postCommentIndent); out.set_comment(); } else { WriteCodePoint(out, codePoint); @@ -476,8 +490,8 @@ bool WriteTagWithPrefix(ostream_wrapper& out, const std::string& prefix, bool WriteBinary(ostream_wrapper& out, const Binary& binary) { WriteDoubleQuotedString(out, EncodeBase64(binary.data(), binary.size()), - false); + StringEscaping::None); return true; } -} -} +} // namespace Utils +} // namespace YAML |