ClickHouse/src/IO/WriteIntText.h

34 lines
744 B
C++
Raw Normal View History

#pragma once
2019-02-26 12:30:36 +00:00
#include <Core/Defines.h>
#include <IO/WriteBuffer.h>
2018-12-29 18:39:55 +00:00
#include <common/itoa.h>
2018-12-29 18:39:55 +00:00
/// 40 digits or 39 digits and a sign
#define WRITE_HELPERS_MAX_INT_WIDTH 40U
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)
{
char tmp[WRITE_HELPERS_MAX_INT_WIDTH];
2018-12-29 18:39:55 +00:00
int len = itoa(x, tmp) - tmp;
buf.write(tmp, len);
}
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-01-04 18:07:52 +00:00
if (likely(reinterpret_cast<intptr_t>(buf.position()) + WRITE_HELPERS_MAX_INT_WIDTH < reinterpret_cast<intptr_t>(buf.buffer().end())))
2018-12-29 18:39:55 +00:00
buf.position() = itoa(x, buf.position());
else
detail::writeUIntTextFallback(x, buf);
}
}