mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Split block map simple/long key for both key/value
This commit is contained in:
@@ -95,8 +95,10 @@ namespace YAML
|
|||||||
void BlockSeqPrepareNode(EmitterNodeType::value child);
|
void BlockSeqPrepareNode(EmitterNodeType::value child);
|
||||||
void FlowMapPrepareNode(EmitterNodeType::value child);
|
void FlowMapPrepareNode(EmitterNodeType::value child);
|
||||||
void BlockMapPrepareNode(EmitterNodeType::value child);
|
void BlockMapPrepareNode(EmitterNodeType::value child);
|
||||||
void BlockMapPrepareKey(EmitterNodeType::value child);
|
void BlockMapPrepareLongKey(EmitterNodeType::value child);
|
||||||
void BlockMapPrepareValue(EmitterNodeType::value child);
|
void BlockMapPrepareSimpleKey(EmitterNodeType::value child);
|
||||||
|
void BlockMapPrepareLongKeyValue(EmitterNodeType::value child);
|
||||||
|
void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child);
|
||||||
|
|
||||||
void SpaceOrIndentTo(bool requireSpace, unsigned indent);
|
void SpaceOrIndentTo(bool requireSpace, unsigned indent);
|
||||||
|
|
||||||
|
@@ -360,13 +360,23 @@ namespace YAML
|
|||||||
{
|
{
|
||||||
const std::size_t childCount = m_pState->CurGroupChildCount();
|
const std::size_t childCount = m_pState->CurGroupChildCount();
|
||||||
|
|
||||||
if(childCount % 2 == 0)
|
if(childCount % 2 == 0) {
|
||||||
BlockMapPrepareKey(child);
|
if(m_pState->GetMapKeyFormat() == LongKey || child == EmitterNodeType::BlockSeq || child == EmitterNodeType::BlockMap)
|
||||||
|
m_pState->SetLongKey();
|
||||||
|
|
||||||
|
if(m_pState->CurGroupLongKey())
|
||||||
|
BlockMapPrepareLongKey(child);
|
||||||
else
|
else
|
||||||
BlockMapPrepareValue(child);
|
BlockMapPrepareSimpleKey(child);
|
||||||
|
} else {
|
||||||
|
if(m_pState->CurGroupLongKey())
|
||||||
|
BlockMapPrepareLongKeyValue(child);
|
||||||
|
else
|
||||||
|
BlockMapPrepareSimpleKeyValue(child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Emitter::BlockMapPrepareKey(EmitterNodeType::value child)
|
void Emitter::BlockMapPrepareLongKey(EmitterNodeType::value child)
|
||||||
{
|
{
|
||||||
const unsigned curIndent = m_pState->CurIndent();
|
const unsigned curIndent = m_pState->CurIndent();
|
||||||
const std::size_t childCount = m_pState->CurGroupChildCount();
|
const std::size_t childCount = m_pState->CurGroupChildCount();
|
||||||
@@ -378,7 +388,39 @@ namespace YAML
|
|||||||
if(childCount > 0) {
|
if(childCount > 0) {
|
||||||
m_stream << "\n";
|
m_stream << "\n";
|
||||||
}
|
}
|
||||||
if(false /* long key */) {
|
if(m_stream.comment()) {
|
||||||
|
m_stream << "\n";
|
||||||
|
}
|
||||||
|
m_stream << IndentTo(curIndent);
|
||||||
|
m_stream << "?";
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(child) {
|
||||||
|
case EmitterNodeType::None:
|
||||||
|
break;
|
||||||
|
case EmitterNodeType::Property:
|
||||||
|
case EmitterNodeType::Scalar:
|
||||||
|
case EmitterNodeType::FlowSeq:
|
||||||
|
case EmitterNodeType::FlowMap:
|
||||||
|
SpaceOrIndentTo(m_pState->HasBegunContent(), curIndent);
|
||||||
|
break;
|
||||||
|
case EmitterNodeType::BlockSeq:
|
||||||
|
case EmitterNodeType::BlockMap:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emitter::BlockMapPrepareSimpleKey(EmitterNodeType::value child)
|
||||||
|
{
|
||||||
|
const unsigned curIndent = m_pState->CurIndent();
|
||||||
|
const std::size_t childCount = m_pState->CurGroupChildCount();
|
||||||
|
|
||||||
|
if(child == EmitterNodeType::None)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(!m_pState->HasBegunNode()) {
|
||||||
|
if(childCount > 0) {
|
||||||
|
m_stream << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,16 +439,37 @@ namespace YAML
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Emitter::BlockMapPrepareValue(EmitterNodeType::value child)
|
void Emitter::BlockMapPrepareLongKeyValue(EmitterNodeType::value child)
|
||||||
{
|
{
|
||||||
const unsigned curIndent = m_pState->CurIndent();
|
const unsigned curIndent = m_pState->CurIndent();
|
||||||
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
|
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
|
||||||
|
|
||||||
if(!m_pState->HasBegunNode()) {
|
if(!m_pState->HasBegunNode()) {
|
||||||
if(false /* was long key */) {
|
|
||||||
} else {
|
|
||||||
m_stream << ":";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch(child) {
|
||||||
|
case EmitterNodeType::None:
|
||||||
|
break;
|
||||||
|
case EmitterNodeType::Property:
|
||||||
|
case EmitterNodeType::Scalar:
|
||||||
|
case EmitterNodeType::FlowSeq:
|
||||||
|
case EmitterNodeType::FlowMap:
|
||||||
|
SpaceOrIndentTo(true, nextIndent);
|
||||||
|
break;
|
||||||
|
case EmitterNodeType::BlockSeq:
|
||||||
|
case EmitterNodeType::BlockMap:
|
||||||
|
m_stream << "\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emitter::BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child)
|
||||||
|
{
|
||||||
|
const unsigned curIndent = m_pState->CurIndent();
|
||||||
|
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
|
||||||
|
|
||||||
|
if(!m_pState->HasBegunNode()) {
|
||||||
|
m_stream << ":";
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(child) {
|
switch(child) {
|
||||||
|
@@ -58,12 +58,26 @@ namespace YAML
|
|||||||
m_hasNonContent = true;
|
m_hasNonContent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EmitterState::SetLongKey()
|
||||||
|
{
|
||||||
|
assert(!m_groups.empty());
|
||||||
|
if(m_groups.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
assert(m_groups.top().type == GroupType::Map);
|
||||||
|
assert(m_groups.top().flowType == FlowType::Block);
|
||||||
|
m_groups.top().longKey = true;
|
||||||
|
}
|
||||||
|
|
||||||
void EmitterState::StartedNode()
|
void EmitterState::StartedNode()
|
||||||
{
|
{
|
||||||
if(m_groups.empty())
|
if(m_groups.empty()) {
|
||||||
m_docCount++;
|
m_docCount++;
|
||||||
else
|
} else {
|
||||||
m_groups.top().childCount++;
|
m_groups.top().childCount++;
|
||||||
|
if(m_groups.top().childCount % 2 == 0)
|
||||||
|
m_groups.top().longKey = false;
|
||||||
|
}
|
||||||
|
|
||||||
m_hasAnchor = false;
|
m_hasAnchor = false;
|
||||||
m_hasTag = false;
|
m_hasTag = false;
|
||||||
@@ -166,6 +180,11 @@ namespace YAML
|
|||||||
return m_groups.empty() ? m_docCount : m_groups.top().childCount;
|
return m_groups.empty() ? m_docCount : m_groups.top().childCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EmitterState::CurGroupLongKey() const
|
||||||
|
{
|
||||||
|
return m_groups.empty() ? false : m_groups.top().longKey;
|
||||||
|
}
|
||||||
|
|
||||||
void EmitterState::ClearModifiedSettings()
|
void EmitterState::ClearModifiedSettings()
|
||||||
{
|
{
|
||||||
m_modifiedSettings.clear();
|
m_modifiedSettings.clear();
|
||||||
|
@@ -37,6 +37,7 @@ namespace YAML
|
|||||||
void SetAnchor();
|
void SetAnchor();
|
||||||
void SetTag();
|
void SetTag();
|
||||||
void SetNonContent();
|
void SetNonContent();
|
||||||
|
void SetLongKey();
|
||||||
void StartedScalar();
|
void StartedScalar();
|
||||||
void StartedGroup(GroupType::value type);
|
void StartedGroup(GroupType::value type);
|
||||||
void EndedGroup(GroupType::value type);
|
void EndedGroup(GroupType::value type);
|
||||||
@@ -48,6 +49,7 @@ namespace YAML
|
|||||||
FlowType::value CurGroupFlowType() const;
|
FlowType::value CurGroupFlowType() const;
|
||||||
int CurGroupIndent() const;
|
int CurGroupIndent() const;
|
||||||
std::size_t CurGroupChildCount() const;
|
std::size_t CurGroupChildCount() const;
|
||||||
|
bool CurGroupLongKey() const;
|
||||||
|
|
||||||
int CurIndent() const { return m_curIndent; }
|
int CurIndent() const { return m_curIndent; }
|
||||||
bool HasAnchor() const { return m_hasAnchor; }
|
bool HasAnchor() const { return m_hasAnchor; }
|
||||||
@@ -127,12 +129,13 @@ namespace YAML
|
|||||||
SettingChanges m_globalModifiedSettings;
|
SettingChanges m_globalModifiedSettings;
|
||||||
|
|
||||||
struct Group {
|
struct Group {
|
||||||
explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0) {}
|
explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0), longKey(false) {}
|
||||||
|
|
||||||
GroupType::value type;
|
GroupType::value type;
|
||||||
FlowType::value flowType;
|
FlowType::value flowType;
|
||||||
int indent;
|
int indent;
|
||||||
std::size_t childCount;
|
std::size_t childCount;
|
||||||
|
bool longKey;
|
||||||
|
|
||||||
SettingChanges modifiedSettings;
|
SettingChanges modifiedSettings;
|
||||||
|
|
||||||
|
@@ -4,17 +4,12 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
YAML::Emitter out;
|
YAML::Emitter out;
|
||||||
out << YAML::Comment("Hello");
|
out << YAML::BeginMap;
|
||||||
out << YAML::BeginSeq;
|
out << YAML::BeginMap;
|
||||||
out << YAML::Comment("Hello");
|
out << "a" << "b";
|
||||||
out << YAML::Anchor("a") << YAML::Comment("anchor") << "item 1" << YAML::Comment("a");
|
out << YAML::EndMap;
|
||||||
out << YAML::BeginMap << YAML::Comment("b");
|
out << "c";
|
||||||
out << "pens" << YAML::Comment("foo") << 2.3 << YAML::Comment("bar");
|
out << YAML::EndMap;
|
||||||
out << "pencils" << 15;
|
|
||||||
out << YAML::EndMap << YAML::Comment("monkey");
|
|
||||||
out << "item 2";
|
|
||||||
out << YAML::EndSeq;
|
|
||||||
out << YAML::Comment("end");
|
|
||||||
|
|
||||||
std::cout << out.c_str() << "\n";
|
std::cout << out.c_str() << "\n";
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user