Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/yaml-cpp/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class YAML_CPP_API Emitter {
bool SetPostCommentIndent(std::size_t n);
bool SetFloatPrecision(std::size_t n);
bool SetDoublePrecision(std::size_t n);
bool SetShowTrailingZero(bool value);
void RestoreGlobalModifiedSettings();

// local setters
Expand Down Expand Up @@ -95,6 +96,7 @@ class YAML_CPP_API Emitter {
void SetStreamablePrecision(std::stringstream&) {}
std::size_t GetFloatPrecision() const;
std::size_t GetDoublePrecision() const;
bool GetShowTrailingZero() const;

void PrepareIntegralStream(std::stringstream& stream) const;
void StartedScalar();
Expand Down Expand Up @@ -187,8 +189,17 @@ inline Emitter& Emitter::WriteStreamable(T value) {
}

if (!special) {
stream << FpToString(value, stream.precision());
auto value_as_str = FpToString(value, stream.precision());
if (GetShowTrailingZero()) {
bool isInScientificNotation = (value_as_str.find('e') != std::string::npos);
bool hasDot = (value_as_str.find('.') != std::string::npos);
if (!isInScientificNotation && !hasDot) {
value_as_str += ".0";
}
}
stream << value_as_str;
}

m_stream << stream.str();

StartedScalar();
Expand Down
8 changes: 8 additions & 0 deletions src/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ bool Emitter::SetDoublePrecision(std::size_t n) {
return m_pState->SetDoublePrecision(n, FmtScope::Global);
}

bool Emitter::SetShowTrailingZero(bool value) {
return m_pState->SetShowTrailingZero(value, FmtScope::Global);
}

void Emitter::RestoreGlobalModifiedSettings() {
m_pState->RestoreGlobalModifiedSettings();
}
Expand Down Expand Up @@ -764,6 +768,10 @@ std::size_t Emitter::GetDoublePrecision() const {
return m_pState->GetDoublePrecision();
}

bool Emitter::GetShowTrailingZero() const {
return m_pState->GetShowTrailingZero();
}

const char* Emitter::ComputeFullBoolName(bool b) const {
const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool
? YesNoBool
Expand Down
6 changes: 6 additions & 0 deletions src/emitterstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ EmitterState::EmitterState()
m_mapKeyFmt(Auto),
m_floatPrecision(std::numeric_limits<float>::max_digits10),
m_doublePrecision(std::numeric_limits<double>::max_digits10),
m_showTrailingZero(false),
//
m_modifiedSettings{},
m_globalModifiedSettings{},
Expand Down Expand Up @@ -397,4 +398,9 @@ bool EmitterState::SetDoublePrecision(std::size_t value,
_Set(m_doublePrecision, value, scope);
return true;
}
bool EmitterState::SetShowTrailingZero(bool value, FmtScope::value scope) {
_Set(m_showTrailingZero, value, scope);
return true;
}

} // namespace YAML
3 changes: 3 additions & 0 deletions src/emitterstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class EmitterState {
std::size_t GetFloatPrecision() const { return m_floatPrecision.get(); }
bool SetDoublePrecision(std::size_t value, FmtScope::value scope);
std::size_t GetDoublePrecision() const { return m_doublePrecision.get(); }
bool SetShowTrailingZero(bool value, FmtScope::value scope);
bool GetShowTrailingZero() const { return m_showTrailingZero.get(); }

private:
template <typename T>
Expand Down Expand Up @@ -146,6 +148,7 @@ class EmitterState {
Setting<EMITTER_MANIP> m_mapKeyFmt;
Setting<std::size_t> m_floatPrecision;
Setting<std::size_t> m_doublePrecision;
Setting<bool> m_showTrailingZero;

SettingChanges m_modifiedSettings;
SettingChanges m_globalModifiedSettings;
Expand Down
42 changes: 42 additions & 0 deletions test/integration/emitter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1780,5 +1780,47 @@ TEST_F(EmitterErrorTest, InvalidAlias) {
ExpectEmitError(ErrorMsg::INVALID_ALIAS);
}

TEST_F(EmitterTest, ShowTrailingZero) {
out << BeginSeq;
out.SetShowTrailingZero(false);
out << 0.;
out << -0.;
out << 3.;
out << 42.;
out.SetShowTrailingZero(true);
out << 0.;
out << -0.;
out << 4.;
out << 51.;
out.SetShowTrailingZero(false);
out << 0.2;
out << 5.12;
out.SetShowTrailingZero(true);
out << 0.2;
out << 6.34;
out << std::numeric_limits<double>::infinity();
out << -std::numeric_limits<double>::infinity();
out << std::numeric_limits<double>::quiet_NaN();
out << std::numeric_limits<double>::signaling_NaN();
out << EndSeq;

ExpectEmit(R"(- 0
- -0
- 3
- 42
- 0.0
- -0.0
- 4.0
- 51.0
- 0.2
- 5.12
- 0.2
- 6.34
- .inf
- -.inf
- .nan
- .nan)");
}

} // namespace
} // namespace YAML