Always quote strings with colons

Otherwise they may get misinterpreted, or cause a syntax error even.
This commit is contained in:
Michał Sawicz 2021-01-28 14:55:54 +01:00
parent a6bbe0e50a
commit 3b73aa8bcd
No known key found for this signature in database
GPG Key ID: 69EFECD3C61D2645
4 changed files with 14 additions and 4 deletions

View File

@ -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::Colon();
static const RegEx& disallowed_block =
Exp::EndScalar() | (Exp::BlankOrBreak() + Exp::Comment()) |
Exp::NotPrintable() | Exp::Utf8_ByteOrderMark() | Exp::Break() |
Exp::Tab();
Exp::Tab() | Exp::Colon();
const RegEx& disallowed =
flowType == FlowType::Flow ? disallowed_flow : disallowed_block;

View File

@ -32,6 +32,10 @@ inline const RegEx& Tab() {
static const RegEx e = RegEx('\t');
return e;
}
inline const RegEx& Colon() {
static const RegEx e = RegEx(':');
return e;
}
inline const RegEx& Blank() {
static const RegEx e = Space() | Tab();
return e;

View File

@ -276,12 +276,12 @@ TEST(NodeTest, SpecialFlow) {
std::vector<SingleNodeTestCase> tests = {
{"[:]", NodeType::Sequence, 1, "[{~: ~}]"},
{"[a:]", NodeType::Sequence, 1, "[{a: ~}]"},
{"[:a]", NodeType::Sequence, 1, "[:a]"},
{"[:a]", NodeType::Sequence, 1, "[\":a\"]"},
{"[,]", NodeType::Sequence, 1, "[~]"},
{"[a:,]", NodeType::Sequence, 1, "[{a: ~}]"},
{"{:}", NodeType::Map, 1, "{~: ~}"},
{"{a:}", NodeType::Map, 1, "{a: ~}"},
{"{:a}", NodeType::Map, 1, "{:a: ~}"},
{"{:a}", NodeType::Map, 1, "{\":a\": ~}"},
{"{,}", NodeType::Map, 1, "{~: ~}"},
{"{a:,}", NodeType::Map, 1, "{a: ~}"},
//testcase for the trailing TAB of scalar

View File

@ -663,6 +663,12 @@ class NodeEmitterTest : public ::testing::Test {
}
};
TEST_F(NodeEmitterTest, StringWithColons) {
Node node{"String:with:colons"};
ExpectOutput("\"String:with:colons\"", node);
}
TEST_F(NodeEmitterTest, SimpleFlowSeqNode) {
Node node;
node.SetStyle(EmitterStyle::Flow);