Browse Source

Add TStringBuf conversion operator for TRcBuf

add TStringBuf conversion operator

add TStringBuf conversion operator
innokentii 2 years ago
parent
commit
1ab9ee3dfe

+ 19 - 9
library/cpp/actors/util/rc_buf.h

@@ -688,7 +688,7 @@ class TRcBuf {
         }
     };
 
-    static constexpr struct TOwnedSlice {} OwnedSlice{};
+    static constexpr struct TOwnedPiece {} OwnedPiece{};
 
     TBackend Backend; // who actually holds the data
     const char *Begin; // data start
@@ -711,7 +711,7 @@ class TRcBuf {
         End = Begin + span.size();
     }
 
-    TRcBuf(TOwnedSlice, const char *data, size_t size, const TRcBuf& from)
+    TRcBuf(TOwnedPiece, const char *data, size_t size, const TRcBuf& from)
         : TRcBuf(from.Backend, {data, size})
     {
         Y_VERIFY(data >= from.GetData());
@@ -720,12 +720,12 @@ class TRcBuf {
         Backend.UpdateCookiesUnsafe(Begin, End);
     }
 
-    TRcBuf(TOwnedSlice, const char *begin, const char *end, const TRcBuf& from)
-        : TRcBuf(OwnedSlice, begin, end - begin, from)
+    TRcBuf(TOwnedPiece, const char *begin, const char *end, const TRcBuf& from)
+        : TRcBuf(OwnedPiece, begin, end - begin, from)
     {}
 
 public:
-    static constexpr struct TSlice {} Slice{};
+    static constexpr struct TPiece {} Piece{};
 
     enum class EResizeResult {
         NoAlloc,
@@ -770,7 +770,7 @@ public:
         : TRcBuf(backend, backend->GetData())
     {}
 
-    TRcBuf(TSlice, const char *data, size_t size, const TRcBuf& from)
+    TRcBuf(TPiece, const char *data, size_t size, const TRcBuf& from)
         : TRcBuf(from.Backend, {data, size})
     {
         Y_VERIFY(data >= from.GetData());
@@ -778,8 +778,8 @@ public:
         Y_VERIFY(data + size <= from.GetData() + from.GetSize());
     }
 
-    TRcBuf(TSlice, const char *begin, const char *end, const TRcBuf& from)
-        : TRcBuf(Slice, begin, end - begin, from)
+    TRcBuf(TPiece, const char *begin, const char *end, const TRcBuf& from)
+        : TRcBuf(Piece, begin, end - begin, from)
     {}
 
     TRcBuf(const TRcBuf& other)
@@ -806,7 +806,7 @@ public:
         if (headroom == 0 && tailroom == 0) {
             TInternalBackend res = TInternalBackend::Uninitialized(size);
             return TRcBuf(
-                OwnedSlice,
+                OwnedPiece,
                 res.data(),
                 res.data() + res.size(),
                 TRcBuf(res));
@@ -918,6 +918,16 @@ public:
         return {GetData(), GetSize()};
     }
 
+    TStringBuf Slice(size_t pos = 0, size_t len = -1) const noexcept {
+        pos = Min(pos, size());
+        len = Min(len, size() - pos);
+        return {const_cast<TRcBuf*>(this)->UnsafeGetDataMut() + pos, len};
+    }
+
+    explicit operator TStringBuf() const noexcept {
+        return Slice();
+    }
+
     TMutableContiguousSpan GetContiguousSpanMut() {
         return {GetDataMut(), GetSize()};
     }

+ 11 - 1
library/cpp/actors/util/rc_buf_ut.cpp

@@ -10,6 +10,16 @@ Y_UNIT_TEST_SUITE(TRcBuf) {
         UNIT_ASSERT_EQUAL(sizeof(TRcBuf), 4 * sizeof(uintptr_t));
     }
 
+    Y_UNIT_TEST(Slice) {
+        auto data = TRcBuf::Copy("Hello", 5);
+        UNIT_ASSERT_VALUES_EQUAL(TString(TStringBuf(data)), TString("Hello"));
+        UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice()), TString("Hello"));
+        UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1)), TString("ello"));
+        UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1, 3)), TString("ell"));
+        UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1, 100)), TString("ello"));
+        UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(0, 4)), TString("Hell"));
+    }
+
     Y_UNIT_TEST(CrossCompare) {
         TString str = "some very long string";
         const TString constStr(str);
@@ -159,7 +169,7 @@ Y_UNIT_TEST_SUITE(TRcBuf) {
 
     Y_UNIT_TEST(SliceUnshare) {
         TRcBuf data = TRcBuf::Uninitialized(10, 20, 30);
-        TRcBuf otherData(TRcBuf::Slice, data.data() + 1, data.size() - 2, data);
+        TRcBuf otherData(TRcBuf::Piece, data.data() + 1, data.size() - 2, data);
         UNIT_ASSERT_EQUAL(otherData.Headroom(), 0);
         UNIT_ASSERT_EQUAL(otherData.Tailroom(), 0);
     }

+ 6 - 6
library/cpp/actors/util/rope.h

@@ -407,13 +407,13 @@ public:
 
         while (begin.Iter != end.Iter) {
             const size_t size = begin.ContiguousSize();
-            Chain.PutToEnd(TRcBuf::Slice, begin.ContiguousData(), size, begin.GetChunk());
+            Chain.PutToEnd(TRcBuf::Piece, begin.ContiguousData(), size, begin.GetChunk());
             begin.AdvanceToNextContiguousBlock();
             Size += size;
         }
 
         if (begin != end && end.PointsToChunkMiddle()) {
-            Chain.PutToEnd(TRcBuf::Slice, begin.Ptr, end.Ptr, begin.GetChunk());
+            Chain.PutToEnd(TRcBuf::Piece, begin.Ptr, end.Ptr, begin.GetChunk());
             Size += end.Ptr - begin.Ptr;
         }
     }
@@ -534,7 +534,7 @@ public:
                     return;
                 }
             }
