Commit 39c396ab authored by Jesse Beder's avatar Jesse Beder
Browse files

Refactored bool emitting to make it 1) correct for the short bool form and 2)...

Refactored bool emitting to make it 1) correct for the short bool form and 2) not barf on early versions of VS
parent 1e0c36c5
...@@ -85,6 +85,7 @@ namespace YAML ...@@ -85,6 +85,7 @@ namespace YAML
void EmitKindTag(); void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag); void EmitTag(bool verbatim, const _Tag& tag);
const char *ComputeFullBoolName(bool b) const;
bool CanEmitNewline() const; bool CanEmitNewline() const;
private: private:
......
...@@ -606,12 +606,45 @@ namespace YAML ...@@ -606,12 +606,45 @@ namespace YAML
PostAtomicWrite(); PostAtomicWrite();
} }
namespace { const char *Emitter::ComputeFullBoolName(bool b) const
struct BoolName { std::string trueName, falseName; }; {
struct BoolFormatNames { BoolName upper, lower, camel; }; const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool ? YesNoBool : m_pState->GetBoolFormat());
struct BoolTypes { BoolFormatNames yesNo, trueFalse, onOff; }; const EMITTER_MANIP caseFmt = m_pState->GetBoolCaseFormat();
switch(mainFmt) {
case YesNoBool:
switch(caseFmt) {
case UpperCase:
return b ? "YES" : "NO";
case CamelCase:
return b ? "Yes" : "No";
case LowerCase: // fall through to default
default:
return b ? "yes" : "no";
}
case OnOffBool:
switch(caseFmt) {
case UpperCase:
return b ? "ON" : "OFF";
case CamelCase:
return b ? "On" : "Off";
case LowerCase: // fall through to default
default:
return b ? "on" : "off";
}
case TrueFalseBool: // fall through to default
default:
switch(caseFmt) {
case UpperCase:
return b ? "TRUE" : "FALSE";
case CamelCase:
return b ? "True" : "False";
case LowerCase: // fall through to default
default:
return b ? "true" : "false";
}
}
} }
Emitter& Emitter::Write(bool b) Emitter& Emitter::Write(bool b)
{ {
if(!good()) if(!good())
...@@ -619,30 +652,13 @@ namespace YAML ...@@ -619,30 +652,13 @@ namespace YAML
PreAtomicWrite(); PreAtomicWrite();
EmitSeparationIfNecessary(); EmitSeparationIfNecessary();
// set up all possible bools to write const char *name = ComputeFullBoolName(b);
static const BoolTypes boolTypes = { if(m_pState->GetBoolLengthFormat() == ShortBool)
{ { "YES", "NO" }, { "yes", "no" }, { "Yes", "No" } },
{ { "TRUE", "FALSE" }, { "true", "false" }, { "True", "False" } },
{ { "ON", "OFF" }, { "on", "off" }, { "On", "Off" } }
};
// select the right one
EMITTER_MANIP boolFmt = m_pState->GetBoolFormat();
EMITTER_MANIP boolLengthFmt = m_pState->GetBoolLengthFormat();
EMITTER_MANIP boolCaseFmt = m_pState->GetBoolCaseFormat();
const BoolFormatNames& fmtNames = (boolFmt == YesNoBool ? boolTypes.yesNo : boolFmt == TrueFalseBool ? boolTypes.trueFalse : boolTypes.onOff);
const BoolName& boolName = (boolCaseFmt == UpperCase ? fmtNames.upper : boolCaseFmt == LowerCase ? fmtNames.lower : fmtNames.camel);
const std::string& name = (b ? boolName.trueName : boolName.falseName);
// and say it!
// TODO: should we disallow writing OnOffBool with ShortBool? (it'll just print "o" for both, which is silly)
if(boolLengthFmt == ShortBool)
m_stream << name[0]; m_stream << name[0];
else else
m_stream << name; m_stream << name;
PostAtomicWrite(); PostAtomicWrite();
return *this; return *this;
} }
......
...@@ -741,6 +741,41 @@ namespace Test ...@@ -741,6 +741,41 @@ namespace Test
out << YAML::EndMap; out << YAML::EndMap;
desiredOutput = "---\napple: \":\"\nbanana: \":\""; desiredOutput = "---\napple: \":\"\nbanana: \":\"";
} }
void BoolFormatting(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::BeginSeq;
out << YAML::TrueFalseBool << YAML::UpperCase << true;
out << YAML::TrueFalseBool << YAML::CamelCase << true;
out << YAML::TrueFalseBool << YAML::LowerCase << true;
out << YAML::TrueFalseBool << YAML::UpperCase << false;
out << YAML::TrueFalseBool << YAML::CamelCase << false;
out << YAML::TrueFalseBool << YAML::LowerCase << false;
out << YAML::YesNoBool << YAML::UpperCase << true;
out << YAML::YesNoBool << YAML::CamelCase << true;
out << YAML::YesNoBool << YAML::LowerCase << true;
out << YAML::YesNoBool << YAML::UpperCase << false;
out << YAML::YesNoBool << YAML::CamelCase << false;
out << YAML::YesNoBool << YAML::LowerCase << false;
out << YAML::OnOffBool << YAML::UpperCase << true;
out << YAML::OnOffBool << YAML::CamelCase << true;
out << YAML::OnOffBool << YAML::LowerCase << true;
out << YAML::OnOffBool << YAML::UpperCase << false;
out << YAML::OnOffBool << YAML::CamelCase << false;
out << YAML::OnOffBool << YAML::LowerCase << false;
out << YAML::ShortBool << YAML::UpperCase << true;
out << YAML::ShortBool << YAML::CamelCase << true;
out << YAML::ShortBool << YAML::LowerCase << true;
out << YAML::ShortBool << YAML::UpperCase << false;
out << YAML::ShortBool << YAML::CamelCase << false;
out << YAML::ShortBool << YAML::LowerCase << false;
out << YAML::EndSeq;
desiredOutput =
"---\n- TRUE\n- True\n- true\n- FALSE\n- False\n- false\n"
"- YES\n- Yes\n- yes\n- NO\n- No\n- no\n"
"- ON\n- On\n- on\n- OFF\n- Off\n- off\n"
"- Y\n- Y\n- y\n- N\n- N\n- n";
}
//////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting // incorrect emitting
...@@ -933,6 +968,7 @@ namespace Test ...@@ -933,6 +968,7 @@ namespace Test
RunEmitterTest(&Emitter::EmptyBinary, "empty binary", passed, total); RunEmitterTest(&Emitter::EmptyBinary, "empty binary", passed, total);
RunEmitterTest(&Emitter::ColonAtEndOfScalar, "colon at end of scalar", passed, total); RunEmitterTest(&Emitter::ColonAtEndOfScalar, "colon at end of scalar", passed, total);
RunEmitterTest(&Emitter::ColonAsScalar, "colon as scalar", passed, total); RunEmitterTest(&Emitter::ColonAsScalar, "colon as scalar", passed, total);
RunEmitterTest(&Emitter::BoolFormatting, "bool formatting", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total); RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total); RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment