string-inl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef STRING_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include string.h"
  3. // For the sake of sane code completion.
  4. #include "string.h"
  5. #endif
  6. namespace NYT::NYson {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. namespace NDetail {
  9. template <typename TLeft, typename TRight>
  10. bool Equals(const TLeft& lhs, const TRight& rhs)
  11. {
  12. auto lhsNull = !lhs.operator bool();
  13. auto rhsNull = !rhs.operator bool();
  14. if (lhsNull != rhsNull) {
  15. return false;
  16. }
  17. if (lhsNull && rhsNull) {
  18. return true;
  19. }
  20. return
  21. lhs.AsStringBuf() == rhs.AsStringBuf() &&
  22. lhs.GetType() == rhs.GetType();
  23. }
  24. class TYsonStringHolder
  25. : public TSharedRangeHolder
  26. , public TWithExtraSpace<TYsonStringHolder>
  27. {
  28. public:
  29. explicit TYsonStringHolder(size_t size)
  30. : Size_(size)
  31. { }
  32. char* GetData()
  33. {
  34. return static_cast<char*>(GetExtraSpacePtr());
  35. }
  36. // TSharedRangeHolder overrides.
  37. std::optional<size_t> GetTotalByteSize() const override
  38. {
  39. return Size_ + sizeof(TYsonStringHolder);
  40. }
  41. static TIntrusivePtr<TYsonStringHolder> Allocate(size_t size)
  42. {
  43. return NewWithExtraSpace<TYsonStringHolder>(size, size);
  44. }
  45. private:
  46. const size_t Size_;
  47. };
  48. } // namespace NDetail
  49. inline bool operator == (const TYsonString& lhs, const TYsonString& rhs)
  50. {
  51. return NDetail::Equals(lhs, rhs);
  52. }
  53. inline bool operator == (const TYsonString& lhs, const TYsonStringBuf& rhs)
  54. {
  55. return NDetail::Equals(lhs, rhs);
  56. }
  57. inline bool operator == (const TYsonStringBuf& lhs, const TYsonString& rhs)
  58. {
  59. return NDetail::Equals(lhs, rhs);
  60. }
  61. inline bool operator == (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs)
  62. {
  63. return NDetail::Equals(lhs, rhs);
  64. }
  65. inline bool operator != (const TYsonString& lhs, const TYsonString& rhs)
  66. {
  67. return !(lhs == rhs);
  68. }
  69. inline bool operator != (const TYsonString& lhs, const TYsonStringBuf& rhs)
  70. {
  71. return !(lhs == rhs);
  72. }
  73. inline bool operator != (const TYsonStringBuf& lhs, const TYsonString& rhs)
  74. {
  75. return !(lhs == rhs);
  76. }
  77. inline bool operator != (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs)
  78. {
  79. return !(lhs == rhs);
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////
  82. } // namespace NYT::NYson
  83. //! A hasher for TYsonString
  84. template <>
  85. struct THash<NYT::NYson::TYsonString>
  86. {
  87. size_t operator () (const NYT::NYson::TYsonString& str) const
  88. {
  89. return str.ComputeHash();
  90. }
  91. };
  92. //! A hasher for TYsonStringBuf
  93. template <>
  94. struct THash<NYT::NYson::TYsonStringBuf>
  95. {
  96. size_t operator () (const NYT::NYson::TYsonStringBuf& str) const
  97. {
  98. return THash<TStringBuf>()(str.AsStringBuf());
  99. }
  100. };