2013-05-04 14:51:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-26 12:30:36 +00:00
|
|
|
#include <Core/Defines.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
2018-12-29 18:39:55 +00:00
|
|
|
#include <common/itoa.h>
|
2013-05-05 16:19:09 +00:00
|
|
|
|
2021-05-04 15:18:51 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-05-04 17:30:32 +00:00
|
|
|
template <typename T> constexpr size_t max_int_width = 20;
|
2021-05-04 17:26:09 +00:00
|
|
|
template <> inline constexpr size_t max_int_width<UInt8> = 3; /// 255
|
|
|
|
template <> inline constexpr size_t max_int_width<Int8> = 4; /// -128
|
|
|
|
template <> inline constexpr size_t max_int_width<UInt16> = 5; /// 65535
|
|
|
|
template <> inline constexpr size_t max_int_width<Int16> = 6; /// -32768
|
|
|
|
template <> inline constexpr size_t max_int_width<UInt32> = 10; /// 4294967295
|
|
|
|
template <> inline constexpr size_t max_int_width<Int32> = 11; /// -2147483648
|
|
|
|
template <> inline constexpr size_t max_int_width<UInt64> = 20; /// 18446744073709551615
|
|
|
|
template <> inline constexpr size_t max_int_width<Int64> = 20; /// -9223372036854775808
|
|
|
|
template <> inline constexpr size_t max_int_width<UInt128> = 39; /// 340282366920938463463374607431768211455
|
|
|
|
template <> inline constexpr size_t max_int_width<Int128> = 40; /// -170141183460469231731687303715884105728
|
|
|
|
template <> inline constexpr size_t max_int_width<UInt256> = 78; /// 115792089237316195423570985008687907853269984665640564039457584007913129639935
|
|
|
|
template <> inline constexpr size_t max_int_width<Int256> = 78; /// -57896044618658097711785492504343953926634992332820282019728792003956564819968
|
2021-05-04 15:18:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-04 14:51:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-02-26 12:31:17 +00:00
|
|
|
|
2013-05-04 14:51:37 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
2018-12-29 18:39:55 +00:00
|
|
|
void NO_INLINE writeUIntTextFallback(T x, WriteBuffer & buf)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-05-04 15:18:51 +00:00
|
|
|
char tmp[max_int_width<T>];
|
2021-05-05 20:04:15 +00:00
|
|
|
char * end = itoa(x, tmp);
|
|
|
|
buf.write(tmp, end - tmp);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-07-20 19:05:07 +00:00
|
|
|
}
|
|
|
|
|
2013-05-04 14:51:37 +00:00
|
|
|
template <typename T>
|
2018-12-29 18:39:55 +00:00
|
|
|
void writeIntText(T x, WriteBuffer & buf)
|
2013-05-04 14:51:37 +00:00
|
|
|
{
|
2021-05-04 15:18:51 +00:00
|
|
|
if (likely(reinterpret_cast<uintptr_t>(buf.position()) + max_int_width<T> < reinterpret_cast<uintptr_t>(buf.buffer().end())))
|
2018-12-29 18:39:55 +00:00
|
|
|
buf.position() = itoa(x, buf.position());
|
|
|
|
else
|
|
|
|
detail::writeUIntTextFallback(x, buf);
|
2013-05-04 14:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|