Browse Source

YT: Support multiple literals in YPathJoin

dgolear 2 years ago
parent
commit
b1e6ce7af7
2 changed files with 14 additions and 7 deletions
  1. 10 5
      library/cpp/yt/string/string_builder-inl.h
  2. 4 2
      library/cpp/yt/string/string_builder.h

+ 10 - 5
library/cpp/yt/string/string_builder-inl.h

@@ -12,13 +12,18 @@ namespace NYT {
 
 inline char* TStringBuilderBase::Preallocate(size_t size)
 {
-    if (Y_UNLIKELY(End_ - Current_ < static_cast<ssize_t>(size))) {
+    Reserve(size + GetLength());
+    return Current_;
+}
+
+inline void TStringBuilderBase::Reserve(size_t size)
+{
+    if (Y_UNLIKELY(End_ - Begin_ < static_cast<ssize_t>(size))) {
         size_t length = GetLength();
-        auto newLength = std::max(length + size, MinBufferLength);
-        DoPreallocate(newLength);
+        auto newLength = std::max(size, MinBufferLength);
+        DoReserve(newLength);
         Current_ = Begin_ + length;
     }
-    return Current_;
 }
 
 inline size_t TStringBuilderBase::GetLength() const
@@ -100,7 +105,7 @@ inline void TStringBuilder::DoReset()
     Buffer_ = {};
 }
 
-inline void TStringBuilder::DoPreallocate(size_t newLength)
+inline void TStringBuilder::DoReserve(size_t newLength)
 {
     Buffer_.ReserveAndResize(newLength);
     auto capacity = Buffer_.capacity();

+ 4 - 2
library/cpp/yt/string/string_builder.h

@@ -26,6 +26,8 @@ public:
 
     char* Preallocate(size_t size);
 
+    void Reserve(size_t size);
+
     size_t GetLength() const;
 
     TStringBuf GetBuffer() const;
@@ -51,7 +53,7 @@ protected:
     char* End_ = nullptr;
 
     virtual void DoReset() = 0;
-    virtual void DoPreallocate(size_t newLength) = 0;
+    virtual void DoReserve(size_t newLength) = 0;
 
     static constexpr size_t MinBufferLength = 128;
 };
@@ -68,7 +70,7 @@ protected:
     TString Buffer_;
 
     void DoReset() override;
-    void DoPreallocate(size_t size) override;
+    void DoReserve(size_t size) override;
 };
 
 ////////////////////////////////////////////////////////////////////////////////