Skip to content

Commit 51ce663

Browse files
authored
Handle a key with length over 1024 as a long key. (jbeder#916)
Fixes jbeder#501
1 parent 3331528 commit 51ce663

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/emitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ Emitter& Emitter::Write(const std::string& str) {
708708
Utils::ComputeStringFormat(str, m_pState->GetStringFormat(),
709709
m_pState->CurGroupFlowType(), stringEscaping == StringEscaping::NonAscii);
710710

711-
if (strFormat == StringFormat::Literal)
711+
if (strFormat == StringFormat::Literal || str.size() > 1024)
712712
m_pState->SetMapKeyFormat(YAML::LongKey, FmtScope::Local);
713713

714714
PrepareNode(EmitterNodeType::Scalar);

test/integration/emitter_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,23 @@ TEST_F(EmitterTest, SimpleLongKey) {
266266
}
267267

268268
TEST_F(EmitterTest, SingleLongKey) {
269+
const std::string shortKey(1024, 'a');
270+
const std::string longKey(1025, 'a');
269271
out << BeginMap;
270272
out << Key << "age";
271273
out << Value << "24";
272274
out << LongKey << Key << "height";
273275
out << Value << "5'9\"";
274276
out << Key << "weight";
275277
out << Value << 145;
278+
out << Key << shortKey;
279+
out << Value << "1";
280+
out << Key << longKey;
281+
out << Value << "1";
276282
out << EndMap;
277283

278-
ExpectEmit("age: 24\n? height\n: 5'9\"\nweight: 145");
284+
ExpectEmit("age: 24\n? height\n: 5'9\"\nweight: 145\n" + shortKey +
285+
": 1\n? " + longKey + "\n: 1");
279286
}
280287

281288
TEST_F(EmitterTest, ComplexLongKey) {

0 commit comments

Comments
 (0)