Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Support calling a const NumberToString, add unit tests + style improvements #2256

Merged
Changes from 1 commit
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
63 changes: 42 additions & 21 deletions Modules/Core/Common/src/itkNumberToString.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,66 @@
*
*=========================================================================*/
#include "itkNumberToString.h"
#include "itkNumericTraits.h"
#include "double-conversion/double-conversion.h"
#include "double-conversion/double-to-string.h"

#include <sstream>

namespace itk
namespace
{

template <>
bool
ConvertToShortest(const double_conversion::DoubleToStringConverter & converter,
const double val,
double_conversion::StringBuilder & builder)
{
return converter.ToShortest(val, &builder);
}

bool
ConvertToShortest(const double_conversion::DoubleToStringConverter & converter,
const float val,
double_conversion::StringBuilder & builder)
{
// Call the converter member function that is specific for single-precision `float`.
return converter.ToShortestSingle(val, &builder);
}

template <typename TValue>
std::string
NumberToString<double>::operator()(double val) const
FloatingPointNumberToString(const TValue val)
{
char buf[256];
const double_conversion::DoubleToStringConverter & converter =
double_conversion::DoubleToStringConverter::EcmaScriptConverter();
// Declare a buffer, large enough for strings like:
// "-100000000000000000000" (-1e20, either float or double, 23 chars)
// "-1.7976931348623157e+308" (-DBL_MAX, 25 chars)
// "-0.0000033333333333333333" (-3e-005/9.0, 26 chars)
char buf[32];
dzenanz marked this conversation as resolved.
Show resolved Hide resolved

double_conversion::StringBuilder builder(buf, sizeof(buf));
builder.Reset();
if (!converter.ToShortest(val, &builder))

if (!ConvertToShortest(double_conversion::DoubleToStringConverter::EcmaScriptConverter(), val, builder))
{
itkGenericExceptionMacro(<< "Conversion failed for " << val);
}
return std::string(builder.Finalize());
}

} // namespace

namespace itk
{

template <>
std::string
NumberToString<float>::operator()(float val) const
NumberToString<double>::operator()(double val) const
{
char buf[256];
const double_conversion::DoubleToStringConverter & converter =
double_conversion::DoubleToStringConverter::EcmaScriptConverter();
return FloatingPointNumberToString(val);
}

double_conversion::StringBuilder builder(buf, sizeof(buf));
builder.Reset();
if (!converter.ToShortestSingle(val, &builder))
{
itkGenericExceptionMacro(<< "Conversion failed for " << val);
}
return std::string(builder.Finalize());
template <>
std::string
NumberToString<float>::operator()(float val) const
{
return FloatingPointNumberToString(val);
}

} // namespace itk