string_builder.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <util/generic/string.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // Forward declarations.
  6. class TStringBuilderBase;
  7. class TStringBuilder;
  8. class TDelimitedStringBuilderWrapper;
  9. template <size_t Length, class... TArgs>
  10. void Format(TStringBuilderBase* builder, const char (&format)[Length], TArgs&&... args);
  11. template <class... TArgs>
  12. void Format(TStringBuilderBase* builder, TStringBuf format, TArgs&&... args);
  13. ////////////////////////////////////////////////////////////////////////////////
  14. //! A simple helper for constructing strings by a sequence of appends.
  15. class TStringBuilderBase
  16. {
  17. public:
  18. virtual ~TStringBuilderBase() = default;
  19. char* Preallocate(size_t size);
  20. void Reserve(size_t size);
  21. size_t GetLength() const;
  22. TStringBuf GetBuffer() const;
  23. void Advance(size_t size);
  24. void AppendChar(char ch);
  25. void AppendChar(char ch, int n);
  26. void AppendString(TStringBuf str);
  27. void AppendString(const char* str);
  28. template <size_t Length, class... TArgs>
  29. void AppendFormat(const char (&format)[Length], TArgs&&... args);
  30. template <class... TArgs>
  31. void AppendFormat(TStringBuf format, TArgs&&... args);
  32. void Reset();
  33. protected:
  34. char* Begin_ = nullptr;
  35. char* Current_ = nullptr;
  36. char* End_ = nullptr;
  37. virtual void DoReset() = 0;
  38. virtual void DoReserve(size_t newLength) = 0;
  39. static constexpr size_t MinBufferLength = 128;
  40. };
  41. ////////////////////////////////////////////////////////////////////////////////
  42. class TStringBuilder
  43. : public TStringBuilderBase
  44. {
  45. public:
  46. TString Flush();
  47. protected:
  48. TString Buffer_;
  49. void DoReset() override;
  50. void DoReserve(size_t size) override;
  51. };
  52. ////////////////////////////////////////////////////////////////////////////////
  53. template <class T>
  54. TString ToStringViaBuilder(const T& value, TStringBuf spec = TStringBuf("v"));
  55. ////////////////////////////////////////////////////////////////////////////////
  56. //! Appends a certain delimiter starting from the second call.
  57. class TDelimitedStringBuilderWrapper
  58. : private TNonCopyable
  59. {
  60. public:
  61. TDelimitedStringBuilderWrapper(
  62. TStringBuilderBase* builder,
  63. TStringBuf delimiter = TStringBuf(", "))
  64. : Builder_(builder)
  65. , Delimiter_(delimiter)
  66. { }
  67. TStringBuilderBase* operator->()
  68. {
  69. if (!FirstCall_) {
  70. Builder_->AppendString(Delimiter_);
  71. }
  72. FirstCall_ = false;
  73. return Builder_;
  74. }
  75. private:
  76. TStringBuilderBase* const Builder_;
  77. const TStringBuf Delimiter_;
  78. bool FirstCall_ = true;
  79. };
  80. ////////////////////////////////////////////////////////////////////////////////
  81. } // namespace NYT
  82. #define STRING_BUILDER_INL_H_
  83. #include "string_builder-inl.h"
  84. #undef STRING_BUILDER_INL_H_