ClickHouse/src/IO/WriteHelpers.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
5.2 KiB
C++
Raw Normal View History

#include <IO/WriteHelpers.h>
#include <cinttypes>
#include <utility>
2022-11-15 00:46:15 +00:00
#include <Common/formatIPv6.h>
#include <Common/hex.h>
2010-06-04 18:25:25 +00:00
namespace DB
{
template <typename IteratorSrc, typename IteratorDst>
void formatHex(IteratorSrc src, IteratorDst dst, size_t num_bytes)
{
size_t src_pos = 0;
size_t dst_pos = 0;
for (; src_pos < num_bytes; ++src_pos)
{
writeHexByteLowercase(src[src_pos], &dst[dst_pos]);
dst_pos += 2;
}
}
/** Function used when byte ordering is important when parsing uuid
* ex: When we create an UUID type
*/
void formatUUID(std::reverse_iterator<const UInt8 *> src16, UInt8 * dst36)
{
formatHex(src16 + 8, &dst36[0], 4);
dst36[8] = '-';
formatHex(src16 + 12, &dst36[9], 2);
dst36[13] = '-';
formatHex(src16 + 14, &dst36[14], 2);
dst36[18] = '-';
formatHex(src16, &dst36[19], 2);
dst36[23] = '-';
formatHex(src16 + 2, &dst36[24], 6);
}
/** Further we want to generate constexpr array of strings with sizes from sequence of unsigned ints [0..N)
* in order to use this arrey for fast conversion of unsigned integers to strings
*/
namespace detail
{
template <unsigned... digits>
2022-11-14 14:46:04 +00:00
struct ToChars
{
static const char value[];
static const size_t size;
};
template <unsigned... digits>
constexpr char ToChars<digits...>::value[] = {('0' + digits)..., 0};
template <unsigned... digits>
constexpr size_t ToChars<digits...>::size = sizeof...(digits);
template <unsigned rem, unsigned... digits>
struct Decompose : Decompose<rem / 10, rem % 10, digits...> {};
template <unsigned... digits>
struct Decompose<0, digits...> : ToChars<digits...> {};
template <>
struct Decompose<0> : ToChars<0> {};
template <unsigned num>
struct NumToString : Decompose<num> {};
template <class T, T... ints>
constexpr std::array<std::pair<const char *, size_t>, sizeof...(ints)> str_make_array_impl(std::integer_sequence<T, ints...>)
{
return std::array<std::pair<const char *, size_t>, sizeof...(ints)> { std::pair<const char *, size_t> {NumToString<ints>::value, NumToString<ints>::size}... };
}
}
/** str_make_array<N>() - generates static array of std::pair<const char *, size_t> for numbers [0..N), where:
* first - null-terminated string representing number
* second - size of the string as would returned by strlen()
*/
template <size_t N>
constexpr std::array<std::pair<const char *, size_t>, N> str_make_array()
{
return detail::str_make_array_impl(std::make_integer_sequence<int, N>{});
}
/// This will generate static array of pair<const char *, size_t> for [0..255] at compile time
static constexpr auto byte_str = str_make_array<256>();
void writeIPv4Text(const IPv4 & ip, WriteBuffer & buf)
{
size_t idx = (ip >> 24);
buf.write(byte_str[idx].first, byte_str[idx].second);
buf.write('.');
idx = (ip >> 16) & 0xFF;
buf.write(byte_str[idx].first, byte_str[idx].second);
buf.write('.');
idx = (ip >> 8) & 0xFF;
buf.write(byte_str[idx].first, byte_str[idx].second);
buf.write('.');
idx = ip & 0xFF;
buf.write(byte_str[idx].first, byte_str[idx].second);
}
void writeIPv6Text(const IPv6 & ip, WriteBuffer & buf)
{
2022-11-15 00:46:15 +00:00
char addr[IPV6_MAX_TEXT_LENGTH + 1] {};
char * paddr = addr;
formatIPv6(reinterpret_cast<const unsigned char *>(&ip), paddr);
buf.write(addr, paddr - addr);
}
2018-08-24 07:30:53 +00:00
void writeException(const Exception & e, WriteBuffer & buf, bool with_stack_trace)
2012-05-08 05:42:05 +00:00
{
writeBinary(e.code(), buf);
writeBinary(String(e.name()), buf);
2022-05-03 13:13:47 +00:00
writeBinary(e.displayText() + getExtraExceptionInfo(e), buf);
2018-08-24 07:30:53 +00:00
if (with_stack_trace)
writeBinary(e.getStackTraceString(), buf);
2018-08-24 07:30:53 +00:00
else
writeBinary(String(), buf);
2012-05-08 05:42:05 +00:00
bool has_nested = false;
2012-05-08 05:42:05 +00:00
writeBinary(has_nested, buf);
}
/// The same, but quotes apply only if there are characters that do not match the identifier without quotes
template <typename F>
static inline void writeProbablyQuotedStringImpl(StringRef s, WriteBuffer & buf, F && write_quoted_string)
{
if (isValidIdentifier(s.toView())
2021-12-09 20:51:52 +00:00
/// This are valid identifiers but are problematic if present unquoted in SQL query.
&& !(s.size == strlen("distinct") && 0 == strncasecmp(s.data, "distinct", strlen("distinct")))
&& !(s.size == strlen("all") && 0 == strncasecmp(s.data, "all", strlen("all"))))
{
2020-08-08 16:34:35 +00:00
writeString(s, buf);
2021-12-09 20:51:52 +00:00
}
else
2020-08-08 16:34:35 +00:00
write_quoted_string(s, buf);
}
void writeProbablyBackQuotedString(StringRef s, WriteBuffer & buf)
{
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { return writeBackQuotedString(s_, buf_); });
}
void writeProbablyDoubleQuotedString(StringRef s, WriteBuffer & buf)
{
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { return writeDoubleQuotedString(s_, buf_); });
}
void writeProbablyBackQuotedStringMySQL(StringRef s, WriteBuffer & buf)
{
writeProbablyQuotedStringImpl(s, buf, [](StringRef s_, WriteBuffer & buf_) { return writeBackQuotedStringMySQL(s_, buf_); });
}
2020-11-09 19:07:38 +00:00
void writePointerHex(const void * ptr, WriteBuffer & buf)
{
writeString("0x", buf);
char hex_str[2 * sizeof(ptr)];
writeHexUIntLowercase(reinterpret_cast<uintptr_t>(ptr), hex_str);
buf.write(hex_str, 2 * sizeof(ptr));
}
2010-06-04 18:25:25 +00:00
}