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
void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag);
const char *ComputeFullBoolName(bool b) const;
bool CanEmitNewline() const;
private:
......
......@@ -606,10 +606,43 @@ namespace YAML
PostAtomicWrite();
}
namespace {
struct BoolName { std::string trueName, falseName; };
struct BoolFormatNames { BoolName upper, lower, camel; };
struct BoolTypes { BoolFormatNames yesNo, trueFalse, onOff; };
const char *Emitter::ComputeFullBoolName(bool b) const
{
const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool ? YesNoBool : m_pState->GetBoolFormat());
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)
......@@ -620,25 +653,8 @@ namespace YAML
PreAtomicWrite();
EmitSeparationIfNecessary();
// set up all possible bools to write
static const BoolTypes boolTypes = {
{ { "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)
const char *name = ComputeFullBoolName(b);
if(m_pState->GetBoolLengthFormat() == ShortBool)
m_stream << name[0];
else
m_stream << name;
......
......@@ -742,6 +742,41 @@ namespace Test
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
......@@ -933,6 +968,7 @@ namespace Test
RunEmitterTest(&Emitter::EmptyBinary, "empty binary", passed, total);
RunEmitterTest(&Emitter::ColonAtEndOfScalar, "colon at end of 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::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