ClickHouse/dbms/include/DB/IO/HexWriteBuffer.h

46 lines
1021 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <DB/IO/BufferWithOwnMemory.h>
#include <DB/IO/WriteBuffer.h>
/// Так как HexWriteBuffer часто создаётся во внутреннем цикле, сделаем у него размер буфера маленьким.
#define DBMS_HEX_WRITE_BUFFER_SIZE 32
namespace DB
{
/** Всё что в него пишут, переводит в HEX (большими буквами) и пишет в другой WriteBuffer.
*/
class HexWriteBuffer : public WriteBuffer
{
protected:
char buf[DBMS_HEX_WRITE_BUFFER_SIZE];
WriteBuffer & out;
void nextImpl()
{
if (!offset())
return;
for (Position p = working_buffer.begin(); p != pos; ++p)
{
out.write("0123456789ABCDEF"[static_cast<unsigned char>(*p) >> 4]);
out.write("0123456789ABCDEF"[static_cast<unsigned char>(*p) & 0xF]);
}
}
public:
HexWriteBuffer(WriteBuffer & out_) : WriteBuffer(buf, sizeof(buf)), out(out_) {}
~HexWriteBuffer()
{
if (!std::uncaught_exception())
nextImpl();
}
};
}