提交 3c9c884d 编写于 作者: A Alexey Milovidov

Better #1665

上级 010a5367
......@@ -354,7 +354,7 @@ void tryReadIntTextUnsafe(T & x, ReadBuffer & buf)
template <bool throw_exception, typename ExcepFun, typename NoExcepFun, typename... Args>
bool exceptionPolicySelector(ExcepFun && excep_f, NoExcepFun && no_excep_f, Args &&... args)
{
if (throw_exception)
if constexpr (throw_exception)
{
excep_f(std::forward<Args>(args)...);
return true;
......@@ -374,7 +374,7 @@ void assertNaN(ReadBuffer & buf);
/// Rough: not exactly nearest machine representable number is returned.
/// Some garbage may be successfully parsed, examples: '.' parsed as 0; 123Inf parsed as inf.
template <typename T, typename ReturnType, char point_symbol = '.'>
template <typename T, typename ReturnType>
ReturnType readFloatTextImpl(T & x, ReadBuffer & buf)
{
static constexpr bool throw_exception = std::is_same_v<ReturnType, void>;
......@@ -401,7 +401,7 @@ ReturnType readFloatTextImpl(T & x, ReadBuffer & buf)
case '-':
negative = true;
break;
case point_symbol:
case '.':
after_point = true;
break;
case '0':
......
......@@ -99,29 +99,26 @@ inline void writeBoolText(bool x, WriteBuffer & buf)
}
inline void writeFloatText(double x, WriteBuffer & buf)
template <typename T>
inline void writeFloatText(T x, WriteBuffer & buf)
{
DoubleConverter<false>::BufferType buffer;
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
static_assert(std::is_same_v<T, double> || std::is_same_v<T, float>, "Argument for writeFloatText must be float or double");
const auto result = DoubleConverter<false>::instance().ToShortest(x, &builder);
using Converter = DoubleConverter<false>;
if (!result)
throw Exception("Cannot print double number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
buf.write(buffer, builder.position());
}
inline void writeFloatText(float x, WriteBuffer & buf)
{
DoubleConverter<false>::BufferType buffer;
Converter::BufferType buffer;
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
const auto result = DoubleConverter<false>::instance().ToShortestSingle(x, &builder);
bool result = false;
if constexpr (std::is_same_v<T, double>)
result = Converter::instance().ToShortest(x, &builder);
else
result = Converter::instance().ToShortestSingle(x, &builder);
if (!result)
throw Exception("Cannot print float number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
throw Exception("Cannot print floating point number", ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
/// TODO Excessive copy. Use optimistic path if buffer have enough bytes.
buf.write(buffer, builder.position());
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册