-            dest->Chain.PutToEnd(TRcBuf::Slice, first->Begin, first->Begin + num, *first);
+            dest->Chain.PutToEnd(TRcBuf::Piece, first->Begin, first->Begin + num, *first);
             first->Begin += num;
         }
     }
@@ -552,7 +552,7 @@ public:
 
         // check if we have to split the block
         if (pos.PointsToChunkMiddle()) {
-            pos.Iter = Chain.InsertBefore(pos.Iter, TRcBuf::Slice, pos->Begin, pos.Ptr, pos.GetChunk());
+            pos.Iter = Chain.InsertBefore(pos.Iter, TRcBuf::Piece, pos->Begin, pos.Ptr, pos.GetChunk());
             ++pos.Iter;
             pos->Begin = pos.Ptr;
         }
@@ -854,7 +854,7 @@ private:
 
         auto addBlock = [&](const TRcBuf& from, const char *begin, const char *end) {
             if (target) {
-                target->Chain.PutToEnd(TRcBuf::Slice, begin, end, from);
+                target->Chain.PutToEnd(TRcBuf::Piece, begin, end, from);
                 target->Size += end - begin;
             }
             Size -= end - begin;
@@ -868,7 +868,7 @@ private:
             const char *firstChunkBegin = begin.PointsToChunkMiddle() ? begin->Begin : nullptr;
             begin->Begin = end.Ptr; // this affects both begin and end iterator pointed values
             if (firstChunkBegin) {
-                Chain.InsertBefore(begin.Iter, TRcBuf::Slice, firstChunkBegin, begin.Ptr, chunkToSplit);
+                Chain.InsertBefore(begin.Iter, TRcBuf::Piece, firstChunkBegin, begin.Ptr, chunkToSplit);
             }
         } else {
             // check the first iterator -- if it starts not from the begin of the block, we have to adjust end of the