string_builder.h 2.7 KB

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