common.h 811 B

1234567891011121314151617181920212223242526272829303132
  1. #pragma once
  2. #include <util/string/hex.h>
  3. #include <util/digest/city.h>
  4. #include <util/system/byteorder.h>
  5. namespace NCodecs {
  6. template <class T>
  7. ui64 DataSignature(const T& t) {
  8. static_assert(!std::is_scalar<T>::value, "no scalars");
  9. return CityHash64(t.data(), t.size());
  10. }
  11. template <class T>
  12. TString HexWriteScalar(T t) {
  13. static_assert(std::is_scalar<T>::value, "scalars only");
  14. t = LittleToBig(t);
  15. TString res = HexEncode(&t, sizeof(t));
  16. res.to_lower();
  17. return res;
  18. }
  19. template <class T>
  20. T HexReadScalar(TStringBuf s) {
  21. static_assert(std::is_scalar<T>::value, "scalars only");
  22. T t = 0;
  23. HexDecode(s.data(), Min(s.size(), sizeof(T)), &t);
  24. t = BigToLittle(t);
  25. return t;
  26. }
  27. }