[llvm] 4c94aff - [ADT] Add more ArrayRef <-> StringRef conversion functions

Steven Wu via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 6 13:55:19 PDT 2023


Author: Steven Wu
Date: 2023-10-06T13:54:53-07:00
New Revision: 4c94aff508eed06b8af43e2d61bd8a5e397927a8

URL: https://github.com/llvm/llvm-project/commit/4c94aff508eed06b8af43e2d61bd8a5e397927a8
DIFF: https://github.com/llvm/llvm-project/commit/4c94aff508eed06b8af43e2d61bd8a5e397927a8.diff

LOG: [ADT] Add more ArrayRef <-> StringRef conversion functions

Add new functions in StringExtras to convert byte size type array to
StringRef and vice versa.

Reviewed By: benlangmuir, dexonsmith

Differential Revision: https://reviews.llvm.org/D139035

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringExtras.h
    llvm/unittests/ADT/StringExtrasTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 091a40dc8afd561..c6fb8b6528089cc 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -58,10 +58,19 @@ inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); }
 inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
   return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size());
 }
+inline StringRef toStringRef(ArrayRef<char> Input) {
+  return StringRef(Input.begin(), Input.size());
+}
 
 /// Construct a string ref from an array ref of unsigned chars.
-inline ArrayRef<uint8_t> arrayRefFromStringRef(StringRef Input) {
-  return {Input.bytes_begin(), Input.bytes_end()};
+template <class CharT = uint8_t>
+inline ArrayRef<CharT> arrayRefFromStringRef(StringRef Input) {
+  static_assert(std::is_same<CharT, char>::value ||
+                    std::is_same<CharT, unsigned char>::value ||
+                    std::is_same<CharT, signed char>::value,
+                "Expected byte type");
+  return ArrayRef<CharT>(reinterpret_cast<const CharT *>(Input.data()),
+                         Input.size());
 }
 
 /// Interpret the given character \p C as a hexadecimal digit and return its

diff  --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 3f69c91b270a355..6f4eb0b85ba54f6 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -362,3 +362,14 @@ TEST(StringExtrasTest, splitCharForLoop) {
     Result.push_back(x);
   EXPECT_THAT(Result, testing::ElementsAre("foo", "bar", "", "baz"));
 }
+
+TEST(StringExtrasTest, arrayToStringRef) {
+  auto roundTripTestString = [](llvm::StringRef Str) {
+    EXPECT_EQ(Str, toStringRef(arrayRefFromStringRef<uint8_t>(Str)));
+    EXPECT_EQ(Str, toStringRef(arrayRefFromStringRef<char>(Str)));
+  };
+  roundTripTestString("");
+  roundTripTestString("foo");
+  roundTripTestString("\0\n");
+  roundTripTestString("\xFF\xFE");
+}


        


More information about the llvm-commits mailing list