From 13626af92af7642eba79837842c59602201d52a1 Mon Sep 17 00:00:00 2001 From: "Dr. Andre Vehreschild" <101638173+vehre-x41@users.noreply.github.com> Date: Thu, 21 Apr 2022 15:19:51 +0200 Subject: [PATCH] Fix escaping anchors in keys (#1101) --- src/emitterutils.cpp | 4 ++-- src/exp.h | 4 ++++ test/integration/emitter_test.cpp | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index c6ad5e5..6cdf6de 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -176,11 +176,11 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType, static const RegEx& disallowed_flow = Exp::EndScalarInFlow() | (Exp::BlankOrBreak() + Exp::Comment()) | Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() | - Exp::Tab(); + 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::Tab() | Exp::Ampersand(); const RegEx& disallowed = flowType == FlowType::Flow ? disallowed_flow : disallowed_block; diff --git a/src/exp.h b/src/exp.h index a0d6d76..c8837f0 100644 --- a/src/exp.h +++ b/src/exp.h @@ -117,6 +117,10 @@ inline const RegEx& ValueInJSONFlow() { static const RegEx e = RegEx(':'); return e; } +inline const RegEx& Ampersand() { + static const RegEx e = RegEx('&'); + return e; +} inline const RegEx Comment() { static const RegEx e = RegEx('#'); return e; diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index 560f631..53407c9 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -1624,6 +1624,15 @@ NodeB: k: [*k0, *k1])"); } +TEST_F(EmitterTest, AnchorEncoding) { + Node node; + node["--- &$ [*$]1"] = 1; + out << node; + ExpectEmit("\"--- &$ [*$]1\": 1"); + Node reparsed = YAML::Load(out.c_str()); + EXPECT_EQ(reparsed["--- &$ [*$]1"].as(), 1); +} + class EmitterErrorTest : public ::testing::Test { protected: void ExpectEmitError(const std::string& expectedError) { @@ -1694,5 +1703,6 @@ TEST_F(EmitterErrorTest, InvalidAlias) { ExpectEmitError(ErrorMsg::INVALID_ALIAS); } + } // namespace } // namespace YAML