#define LLVM_SUPPORT_RAW_OSTREAM_H
#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
#include <cassert>
#include <cstring>
#include <string>
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;
#include "gtest/gtest.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
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