ClickHouse/src/IO/WriteIntText.h

50 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
2019-02-26 12:30:36 +00:00
#include <Core/Defines.h>
#include <IO/WriteBuffer.h>
2021-10-02 07:13:14 +00:00
#include <base/itoa.h>
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;
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
}
namespace DB
{
2019-02-26 12:31:17 +00:00
namespace detail
{
template <typename T>
2018-12-29 18:39:55 +00:00
void NO_INLINE writeUIntTextFallback(T x, WriteBuffer & buf)
{
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);
}
2018-07-20 19:05:07 +00:00
}
template <typename T>
2018-12-29 18:39:55 +00:00
void writeIntText(T x, WriteBuffer & buf)
{
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);
}
}