[llvm-commits] [llvm] r76754 - in /llvm/trunk: include/llvm/Support/raw_ostream.h unittests/ADT/StringRefTest.cpp

Daniel Dunbar daniel at zuster.org
Wed Jul 22 10:13:20 PDT 2009


Author: ddunbar
Date: Wed Jul 22 12:13:20 2009
New Revision: 76754

URL: http://llvm.org/viewvc/llvm-project?rev=76754&view=rev
Log:
Support writing a StringRef to a raw_ostream directly.

Modified:
    llvm/trunk/include/llvm/Support/raw_ostream.h
    llvm/trunk/unittests/ADT/StringRefTest.cpp

Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=76754&r1=76753&r2=76754&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Jul 22 12:13:20 2009
@@ -15,6 +15,7 @@
 #define LLVM_SUPPORT_RAW_OSTREAM_H
 
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
 #include <cassert>
 #include <cstring>
 #include <string>
@@ -151,21 +152,27 @@
     return *this;
   }
 
-  raw_ostream &operator<<(const char *Str) {
-    // Inline fast path, particulary for constant strings where a
-    // sufficiently smart compiler will simplify strlen.
-
-    size_t Size = strlen(Str);
+  raw_ostream &operator<<(const StringRef &Str) {
+    // Inline fast path, particularly for strings with a known length.
+    size_t Size = Str.size();
 
     // Make sure we can use the fast path.
     if (OutBufCur+Size > OutBufEnd)
-      return write(Str, Size);
+      return write(Str.data(), Size);
 
-    memcpy(OutBufCur, Str, Size);
+    memcpy(OutBufCur, Str.data(), Size);
     OutBufCur += Size;
     return *this;
   }
 
+  raw_ostream &operator<<(const char *Str) {
+    // Inline fast path, particulary for constant strings where a sufficiently
+    // smart compiler will simplify strlen.
+
+    this->operator<<(StringRef(Str));
+    return *this;
+  }
+
   raw_ostream &operator<<(const std::string& Str) {
     write(Str.data(), Str.length());
     return *this;

Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=76754&r1=76753&r2=76754&view=diff

==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Wed Jul 22 12:13:20 2009
@@ -9,6 +9,7 @@
 
 #include "gtest/gtest.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 namespace {
@@ -66,6 +67,11 @@
   EXPECT_TRUE(Str.startswith("he"));
   EXPECT_FALSE(Str.startswith("helloworld"));
   EXPECT_FALSE(Str.startswith("hi"));
+
+  std::string Storage;
+  raw_string_ostream OS(Storage);
+  OS << StringRef("hello");
+  EXPECT_EQ("hello", OS.str());
 }
 
 } // end anonymous namespace





More information about the llvm-commits mailing list