string.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include "public.h"
  3. #include <library/cpp/yt/memory/ref.h>
  4. #include <library/cpp/yt/string/format.h>
  5. #include <variant>
  6. namespace NYT::NYson {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //! Contains a sequence of bytes in YSON encoding annotated with EYsonType describing
  9. //! the content. Could be null. Non-owning.
  10. class TYsonStringBuf
  11. {
  12. public:
  13. //! Constructs a null instance.
  14. TYsonStringBuf();
  15. //! Constructs an instance from TYsonString.
  16. TYsonStringBuf(const TYsonString& ysonString);
  17. //! Constructs a non-null instance with given type and content.
  18. explicit TYsonStringBuf(
  19. const TString& data,
  20. EYsonType type = EYsonType::Node);
  21. //! Constructs a non-null instance with given type and content.
  22. explicit TYsonStringBuf(
  23. TStringBuf data,
  24. EYsonType type = EYsonType::Node);
  25. //! Constructs a non-null instance with given type and content
  26. //! (without this overload there is no way to construct TYsonStringBuf from
  27. //! string literal).
  28. explicit TYsonStringBuf(
  29. const char* data,
  30. EYsonType type = EYsonType::Node);
  31. //! Returns |true| if the instance is not null.
  32. explicit operator bool() const;
  33. //! Returns the underlying YSON bytes. The instance must be non-null.
  34. TStringBuf AsStringBuf() const;
  35. //! Returns type of YSON contained here. The instance must be non-null.
  36. EYsonType GetType() const;
  37. protected:
  38. TStringBuf Data_;
  39. EYsonType Type_;
  40. bool Null_;
  41. };
  42. ////////////////////////////////////////////////////////////////////////////////
  43. //! An owning version of TYsonStringBuf.
  44. /*!
  45. * Internally captures the data either via TString or a polymorphic ref-counted holder.
  46. */
  47. class TYsonString
  48. {
  49. public:
  50. //! Constructs a null instance.
  51. TYsonString();
  52. //! Constructs an instance from TYsonStringBuf.
  53. //! Copies the data into a ref-counted payload.
  54. explicit TYsonString(const TYsonStringBuf& ysonStringBuf);
  55. //! Constructs an instance from TStringBuf.
  56. //! Copies the data into a ref-counted payload.
  57. explicit TYsonString(
  58. TStringBuf data,
  59. EYsonType type = EYsonType::Node);
  60. //! Constructs an instance from TString.
  61. //! Zero-copy for CoW TString: retains the reference to TString in payload.
  62. explicit TYsonString(
  63. const TString& data,
  64. EYsonType type = EYsonType::Node);
  65. //! Constructs an instance from TSharedRef.
  66. //! Zero-copy; retains the reference to TSharedRef holder in payload.
  67. explicit TYsonString(
  68. const TSharedRef& ref,
  69. EYsonType type = EYsonType::Node);
  70. //! Returns |true| if the instance is not null.
  71. explicit operator bool() const;
  72. //! Returns type of YSON contained here. The instance must be non-null.
  73. EYsonType GetType() const;
  74. //! Returns the non-owning data. The instance must be non-null.
  75. TStringBuf AsStringBuf() const;
  76. //! Returns the data represented by TString. The instance must be non-null.
  77. //! Copies the data in case the payload is not TString.
  78. TString ToString() const;
  79. //! Returns the data represented by TSharedRef. The instance must be non-null.
  80. //! The data is never copied.
  81. TSharedRef ToSharedRef() const;
  82. //! Computes the hash code.
  83. size_t ComputeHash() const;
  84. //! Allow to serialize/deserialize using the ::Save ::Load functions. See util/ysaveload.h.
  85. void Save(IOutputStream* s) const;
  86. void Load(IInputStream* s);
  87. private:
  88. struct TNullPayload
  89. { };
  90. std::variant<TNullPayload, TSharedRangeHolderPtr, TString> Payload_;
  91. const char* Begin_;
  92. ui64 Size_ : 56;
  93. EYsonType Type_ : 8;
  94. };
  95. ////////////////////////////////////////////////////////////////////////////////
  96. bool operator == (const TYsonString& lhs, const TYsonString& rhs);
  97. bool operator == (const TYsonString& lhs, const TYsonStringBuf& rhs);
  98. bool operator == (const TYsonStringBuf& lhs, const TYsonString& rhs);
  99. bool operator == (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs);
  100. bool operator != (const TYsonString& lhs, const TYsonString& rhs);
  101. bool operator != (const TYsonString& lhs, const TYsonStringBuf& rhs);
  102. bool operator != (const TYsonStringBuf& lhs, const TYsonString& rhs);
  103. bool operator != (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs);
  104. void FormatValue(TStringBuilderBase* builder, const TYsonString& yson, TStringBuf spec);
  105. void FormatValue(TStringBuilderBase* builder, const TYsonStringBuf& yson, TStringBuf spec);
  106. ////////////////////////////////////////////////////////////////////////////////
  107. } // namespace NYT::NYson
  108. #define STRING_INL_H_
  109. #include "string-inl.h"
  110. #undef STRING_INL_H_