Split block map simple/long key for both key/value

This commit is contained in:
Jesse Beder
2012-05-22 14:20:50 -05:00
parent 952fe51c73
commit 41e4cd3308
5 changed files with 109 additions and 27 deletions

View File

@@ -95,8 +95,10 @@ namespace YAML
void BlockSeqPrepareNode(EmitterNodeType::value child);
void FlowMapPrepareNode(EmitterNodeType::value child);
void BlockMapPrepareNode(EmitterNodeType::value child);
void BlockMapPrepareKey(EmitterNodeType::value child);
void BlockMapPrepareValue(EmitterNodeType::value child);
void BlockMapPrepareLongKey(EmitterNodeType::value child);
void BlockMapPrepareSimpleKey(EmitterNodeType::value child);
void BlockMapPrepareLongKeyValue(EmitterNodeType::value child);
void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child);
void SpaceOrIndentTo(bool requireSpace, unsigned indent);

View File

@@ -360,13 +360,23 @@ namespace YAML
{
const std::size_t childCount = m_pState->CurGroupChildCount();
if(childCount % 2 == 0)
BlockMapPrepareKey(child);
else
BlockMapPrepareValue(child);
if(childCount % 2 == 0) {
if(m_pState->GetMapKeyFormat() == LongKey || child == EmitterNodeType::BlockSeq || child == EmitterNodeType::BlockMap)
m_pState->SetLongKey();
if(m_pState->CurGroupLongKey())
BlockMapPrepareLongKey(child);
else
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 std::size_t childCount = m_pState->CurGroupChildCount();
@@ -378,7 +388,39 @@ namespace YAML
if(childCount > 0) {
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,12 @@ namespace YAML
}
}
void Emitter::BlockMapPrepareValue(EmitterNodeType::value child)
void Emitter::BlockMapPrepareLongKeyValue(EmitterNodeType::value child)
{
const unsigned curIndent = m_pState->CurIndent();
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
if(!m_pState->HasBegunNode()) {
if(false /* was long key */) {
} else {
m_stream << ":";
}
}
switch(child) {
@@ -425,6 +463,31 @@ namespace YAML
}
}
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) {
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;
}
}
// SpaceOrIndentTo
// . Prepares for some more content by proper spacing
void Emitter::SpaceOrIndentTo(bool requireSpace, unsigned indent)

View File

@@ -58,12 +58,26 @@ namespace YAML
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()
{
if(m_groups.empty())
if(m_groups.empty()) {
m_docCount++;
else
} else {
m_groups.top().childCount++;
if(m_groups.top().childCount % 2 == 0)
m_groups.top().longKey = false;
}
m_hasAnchor = false;
m_hasTag = false;
@@ -166,6 +180,11 @@ namespace YAML
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()
{
m_modifiedSettings.clear();

View File

@@ -37,6 +37,7 @@ namespace YAML
void SetAnchor();
void SetTag();
void SetNonContent();
void SetLongKey();
void StartedScalar();
void StartedGroup(GroupType::value type);
void EndedGroup(GroupType::value type);
@@ -48,6 +49,7 @@ namespace YAML
FlowType::value CurGroupFlowType() const;
int CurGroupIndent() const;
std::size_t CurGroupChildCount() const;
bool CurGroupLongKey() const;
int CurIndent() const { return m_curIndent; }
bool HasAnchor() const { return m_hasAnchor; }
@@ -127,12 +129,13 @@ namespace YAML
SettingChanges m_globalModifiedSettings;
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;
FlowType::value flowType;
int indent;
std::size_t childCount;
bool longKey;
SettingChanges modifiedSettings;

View File

@@ -4,17 +4,12 @@
int main()
{
YAML::Emitter out;
out << YAML::Comment("Hello");
out << YAML::BeginSeq;
out << YAML::Comment("Hello");
out << YAML::Anchor("a") << YAML::Comment("anchor") << "item 1" << YAML::Comment("a");
out << YAML::BeginMap << YAML::Comment("b");
out << "pens" << YAML::Comment("foo") << 2.3 << YAML::Comment("bar");
out << "pencils" << 15;
out << YAML::EndMap << YAML::Comment("monkey");
out << "item 2";
out << YAML::EndSeq;
out << YAML::Comment("end");
out << YAML::BeginMap;
out << YAML::BeginMap;
out << "a" << "b";
out << YAML::EndMap;
out << "c";
out << YAML::EndMap;
std::cout << out.c_str() << "\n";
return 0;