[llvm] r258535 - Add ArrayRef support to EndianStream.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 22 11:44:47 PST 2016


Author: rafael
Date: Fri Jan 22 13:44:46 2016
New Revision: 258535

URL: http://llvm.org/viewvc/llvm-project?rev=258535&view=rev
Log:
Add ArrayRef support to EndianStream.

Using an array instead of ArrayRef would allow type inference, but
(short of using C99) one would still need to write

    typedef uint16_t VT[];
    LE.write(VT{0x1234, 0x5678});

Modified:
    llvm/trunk/include/llvm/Support/EndianStream.h
    llvm/trunk/unittests/Support/EndianStreamTest.cpp

Modified: llvm/trunk/include/llvm/Support/EndianStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/EndianStream.h?rev=258535&r1=258534&r2=258535&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/EndianStream.h (original)
+++ llvm/trunk/include/llvm/Support/EndianStream.h Fri Jan 22 13:44:46 2016
@@ -15,6 +15,7 @@
 #ifndef LLVM_SUPPORT_ENDIANSTREAM_H
 #define LLVM_SUPPORT_ENDIANSTREAM_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -26,6 +27,12 @@ namespace endian {
 template <endianness endian> struct Writer {
   raw_ostream &OS;
   Writer(raw_ostream &OS) : OS(OS) {}
+  template <typename value_type> void write(ArrayRef<value_type> Vals) {
+    for (value_type V : Vals) {
+      value_type Swaped = byte_swap<value_type, endian>(V);
+      OS.write((const char *)&Swaped, sizeof(value_type));
+    }
+  }
   template <typename value_type> void write(value_type Val) {
     Val = byte_swap<value_type, endian>(Val);
     OS.write((const char *)&Val, sizeof(value_type));

Modified: llvm/trunk/unittests/Support/EndianStreamTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/EndianStreamTest.cpp?rev=258535&r1=258534&r2=258535&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/EndianStreamTest.cpp (original)
+++ llvm/trunk/unittests/Support/EndianStreamTest.cpp Fri Jan 22 13:44:46 2016
@@ -153,5 +153,35 @@ TEST(EndianStream, WriteDoubleBE) {
   EXPECT_EQ(static_cast<uint8_t>(data[7]), 0x20);
 }
 
+TEST(EndianStream, WriteArrayLE) {
+  SmallString<16> Data;
+
+  {
+    raw_svector_ostream OS(Data);
+    endian::Writer<little> LE(OS);
+    LE.write<uint16_t>({0x1234, 0x5678});
+  }
+
+  EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
+  EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
+  EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
+  EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
+}
+
+TEST(EndianStream, WriteVectorLE) {
+  SmallString<16> Data;
+
+  {
+    raw_svector_ostream OS(Data);
+    endian::Writer<little> LE(OS);
+    std::vector<uint16_t> Vec{0x1234, 0x5678};
+    LE.write<uint16_t>(Vec);
+  }
+
+  EXPECT_EQ(static_cast<uint8_t>(Data[0]), 0x34);
+  EXPECT_EQ(static_cast<uint8_t>(Data[1]), 0x12);
+  EXPECT_EQ(static_cast<uint8_t>(Data[2]), 0x78);
+  EXPECT_EQ(static_cast<uint8_t>(Data[3]), 0x56);
+}
 
 } // end anon namespace




More information about the llvm-commits mailing list