varint.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/stream/input.h>
  4. #include <util/stream/output.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. constexpr size_t MaxVarInt64Size = (8 * sizeof(ui64) - 1) / 7 + 1;
  8. constexpr size_t MaxVarUint64Size = (8 * sizeof(ui64) - 1) / 7 + 1;
  9. constexpr size_t MaxVarInt32Size = (8 * sizeof(ui32) - 1) / 7 + 1;
  10. constexpr size_t MaxVarUint32Size = (8 * sizeof(ui32) - 1) / 7 + 1;
  11. ////////////////////////////////////////////////////////////////////////////////
  12. // Various functions to read/write varints.
  13. // Return the number of bytes written.
  14. int WriteVarUint64(IOutputStream* output, ui64 value);
  15. int WriteVarUint32(IOutputStream* output, ui32 value);
  16. int WriteVarInt32(IOutputStream* output, i32 value);
  17. int WriteVarInt64(IOutputStream* output, i64 value);
  18. // Assume that #output has enough capacity.
  19. int WriteVarUint64(char* output, ui64 value);
  20. int WriteVarUint32(char* output, ui32 value);
  21. int WriteVarInt32(char* output, i32 value);
  22. int WriteVarInt64(char* output, i64 value);
  23. // Return the number of bytes read.
  24. int ReadVarUint64(IInputStream* input, ui64* value);
  25. int ReadVarUint32(IInputStream* input, ui32* value);
  26. int ReadVarInt32(IInputStream* input, i32* value);
  27. int ReadVarInt64(IInputStream* input, i64* value);
  28. // Assume that #input contains a valid varint.
  29. int ReadVarUint64(const char* input, ui64* value);
  30. int ReadVarUint32(const char* input, ui32* value);
  31. int ReadVarInt32(const char* input, i32* value);
  32. int ReadVarInt64(const char* input, i64* value);
  33. int ReadVarUint64(const char* input, const char* end, ui64* value);
  34. int ReadVarUint32(const char* input, const char* end, ui32* value);
  35. int ReadVarInt32(const char* input, const char* end, i32* value);
  36. int ReadVarInt64(const char* input, const char* end, i64* value);
  37. ////////////////////////////////////////////////////////////////////////////////
  38. } // namespace NYT
  39. #define VARINT_INL_H_
  40. #include "varint-inl.h"
  41. #undef VARINT_INL_H_