fix issue501: handle the long key properly.

This commit is contained in:
dota17 2020-07-07 10:34:57 +08:00
parent 6d5cfab5fd
commit 64da94dd73
2 changed files with 9 additions and 2 deletions

View File

@ -695,7 +695,7 @@ Emitter& Emitter::Write(const std::string& str) {
Utils::ComputeStringFormat(str, m_pState->GetStringFormat(),
m_pState->CurGroupFlowType(), escapeNonAscii);
if (strFormat == StringFormat::Literal)
if (strFormat == StringFormat::Literal || str.size() > 1024)
m_pState->SetMapKeyFormat(YAML::LongKey, FmtScope::Local);
PrepareNode(EmitterNodeType::Scalar);

View File

@ -266,6 +266,8 @@ TEST_F(EmitterTest, SimpleLongKey) {
}
TEST_F(EmitterTest, SingleLongKey) {
const std::string& shortKey = std::string(1024, 'a');
const std::string& longKey = std::string(1025, 'a');
out << BeginMap;
out << Key << "age";
out << Value << "24";
@ -273,9 +275,14 @@ TEST_F(EmitterTest, SingleLongKey) {
out << Value << "5'9\"";
out << Key << "weight";
out << Value << 145;
out << Key << shortKey;
out << Value << "1";
out << Key << longKey;
out << Value << "1";
out << EndMap;
ExpectEmit("age: 24\n? height\n: 5'9\"\nweight: 145");
ExpectEmit("age: 24\n? height\n: 5'9\"\nweight: 145\n" + shortKey +
": 1\n? " + longKey + "\n: 1");
}
TEST_F(EmitterTest, ComplexLongKey) {