Probably better [#METR-22173].

This commit is contained in:
Alexey Milovidov 2016-08-04 08:19:37 +03:00
parent 4922eac99a
commit f638ebbbf5

View File

@ -105,81 +105,17 @@ inline void readVarInt(Int16 & x, ReadBuffer & istr)
inline void writeVarUInt(UInt64 x, std::ostream & ostr) inline void writeVarUInt(UInt64 x, std::ostream & ostr)
{ {
char buf[9]; for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (byte > 0x7F)
x |= 0x80;
buf[0] = static_cast<UInt8>(x | 0x80); ostr.put(x);
if (x >= (1ULL << 7))
{ x >>= 7;
buf[1] = static_cast<UInt8>((x >> 7) | 0x80); if (!x)
if (x >= (1ULL << 14)) return;
{
buf[2] = static_cast<UInt8>((x >> 14) | 0x80);
if (x >= (1ULL << 21))
{
buf[3] = static_cast<UInt8>((x >> 21) | 0x80);
if (x >= (1ULL << 28))
{
buf[4] = static_cast<UInt8>((x >> 28) | 0x80);
if (x >= (1ULL << 35))
{
buf[5] = static_cast<UInt8>((x >> 35) | 0x80);
if (x >= (1ULL << 42))
{
buf[6] = static_cast<UInt8>((x >> 42) | 0x80);
if (x >= (1ULL << 49))
{
buf[7] = static_cast<UInt8>((x >> 49) | 0x80);
if (x >= (1ULL << 56))
{
buf[8] = static_cast<UInt8>(x >> 56);
ostr.write(buf, 9);
}
else
{
buf[7] &= 0x7F;
ostr.write(buf, 8);
}
}
else
{
buf[6] &= 0x7F;
ostr.write(buf, 7);
}
}
else
{
buf[5] &= 0x7F;
ostr.write(buf, 6);
}
}
else
{
buf[4] &= 0x7F;
ostr.write(buf, 5);
}
}
else
{
buf[3] &= 0x7F;
ostr.write(buf, 4);
}
}
else
{
buf[2] &= 0x7F;
ostr.write(buf, 3);
}
}
else
{
buf[1] &= 0x7F;
ostr.write(buf, 2);
}
}
else
{
buf[0] &= 0x7F;
ostr.write(buf, 1);
} }
} }
@ -197,9 +133,9 @@ inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
if (istr.eof()) if (istr.eof())
throwReadAfterEOF(); throwReadAfterEOF();
int byte = *istr.position(); uint64_t byte = *istr.position();
++istr.position(); ++istr.position();
x |= (static_cast<uint64_t>(byte) & 0x7F) << (7 * i); x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80)) if (!(byte & 0x80))
return; return;
@ -209,187 +145,54 @@ inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
inline void readVarUInt(UInt64 & x, std::istream & istr) inline void readVarUInt(UInt64 & x, std::istream & istr)
{ {
int byte; x = 0;
for (size_t i = 0; i < 9; ++i)
byte = istr.get();
x = static_cast<UInt64>(byte) & 0x7F;
if (byte & 0x80)
{ {
byte = istr.get(); uint64_t byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 7; x |= (byte & 0x7F) << (7 * i);
if (byte & 0x80)
{ if (!(byte & 0x80))
byte = istr.get(); return;
x |= (static_cast<UInt64>(byte) & 0x7F) << 14;
if (byte & 0x80)
{
byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 21;
if (byte & 0x80)
{
byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 28;
if (byte & 0x80)
{
byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 35;
if (byte & 0x80)
{
byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 42;
if (byte & 0x80)
{
byte = istr.get();
x |= (static_cast<UInt64>(byte) & 0x7F) << 49;
if (byte & 0x80)
{
byte = istr.get();
x |= static_cast<UInt64>(byte) << 56;
}
}
}
}
}
}
}
} }
} }
inline void writeVarUInt(UInt64 x, WriteBuffer & ostr) inline void writeVarUInt(UInt64 x, WriteBuffer & ostr)
{ {
char buf[9]; for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (byte > 0x7F)
x |= 0x80;
buf[0] = static_cast<UInt8>(x | 0x80); ostr.nextIfAtEnd();
if (x >= (1ULL << 7)) *ostr.position() = x;
{ ++ostr.position();
buf[1] = static_cast<UInt8>((x >> 7) | 0x80);
if (x >= (1ULL << 14)) x >>= 7;
{ if (!x)
buf[2] = static_cast<UInt8>((x >> 14) | 0x80); return;
if (x >= (1ULL << 21))
{
buf[3] = static_cast<UInt8>((x >> 21) | 0x80);
if (x >= (1ULL << 28))
{
buf[4] = static_cast<UInt8>((x >> 28) | 0x80);
if (x >= (1ULL << 35))
{
buf[5] = static_cast<UInt8>((x >> 35) | 0x80);
if (x >= (1ULL << 42))
{
buf[6] = static_cast<UInt8>((x >> 42) | 0x80);
if (x >= (1ULL << 49))
{
buf[7] = static_cast<UInt8>((x >> 49) | 0x80);
if (x >= (1ULL << 56))
{
buf[8] = static_cast<UInt8>(x >> 56);
ostr.write(buf, 9);
}
else
{
buf[7] &= 0x7F;
ostr.write(buf, 8);
}
}
else
{
buf[6] &= 0x7F;
ostr.write(buf, 7);
}
}
else
{
buf[5] &= 0x7F;
ostr.write(buf, 6);
}
}
else
{
buf[4] &= 0x7F;
ostr.write(buf, 5);
}
}
else
{
buf[3] &= 0x7F;
ostr.write(buf, 4);
}
}
else
{
buf[2] &= 0x7F;
ostr.write(buf, 3);
}
}
else
{
buf[1] &= 0x7F;
ostr.write(buf, 2);
}
}
else
{
buf[0] &= 0x7F;
ostr.write(buf, 1);
} }
} }
inline char * writeVarUInt(UInt64 x, char * ostr) inline char * writeVarUInt(UInt64 x, char * ostr)
{ {
*ostr = static_cast<UInt8>(x | 0x80); for (size_t i = 0; i < 9; ++i)
if (x >= (1ULL << 7))
{ {
*++ostr = static_cast<UInt8>((x >> 7) | 0x80); uint8_t byte = x & 0x7F;
if (x >= (1ULL << 14)) if (byte > 0x7F)
{ x |= 0x80;
*++ostr = static_cast<UInt8>((x >> 14) | 0x80);
if (x >= (1ULL << 21))
{
*++ostr = static_cast<UInt8>((x >> 21) | 0x80);
if (x >= (1ULL << 28))
{
*++ostr = static_cast<UInt8>((x >> 28) | 0x80);
if (x >= (1ULL << 35))
{
*++ostr = static_cast<UInt8>((x >> 35) | 0x80);
if (x >= (1ULL << 42))
{
*++ostr = static_cast<UInt8>((x >> 42) | 0x80);
if (x >= (1ULL << 49))
{
*++ostr = static_cast<UInt8>((x >> 49) | 0x80);
if (x >= (1ULL << 56))
{
*++ostr = static_cast<UInt8>(x >> 56);
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
}
else
*ostr &= 0x7F;
return ++ostr; *ostr = x;
++ostr;
x >>= 7;
if (!x)
return ostr;
}
return ostr;
} }
@ -397,41 +200,21 @@ inline const char * readVarUInt(UInt64 & x, const char * istr, size_t size)
{ {
const char * end = istr + size; const char * end = istr + size;
x = static_cast<UInt64>(*istr) & 0x7F; x = 0;
if (*istr & 0x80 && ++istr < end) for (size_t i = 0; i < 9; ++i)
{ {
x |= (static_cast<UInt64>(*istr) & 0x7F) << 7; if (istr == end)
if (*istr & 0x80 && ++istr < end) throwReadAfterEOF();
{
x |= (static_cast<UInt64>(*istr) & 0x7F) << 14; uint64_t byte = *istr;
if (*istr & 0x80 && ++istr < end) ++istr;
{ x |= (byte & 0x7F) << (7 * i);
x |= (static_cast<UInt64>(*istr) & 0x7F) << 21;
if (*istr & 0x80 && ++istr < end) if (!(byte & 0x80))
{ return istr;
x |= (static_cast<UInt64>(*istr) & 0x7F) << 28;
if (*istr & 0x80 && ++istr < end)
{
x |= (static_cast<UInt64>(*istr) & 0x7F) << 35;
if (*istr & 0x80 && ++istr < end)
{
x |= (static_cast<UInt64>(*istr) & 0x7F) << 42;
if (*istr & 0x80 && ++istr < end)
{
x |= (static_cast<UInt64>(*istr) & 0x7F) << 49;
if (*istr & 0x80 && ++istr < end)
{
x |= static_cast<UInt64>(*istr) << 56;
}
}
}
}
}
}
}
} }
return ++istr; return istr;
} }