From 3b73aa8bcd4d8fd14333d75928ac7cf60f7bc5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sawicz?= Date: Thu, 28 Jan 2021 14:55:54 +0100 Subject: [PATCH] Always quote strings with colons Otherwise they may get misinterpreted, or cause a syntax error even. --- src/emitterutils.cpp | 4 ++-- src/exp.h | 4 ++++ test/integration/load_node_test.cpp | 4 ++-- test/node/node_test.cpp | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 0113c45..6db35db 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::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; diff --git a/src/exp.h b/src/exp.h index 301449e..cb6491c 100644 --- a/src/exp.h +++ b/src/exp.h @@ -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; diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index 958e735..9d6d499 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -276,12 +276,12 @@ TEST(NodeTest, SpecialFlow) { std::vector 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 diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 4f577c8..17d2b7a 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -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);