[libcxx-commits] [PATCH] D111948: [Demangle] Add prepend functionality to OutputString

Luís Ferreira via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 21 18:35:54 PDT 2021


ljmf00 updated this revision to Diff 381454.
ljmf00 added a comment.

Renamed OutputString test to OutputBuffer test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111948/new/

https://reviews.llvm.org/D111948

Files:
  libcxxabi/src/demangle/Utility.h
  llvm/include/llvm/Demangle/Utility.h
  llvm/unittests/Demangle/OutputBufferTest.cpp


Index: llvm/unittests/Demangle/OutputBufferTest.cpp
===================================================================
--- llvm/unittests/Demangle/OutputBufferTest.cpp
+++ llvm/unittests/Demangle/OutputBufferTest.cpp
@@ -60,3 +60,21 @@
 
   std::free(OB.getBuffer());
 }
+
+TEST(OutputBufferTest, Prepend) {
+  OutputBuffer OB;
+
+  OB.prepend("n");
+  EXPECT_EQ("n", toString(OB));
+
+  OB << "abc";
+  OB.prepend("def");
+  EXPECT_EQ("defnabc", toString(OB));
+
+  OB.setCurrentPosition(3);
+
+  OB.prepend("abc");
+  EXPECT_EQ("abcdef", toString(OB));
+
+  std::free(OB.getBuffer());
+}
Index: llvm/include/llvm/Demangle/Utility.h
===================================================================
--- llvm/include/llvm/Demangle/Utility.h
+++ llvm/include/llvm/Demangle/Utility.h
@@ -95,6 +95,17 @@
 
   OutputBuffer &operator<<(StringView R) { return (*this += R); }
 
+  OutputBuffer prepend(StringView R) {
+    size_t Size = R.size();
+
+    grow(Size);
+    std::memmove(Buffer + Size, Buffer, CurrentPosition);
+    std::memcpy(Buffer, R.begin(), Size);
+    CurrentPosition += Size;
+
+    return *this;
+  }
+
   OutputBuffer &operator<<(char C) { return (*this += C); }
 
   OutputBuffer &operator<<(long long N) {
Index: libcxxabi/src/demangle/Utility.h
===================================================================
--- libcxxabi/src/demangle/Utility.h
+++ libcxxabi/src/demangle/Utility.h
@@ -95,6 +95,17 @@
 
   OutputBuffer &operator<<(StringView R) { return (*this += R); }
 
+  OutputBuffer prepend(StringView R) {
+    size_t Size = R.size();
+
+    grow(Size);
+    std::memmove(Buffer + Size, Buffer, CurrentPosition);
+    std::memcpy(Buffer, R.begin(), Size);
+    CurrentPosition += Size;
+
+    return *this;
+  }
+
   OutputBuffer &operator<<(char C) { return (*this += C); }
 
   OutputBuffer &operator<<(long long N) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111948.381454.patch
Type: text/x-patch
Size: 1855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211022/9f3956f1/attachment.bin>


More information about the libcxx-commits mailing list