Skip to content

Commit 13626af

Browse files
authored
Fix escaping anchors in keys (jbeder#1101)
1 parent 4c982d5 commit 13626af

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/emitterutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
176176
static const RegEx& disallowed_flow =
177177
Exp::EndScalarInFlow() | (Exp::BlankOrBreak() + Exp::Comment()) |
178178
Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() |
179-
Exp::Tab();
179+
Exp::Tab() | Exp::Ampersand();
180180
static const RegEx& disallowed_block =
181181
Exp::EndScalar() | (Exp::BlankOrBreak() + Exp::Comment()) |
182182
Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() |
183-
Exp::Tab();
183+
Exp::Tab() | Exp::Ampersand();
184184
const RegEx& disallowed =
185185
flowType == FlowType::Flow ? disallowed_flow : disallowed_block;
186186

src/exp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ inline const RegEx& ValueInJSONFlow() {
117117
static const RegEx e = RegEx(':');
118118
return e;
119119
}
120+
inline const RegEx& Ampersand() {
121+
static const RegEx e = RegEx('&');
122+
return e;
123+
}
120124
inline const RegEx Comment() {
121125
static const RegEx e = RegEx('#');
122126
return e;

test/integration/emitter_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,15 @@ TEST_F(EmitterTest, ComplexFlowSeqEmbeddingAMapUsingAliases3b) {
16241624
k: [*k0, *k1])");
16251625
}
16261626

1627+
TEST_F(EmitterTest, AnchorEncoding) {
1628+
Node node;
1629+
node["--- &$ [*$]1"] = 1;
1630+
out << node;
1631+
ExpectEmit("\"--- &$ [*$]1\": 1");
1632+
Node reparsed = YAML::Load(out.c_str());
1633+
EXPECT_EQ(reparsed["--- &$ [*$]1"].as<int>(), 1);
1634+
}
1635+
16271636
class EmitterErrorTest : public ::testing::Test {
16281637
protected:
16291638
void ExpectEmitError(const std::string& expectedError) {
@@ -1694,5 +1703,6 @@ TEST_F(EmitterErrorTest, InvalidAlias) {
16941703

16951704
ExpectEmitError(ErrorMsg::INVALID_ALIAS);
16961705
}
1706+
16971707
} // namespace
16981708
} // namespace YAML

0 commit comments

Comments
 (0)